diff --git a/changelogs/fragments/3331-do_not_ignore_volatile_configs_by_option.yml b/changelogs/fragments/3331-do_not_ignore_volatile_configs_by_option.yml new file mode 100644 index 0000000000..3e176c9b49 --- /dev/null +++ b/changelogs/fragments/3331-do_not_ignore_volatile_configs_by_option.yml @@ -0,0 +1,2 @@ +minor_changes: + - "lxd_container - add ``ignore_volatile_options`` option which allows to disable the behavior that the module ignores options starting with ``volatile.`` (https://github.com/ansible-collections/community.general/pull/3331)." \ No newline at end of file diff --git a/plugins/modules/cloud/lxd/lxd_container.py b/plugins/modules/cloud/lxd/lxd_container.py index f4a97288ae..16eea8f44c 100644 --- a/plugins/modules/cloud/lxd/lxd_container.py +++ b/plugins/modules/cloud/lxd/lxd_container.py @@ -31,16 +31,24 @@ options: description: - 'The config for the container (e.g. {"limits.cpu": "2"}). See U(https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1)' - - If the container already exists and its "config" value in metadata - obtained from - GET /1.0/containers/ + - If the container already exists and its "config" values in metadata + obtained from GET /1.0/containers/ U(https://github.com/lxc/lxd/blob/master/doc/rest-api.md#10containersname) - are different, they this module tries to apply the configurations. - - The key starts with 'volatile.' are ignored for this comparison. - - Not all config values are supported to apply the existing container. - Maybe you need to delete and recreate a container. + are different, this module tries to apply the configurations. + - The keys starting with C(volatile.) are ignored for this comparison when I(ignore_volatile_options=true). type: dict required: false + ignore_volatile_options: + description: + - If set to C(true), options starting with C(volatile.) are ignored. As a result, + they are reapplied for each execution. + - This default behavior can be changed by setting this option to C(false). + - The default value C(true) will be deprecated in community.general 4.0.0, + and will change to C(false) in community.general 5.0.0. + type: bool + default: true + required: false + version_added: 3.7.0 profiles: description: - Profile to be used by the container @@ -176,6 +184,7 @@ EXAMPLES = ''' - name: Create a started container community.general.lxd_container: name: mycontainer + ignore_volatile_options: true state: started source: type: image @@ -209,6 +218,7 @@ EXAMPLES = ''' - name: Create a started container community.general.lxd_container: name: mycontainer + ignore_volatile_options: true state: started source: type: image @@ -279,6 +289,7 @@ EXAMPLES = ''' - name: Create LXD container community.general.lxd_container: name: new-container-1 + ignore_volatile_options: true state: started source: type: image @@ -289,6 +300,7 @@ EXAMPLES = ''' - name: Create container on another node community.general.lxd_container: name: new-container-2 + ignore_volatile_options: true state: started source: type: image @@ -557,7 +569,7 @@ class LXDContainerManagement(object): def _needs_to_change_container_config(self, key): if key not in self.config: return False - if key == 'config': + if key == 'config' and self.ignore_volatile_options: # the old behavior is to ignore configurations by keyword "volatile" old_configs = dict((k, v) for k, v in self.old_container_json['metadata'][key].items() if not k.startswith('volatile.')) for k, v in self.config['config'].items(): if k not in old_configs: @@ -565,6 +577,14 @@ class LXDContainerManagement(object): if old_configs[k] != v: return True return False + elif key == 'config': # next default behavior + old_configs = dict((k, v) for k, v in self.old_container_json['metadata'][key].items()) + for k, v in self.config['config'].items(): + if k not in old_configs: + return True + if old_configs[k] != v: + return True + return False else: old_configs = self.old_container_json['metadata'][key] return self.config[key] != old_configs @@ -606,6 +626,7 @@ class LXDContainerManagement(object): try: if self.trust_password is not None: self.client.authenticate(self.trust_password) + self.ignore_volatile_options = self.module.params.get('ignore_volatile_options') self.old_container_json = self._get_container_json() self.old_state = self._container_json_to_module_state(self.old_container_json) @@ -651,6 +672,10 @@ def main(): config=dict( type='dict', ), + ignore_volatile_options=dict( + type='bool', + default=True + ), devices=dict( type='dict', ), @@ -703,7 +728,13 @@ def main(): ), supports_check_mode=False, ) - + # if module.params['ignore_volatile_options'] is None: + # module.params['ignore_volatile_options'] = True + # module.deprecate( + # 'If the keyword "volatile" is used in a playbook in the config section, a + # "changed" message will appear with every run, even without a change to the playbook. + # This will change in the future. + # Please test your scripts by "ignore_volatile_options: false"', version='5.0.0', collection_name='community.general') lxd_manage = LXDContainerManagement(module=module) lxd_manage.run()