Search code examples
dictionaryansiblehostvars

Ansible - dynamically access hostvars with current hosts in play during runtime


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?


Solution

  • 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