I am using ansible 2.9.27 with a dynamic inventory on Hetzner cloud servers.
I have 20 servers that I want to iterate over with my playbook.
$ ansible-inventory -i inventory/production/ --graph | grep srv
| | |--srv01.mydomain.co.uk
| | |--srv02.mydomain.co.uk
| | |--srv03.mydomain.co.uk
| | |--srv04.mydomain.co.uk
| | |--srv05.mydomain.co.uk
| | |--srv06.mydomain.co.uk
| | |--srv07.mydomain.co.uk
| | |--srv08.mydomain.co.uk
| | |--srv09.mydomain.co.uk
| | |--srv10.mydomain.co.uk
| | |--srv11.mydomain.co.uk
| | |--srv12.mydomain.co.uk
| | |--srv13.mydomain.co.uk
| | |--srv14.mydomain.co.uk
| | |--srv15.mydomain.co.uk
| | |--srv16.mydomain.co.uk
| | |--srv17.mydomain.co.uk
| | |--srv18.mydomain.co.uk
| | |--srv19.mydomain.co.uk
| | |--srv20.mydomain.co.uk
Looking at various places, including How do i loop over hosts identified by wildcards in Ansible, my belief is that if I have the following playbook:
- name: test iteration over names
hosts: "{{ HOSTS }}"
gather_facts: False
tasks:
- ping:
with_items: "{{ ansible_play_hosts }}"
It should ping each host once. The following command should ping 2 hosts:
$ ansible-playbook --inventory inventory/production iteration.yaml --extra-vars "HOSTS=srv?5.mydomain.co.uk "
PLAY [test iteration over names] *************************************************************************************************************************************************************************
TASK [ping] **********************************************************************************************************************************************************************************************
ok: [srv05.mydomain.co.uk] => (item=srv05.mydomain.co.uk)
ok: [srv05.mydomain.co.uk] => (item=srv15.mydomain.co.uk)
ok: [srv15.mydomain.co.uk] => (item=srv05.mydomain.co.uk)
ok: [srv15.mydomain.co.uk] => (item=srv15.mydomain.co.uk)
PLAY RECAP ***********************************************************************************************************************************************************************************************
srv05.mydomain.co.uk : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
srv15.mydomain.co.uk : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
the play recap lists the anticipated number of hosts, but it pings each host the same number of times as the number of hosts specified by the wildcard.
HOSTS=srv15.mydomain.co.uk
would ping one host once
HOSTS=srv?5.mydomain.co.uk
pings 2 hosts (srv05
and srv15
) twice
HOSTS=srv1?.mydomain.co.uk
pings 10 hosts (srv10
to srv19
) 10 times
etc.
Why is this and how do I perform the task once per host?