1---
2- hosts: 127.0.0.1
3  connection: local  # otherwise Ansible will complain that it cannot connect via ssh to 127.0.0.1:22
4  gather_facts: no
5  tasks:
6    - name: Show all groups
7      debug:
8        var: groups
9    - name: Make sure docker_swarm groups are there
10      assert:
11        that:
12          - groups.all | length > 0
13          - groups.leader | length == 1
14          - groups.manager | length > 0
15          - groups.worker | length >= 0
16          - groups.nonleaders | length >= 0
17
18- hosts: all
19  connection: local  # otherwise Ansible will complain that it cannot connect via ssh to 127.0.0.1:22
20  vars:
21    # for some reason, Ansible can't find the Python interpreter when connecting to the nodes,
22    # which is in fact just localhost in disguise. That's why we use ansible_playbook_python.
23    ansible_python_interpreter: "{{ ansible_playbook_python }}"
24  tasks:
25    - name: Check for groups
26      assert:
27        that:
28          - "groups.manager | length > 0"
29          - "groups.worker | length >= 0"
30          - "groups.leader | length == 1"
31      run_once: yes
32
33    - name: List manager group
34      debug:
35        var: groups.manager
36      run_once: yes
37
38    - name: List worker group
39      debug:
40        var: groups.worker
41      run_once: yes
42
43    - name: List leader group
44      debug:
45        var: groups.leader
46      run_once: yes
47
48    - name: Print ansible_host per host
49      debug:
50        var: ansible_host
51
52    - name: Make sure docker_swarm_node_attributes is available
53      assert:
54        that:
55          - docker_swarm_node_attributes is not undefined
56    - name: Print docker_swarm_node_attributes per host
57      debug:
58        var: docker_swarm_node_attributes
59