From bf3c291606ccd92272038d7df00b10ba791e4592 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 26 Feb 2019 12:57:02 +0100 Subject: [PATCH] docker_swarm: add diff support (#52947) * Add diff support to docker_swarm. * Add changelog. * Using diff mode for docker_swarm tests. This improves debugging, since the the module now outputs more information on *why* it did certain actions. --- .../fragments/52947-docker_swarm-diff.yaml | 2 ++ .../modules/cloud/docker/docker_swarm.py | 5 ++++ .../targets/docker_swarm/tasks/test_swarm.yml | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 changelogs/fragments/52947-docker_swarm-diff.yaml diff --git a/changelogs/fragments/52947-docker_swarm-diff.yaml b/changelogs/fragments/52947-docker_swarm-diff.yaml new file mode 100644 index 00000000000..7eee70e4edc --- /dev/null +++ b/changelogs/fragments/52947-docker_swarm-diff.yaml @@ -0,0 +1,2 @@ +minor_changes: +- "docker_swarm - module now supports ``--diff`` mode." diff --git a/lib/ansible/modules/cloud/docker/docker_swarm.py b/lib/ansible/modules/cloud/docker/docker_swarm.py index f45b44134a0..f076c3a3f45 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm.py @@ -391,6 +391,11 @@ class SwarmManager(DockerBaseClass): choice_map.get(self.state)() + if self.client.module._diff or self.parameters.debug: + diff = dict() + diff['before'], diff['after'] = self.differences.get_before_after() + self.results['diff'] = diff + def __isSwarmManager(self): try: data = self.client.inspect_swarm() diff --git a/test/integration/targets/docker_swarm/tasks/test_swarm.yml b/test/integration/targets/docker_swarm/tasks/test_swarm.yml index 28cf74c826f..01d5738cd5f 100644 --- a/test/integration/targets/docker_swarm/tasks/test_swarm.yml +++ b/test/integration/targets/docker_swarm/tasks/test_swarm.yml @@ -4,6 +4,7 @@ docker_swarm: state: absent force: true + diff: no - name: Test parameters with state=join docker_swarm: @@ -34,18 +35,21 @@ state: present advertise_addr: "{{ansible_default_ipv4.address}}" check_mode: yes + diff: yes register: output_1 - name: Create a Swarm cluster docker_swarm: state: present advertise_addr: "{{ansible_default_ipv4.address}}" + diff: yes register: output_2 - name: Create a Swarm cluster (idempotent) docker_swarm: state: present advertise_addr: "{{ansible_default_ipv4.address}}" + diff: yes register: output_3 - name: Create a Swarm cluster (idempotent, check mode) @@ -53,36 +57,48 @@ state: present advertise_addr: "{{ansible_default_ipv4.address}}" check_mode: yes + diff: yes register: output_4 - name: assert changed when create a new swarm cluster assert: that: - 'output_1 is changed' + - 'output_1.diff.before is defined' + - 'output_1.diff.after is defined' - 'output_2 is changed' - 'output_2.actions[0] | regex_search("New Swarm cluster created: ")' - 'output_2.swarm_facts.JoinTokens.Manager' - 'output_2.swarm_facts.JoinTokens.Worker' + - 'output_2.diff.before is defined' + - 'output_2.diff.after is defined' - 'output_3 is not changed' + - 'output_3.diff.before is defined' + - 'output_3.diff.after is defined' - 'output_4 is not changed' + - 'output_4.diff.before is defined' + - 'output_4.diff.after is defined' - name: Remove a Swarm cluster (check mode) docker_swarm: state: absent force: true check_mode: yes + diff: yes register: output_1 - name: Remove a Swarm cluster docker_swarm: state: absent force: true + diff: yes register: output_2 - name: Remove a Swarm cluster (idempotent) docker_swarm: state: absent force: true + diff: yes register: output_3 - name: Remove a Swarm cluster (idempotent, check mode) @@ -90,19 +106,29 @@ state: absent force: true check_mode: yes + diff: yes register: output_4 - name: assert changed when remove a swarm cluster assert: that: - 'output_1 is changed' + - 'output_1.diff.before is defined' + - 'output_1.diff.after is defined' - 'output_2 is changed' - 'output_2.actions[0] == "Node has left the swarm cluster"' + - 'output_2.diff.before is defined' + - 'output_2.diff.after is defined' - 'output_3 is not changed' + - 'output_3.diff.before is defined' + - 'output_3.diff.after is defined' - 'output_4 is not changed' + - 'output_4.diff.before is defined' + - 'output_4.diff.after is defined' always: - name: Cleanup docker_swarm: state: absent force: true + diff: no