diff --git a/changelogs/fragments/53003-docker_swarm-force-new-cluster.yml b/changelogs/fragments/53003-docker_swarm-force-new-cluster.yml new file mode 100644 index 00000000000..f2a4d885e3f --- /dev/null +++ b/changelogs/fragments/53003-docker_swarm-force-new-cluster.yml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_swarm - the ``force`` option was ignored when ``state: present``." diff --git a/lib/ansible/modules/cloud/docker/docker_swarm.py b/lib/ansible/modules/cloud/docker/docker_swarm.py index 4ca8bd78cb4..f86fe3caa96 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm.py @@ -254,7 +254,6 @@ class TaskParameters(DockerBaseClass): self.advertise_addr = None self.listen_addr = None - self.force_new_cluster = None self.remote_addrs = None self.join_token = None @@ -353,8 +352,8 @@ class TaskParameters(DockerBaseClass): def compare_to_active(self, other): for k in self.__dict__: - if k in ('advertise_addr', 'listen_addr', 'force_new_cluster', 'remote_addrs', - 'join_token', 'force', 'rotate_worker_token', 'rotate_manager_token', 'spec'): + if k in ('advertise_addr', 'listen_addr', 'remote_addrs', 'join_token', + 'rotate_worker_token', 'rotate_manager_token', 'spec'): continue if self.__dict__[k] is None: continue @@ -414,7 +413,7 @@ class SwarmManager(DockerBaseClass): return def init_swarm(self): - if self.__isSwarmManager(): + if not self.force and self.__isSwarmManager(): self.__update_swarm() return @@ -422,7 +421,7 @@ class SwarmManager(DockerBaseClass): try: self.client.init_swarm( advertise_addr=self.parameters.advertise_addr, listen_addr=self.parameters.listen_addr, - force_new_cluster=self.parameters.force_new_cluster, swarm_spec=self.parameters.spec) + force_new_cluster=self.force, swarm_spec=self.parameters.spec) except APIError as exc: self.client.fail("Can not create a new Swarm Cluster: %s" % to_native(exc)) diff --git a/test/integration/targets/docker_swarm/tasks/tests/basic.yml b/test/integration/targets/docker_swarm/tasks/tests/basic.yml index 98bfbf56d24..75c7fc6eeb6 100644 --- a/test/integration/targets/docker_swarm/tasks/tests/basic.yml +++ b/test/integration/targets/docker_swarm/tasks/tests/basic.yml @@ -61,6 +61,23 @@ diff: yes register: output_4 +- name: Create a Swarm cluster (force re-create) + docker_swarm: + state: present + advertise_addr: "{{ansible_default_ipv4.address}}" + force: yes + diff: yes + register: output_5 + +- name: Create a Swarm cluster (force re-create, check mode) + docker_swarm: + state: present + advertise_addr: "{{ansible_default_ipv4.address}}" + force: yes + check_mode: yes + diff: yes + register: output_6 + - name: assert changed when create a new swarm cluster assert: that: @@ -79,6 +96,12 @@ - 'output_4 is not changed' - 'output_4.diff.before is defined' - 'output_4.diff.after is defined' + - 'output_5 is changed' + - 'output_5.diff.before is defined' + - 'output_5.diff.after is defined' + - 'output_6 is changed' + - 'output_6.diff.before is defined' + - 'output_6.diff.after is defined' #################################################################### ## Removal #########################################################