2

I am running an Ansible play which uses the serial property to run against a subset of the hosts at a time.

I would like to get the current serial counter so I can use this to dynamically set a group name in an add_host task.

For example my hosts file is as follows:

"172.23.130.13",
"172.23.130.34",
"172.23.130.99",
"172.23.130.94"

My play is as follows:

- name: Create Add and Remove Group Batches
  gather_facts: false
  hosts: all
  become: true
  no_log: false
  serial: 2

  tasks:

    - add_host:
        name: "{{ item.1 }}"
        ansible_ssh_port: 2020
        group: loadbalancer-{{ <<serial counter here>> }}
        action: add
      with_indexed_items: "{{ ansible_play_batch  }}"
      delegate_to: localhost

This play will run twice as serial is set to 2. I would therefore like 2 groups to be created by the add_host task. One group called loadbalancer-1 and another called loadbalancer-2.

The group loadbalancer-1 will contain the first 2 IPs from the hosts file and the group loadbalancer-2 the next 2.

Is this possible?

Thanks for any help people can offer.

U880D
  • 8,601
  • 6
  • 24
  • 40
Jon Hunter
  • 882
  • 2
  • 8
  • 18
  • 1
    Would the [loop control](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html?highlight=loop%20control#loop-control) help? You could define the loop control variable and use that to group the hosts? – Bruce Becker Nov 06 '18 at 12:42
  • This question might be answered by the example of how to [controlling Ansible playbook execution](https://stackoverflow.com/a/75035186/6771046). – U880D Feb 14 '23 at 18:30

0 Answers0