diff --git a/changelogs/fragments/44808-docker_container-idempotency.yaml b/changelogs/fragments/44808-docker_container-idempotency.yaml new file mode 100644 index 00000000000..f51ccb6bf14 --- /dev/null +++ b/changelogs/fragments/44808-docker_container-idempotency.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "improves docker_container idempotency (https://github.com/ansible/ansible/pull/44808)" diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 55c6bce377e..98da826bb0a 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1240,7 +1240,6 @@ class Container(DockerBaseClass): # Map parameters to container inspect results config_mapping = dict( - auto_remove=host_config.get('AutoRemove'), expected_cmd=config.get('Cmd'), domainname=config.get('Domainname'), hostname=config.get('Hostname'), @@ -1260,8 +1259,6 @@ class Container(DockerBaseClass): ipc_mode=host_config.get("IpcMode"), labels=config.get('Labels'), expected_links=host_config.get('Links'), - log_driver=log_config.get('Type'), - log_options=log_config.get('Config'), mac_address=network.get('MacAddress'), memory_swappiness=host_config.get('MemorySwappiness'), network_mode=host_config.get('NetworkMode'), @@ -1273,7 +1270,6 @@ class Container(DockerBaseClass): expected_ports=host_config.get('PortBindings'), read_only=host_config.get('ReadonlyRootfs'), restart_policy=restart_policy.get('Name'), - restart_retries=restart_policy.get('MaximumRetryCount'), # Cannot test shm_size, as shm_size is not included in container inspection results. # shm_size=host_config.get('ShmSize'), security_opts=host_config.get("SecurityOpt"), @@ -1286,9 +1282,21 @@ class Container(DockerBaseClass): expected_volumes=config.get('Volumes'), expected_binds=host_config.get('Binds'), volumes_from=host_config.get('VolumesFrom'), - volume_driver=host_config.get('VolumeDriver'), working_dir=host_config.get('WorkingDir') ) + if self.parameters.restart_policy: + config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount') + if self.parameters.log_driver: + config_mapping['log_driver'] = log_config.get('Type') + config_mapping['log_options'] = log_config.get('Config') + + if self.parameters.client.HAS_AUTO_REMOVE_OPT: + # auto_remove is only supported in docker>=2 + config_mapping['auto_remove'] = host_config.get('AutoRemove') + + if HAS_DOCKER_PY_3: + # volume_driver moved to create_host_config in > 3 + config_mapping['volume_driver'] = host_config.get('VolumeDriver') differences = [] for key, value in config_mapping.items(): @@ -1386,7 +1394,6 @@ class Container(DockerBaseClass): cpu_quota=host_config.get('CpuQuota'), cpuset_cpus=host_config.get('CpusetCpus'), cpuset_mems=host_config.get('CpusetMems'), - cpu_shares=host_config.get('CpuShares'), kernel_memory=host_config.get("KernelMemory"), memory=host_config.get('Memory'), memory_reservation=host_config.get('MemoryReservation'), @@ -1395,6 +1402,10 @@ class Container(DockerBaseClass): oom_killer=host_config.get('OomKillDisable'), ) + if HAS_DOCKER_PY_3: + # cpu_shares moved to create_host_config in > 3 + config_mapping['cpu_shares'] = host_config.get('CpuShares') + differences = [] for key, value in config_mapping.items(): if getattr(self.parameters, key, None) and getattr(self.parameters, key) != value: @@ -1703,6 +1714,11 @@ class ContainerManager(DockerBaseClass): super(ContainerManager, self).__init__() + if client.module.params.get('log_options') and not client.module.params.get('log_driver'): + client.module.warn('log_options is ignored when log_driver is not specified') + if client.module.params.get('restart_retries') and not client.module.params.get('restart_policy'): + client.module.warn('restart_retries is ignored when restart_policy is not specified') + self.client = client self.parameters = TaskParameters(client) self.check_mode = self.client.check_mode