1

Using Ansible 2.5.0.

Question: How do I dynamically access the host variables of the current hosts in play, during runtime execution.

Inventory file:

[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8

[group_two]
host-3 ansible_host=2.4.6.8

My current task:

- hosts: group_one
  tasks:
    - name: set fact for all ip's in play execution
      set_fact:
        all_ips: "{{ groups['all'] | map('extract', hostvars, ['ansible_host'] ) | join(',') }}"

    # outcome = 1.2.3.4,5.6.7.8,2.4.6.8

This outcome is undesired. During execution, only host-1 and 2 are used, but the IP of host-3 is added as well.

Now, I've looked at the magic variables of Ansible, but it seems there is no variable which could be used in a way that it satisfy my needs.

Because I want the variable during runtime, I do not want to set the variable "{{ groups['group_one'] }}" as this would defeat the purpose.

How do I configure Ansible in such a way, the outcome would be 1.2.3.4,5.6.7.8, without statically configuring any variables?

greg-449
  • 109,219
  • 232
  • 102
  • 145
Kevin C
  • 4,851
  • 8
  • 30
  • 64
  • Have you considered looping over `ansible_play_hosts`? "ansible_play_hosts is the full list of all hosts still active in the current play." – chash Jul 17 '20 at 21:09
  • How would one write such a configuration? Please test and provide an answer if it is the desired outcomd – Kevin C Jul 17 '20 at 21:20

1 Answers1

2

You can use the ansible_play_hosts magic variable as a dynamic reference to the current hosts in the case that you do not want to use group_one.

ansible_play_hosts is the full list of all hosts still active in the current play.

inventory.ini

[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8

[group_two]
host-3 ansible_host=2.4.6.8

foo.yaml

- hosts: group_one
  gather_facts: no

  tasks:
    - set_fact:
        all_ips: "{{ ansible_play_hosts | map('extract', hostvars, ['ansible_host']) | join(',') }}"
    - debug:
        msg: "{{ all_ips }}"
      run_once: yes
$ ansible-playbook -i inventory.ini foo.yaml 

PLAY [group_one] *****************************************************************************************************************************************************************************************************************************

TASK [set_fact] ******************************************************************************************************************************************************************************************************************************
ok: [host-1]
ok: [host-2]

TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [host-1] => {
    "msg": "1.2.3.4,5.6.7.8"
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
host-1                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host-2                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
chash
  • 3,975
  • 13
  • 29
  • Wauw, I didn't knew that would work, since the output of "ansible_play_hosts" is simply the names of the hosts, and does not include any variables... excellent! – Kevin C Jul 17 '20 at 21:43