
Allow to update server attributes such as its description. Changed default value of server attribute 'security_groups' from ['default'] to [] because the latter is the default in python-openstackclient [1] and the former behavior causes issues with existing servers [2]: Previously, when no 'security_groups' parameter was given, the server module would change existing servers to use the default security group, dropping all other security groups assigned to the server. Our (undocumented) guideline when writing modules is to only add or change what has been requested by the user and to stick to defaults from openstacksdk and python-openstackclient whenever possible. Since we have to break backward compatibility with the next release anyway, we take this opportunity to clean up this odd behavior. Now, when no security groups are given, then security groups of an existing server will not be touched. Closes story #2007893 [2]. Note, Nova will create a server in the default security group, if the security_groups parameter is omitted. Dropped 'openstack' field from server module's results. This variable expanded to additional server information which might be useful for Ansible inventories and was filled from openstacksdk's get_openstack_vars() function [3]. Variables in this function can make additional cloud queries to retrieve additional data, so calling this function can be expensive [4]. Users can use *_info modules to retrieve this data on-demand. Dropped 'availabity_zone' attribute from generic OpenStackModule arguments and inserted it into server and volume modules because it is relevant to those two modules only. This is completes what was started years ago [5] and is possible now since we have breaking changes anyway. Switched attribute name 'userdata' with its alias 'user_data' to match openstacksdk's attribute names which are used e.g. in module results. The previous attribute name 'userdata' is now used as an alias and 'user_data' is used as the attribute name to keep backward compatibility. Wait for server to get into 'ACTIVE' state when creating a server and attribute 'wait' has been set to true. Sorted argument specs and documentation of the server module and marked attributes which are not updatable. Changed unstable bash script example in server module documentation. Renamed server's module attribute 'delete_fip' to 'delete_ips' to match openstacksdk and clarify that it includes all floating ip addresses of the server. Renamed server_info's module attribute 'server' to 'name' and added the former as an alias to be consistent with other *_info modules. Added RETURN fields documentation for the module results of both server and server_info modules. Added description and examples of how to use the 'filters' attribute of the server_info module. Closes story #2007873 [6]. Removed 'openstack_' prefix from module results because the prefix is not consistently used across modules, is more to type without any benefit and removal of the prefix allows us to signal to users that their code for handling module results has to be updated. Many modules have different return values with openstacksdk >= 0.99.0 because it consistently uses resource proxies now. Added assertions for module results to catch future changes in the openstacksdk and our Ansible modules. Added integration tests to check the update mechanism of the server module. Fixed indentation in integration tests. Ensure proper creation and deletion of resources such as networks, subnets and servers in integration tests of server_action module. Renamed ci/roles/server/defaults/main.yaml to main.yml, removing the 'a' in the file extension to be consistent with other filenames. Dropped deprecated function openstack_find_nova_addresses() and incorporated its code directly into the server module because it is not used anywhere else. [1]e49ad1795b/openstackclient/compute/v2/server.py (L1070)
[2] https://storyboard.openstack.org/#!/story/2007893 [3]9e9fc98795/openstack/cloud/_compute.py (L1772)
[4]9e9fc98795/openstack/cloud/meta.py (L482)
[5]9bf33e56dd
[6] https://storyboard.openstack.org/#!/story/2007873 Signed-off-by: Jakob Meng <code@jakobmeng.de> Change-Id: I2f955519a7e8c782b1dab8f94f7a019ed384b81d
161 lines
4.7 KiB
Python
161 lines
4.7 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2016, Mario Santos <mario.rf.santos@gmail.com>
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: server_metadata
|
|
short_description: Add/Update/Delete Metadata in Compute Instances from OpenStack
|
|
author: OpenStack Ansible SIG
|
|
description:
|
|
- Add, Update or Remove metadata in compute instances from OpenStack.
|
|
options:
|
|
server:
|
|
description:
|
|
- Name of the instance to update the metadata
|
|
required: true
|
|
aliases: ['name']
|
|
type: str
|
|
meta:
|
|
description:
|
|
- 'A list of key value pairs that should be provided as a metadata to
|
|
the instance or a string containing a list of key-value pairs.
|
|
Eg: meta: "key1=value1,key2=value2"'
|
|
required: true
|
|
type: dict
|
|
state:
|
|
description:
|
|
- Should the resource be present or absent.
|
|
choices: [present, absent]
|
|
default: present
|
|
type: str
|
|
requirements:
|
|
- "python >= 3.6"
|
|
- "openstacksdk"
|
|
|
|
extends_documentation_fragment:
|
|
- openstack.cloud.openstack
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
# Creates or updates hostname=test1 as metadata of the server instance vm1
|
|
- name: add metadata to compute instance
|
|
hosts: localhost
|
|
tasks:
|
|
- name: add metadata to instance
|
|
openstack.cloud.server_metadata:
|
|
state: present
|
|
auth:
|
|
auth_url: https://openstack-api.example.com:35357/v2.0/
|
|
username: admin
|
|
password: admin
|
|
project_name: admin
|
|
name: vm1
|
|
meta:
|
|
hostname: test1
|
|
group: group1
|
|
|
|
# Removes the keys under meta from the instance named vm1
|
|
- name: delete metadata from compute instance
|
|
hosts: localhost
|
|
tasks:
|
|
- name: delete metadata from instance
|
|
openstack.cloud.server_metadata:
|
|
state: absent
|
|
auth:
|
|
auth_url: https://openstack-api.example.com:35357/v2.0/
|
|
username: admin
|
|
password: admin
|
|
project_name: admin
|
|
name: vm1
|
|
meta:
|
|
hostname:
|
|
group:
|
|
'''
|
|
|
|
RETURN = '''
|
|
server_id:
|
|
description: The compute instance id where the change was made
|
|
returned: success
|
|
type: str
|
|
sample: "324c4e91-3e03-4f62-9a4d-06119a8a8d16"
|
|
metadata:
|
|
description: The metadata of compute instance after the change
|
|
returned: success
|
|
type: dict
|
|
sample: {'key1': 'value1', 'key2': 'value2'}
|
|
'''
|
|
|
|
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
|
|
|
|
|
class ServerMetadataModule(OpenStackModule):
|
|
argument_spec = dict(
|
|
server=dict(required=True, aliases=['name']),
|
|
meta=dict(required=True, type='dict'),
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
)
|
|
module_kwargs = dict(
|
|
supports_check_mode=True
|
|
)
|
|
|
|
def _needs_update(self, server_metadata=None, metadata=None):
|
|
if server_metadata is None:
|
|
server_metadata = {}
|
|
if metadata is None:
|
|
metadata = {}
|
|
return len(set(metadata.items()) - set(server_metadata.items())) != 0
|
|
|
|
def _get_keys_to_delete(self, server_metadata_keys=None, metadata_keys=None):
|
|
if server_metadata_keys is None:
|
|
server_metadata_keys = []
|
|
if metadata_keys is None:
|
|
metadata_keys = []
|
|
return set(server_metadata_keys) & set(metadata_keys)
|
|
|
|
def run(self):
|
|
state = self.params['state']
|
|
server_param = self.params['server']
|
|
meta_param = self.params['meta']
|
|
changed = False
|
|
|
|
server = self.conn.get_server(server_param)
|
|
if not server:
|
|
self.fail_json(
|
|
msg='Could not find server {0}'.format(server_param))
|
|
|
|
if state == 'present':
|
|
# check if it needs update
|
|
if self._needs_update(
|
|
server_metadata=server.metadata, metadata=meta_param
|
|
):
|
|
if not self.ansible.check_mode:
|
|
self.conn.set_server_metadata(server_param, meta_param)
|
|
changed = True
|
|
elif state == 'absent':
|
|
# remove from params the keys that do not exist in the server
|
|
keys_to_delete = self._get_keys_to_delete(
|
|
server.metadata.keys(), meta_param.keys())
|
|
if len(keys_to_delete) > 0:
|
|
if not self.ansible.check_mode:
|
|
self.conn.delete_server_metadata(
|
|
server_param, keys_to_delete)
|
|
changed = True
|
|
|
|
if changed:
|
|
server = self.conn.get_server(server_param)
|
|
|
|
self.exit_json(
|
|
changed=changed, server_id=server.id, metadata=server.metadata)
|
|
|
|
|
|
def main():
|
|
module = ServerMetadataModule()
|
|
module()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|