Bootstrap FreeKB - Ansible - Delegating facts variables list dictionaries using hostvars
Ansible - Delegating facts variables list dictionaries using hostvars

Updated:   |  Ansible articles

By default, facts, variables, lists and dictionaries are created on the target servers. In this example, the "foo" variable will be created on each target server.

---
- hosts: all
  tasks:
  - set_fact:
      foo: bar

  - debug:
      var: foo
...

 

Running this playbook should return the following, which shows that the "foo" variable was created on the target server (server1.example.com in this example).

TASK [set_fact] 
ok: [server1.example.com]

TASK [debug] 
ok: [server1.example.com] => {
    "foo": "bar"
}

 

hostvars can be used to create and/or access a fact, variable, list, or dictionary on some other target server. Most often, this is done on localhost. Let's say you create the "foo" variable on server1.example.com and then try using the debug tasks to output the "foo" variable on localhost.

---
- hosts: all
  tasks:
  - set_fact:
      foo: bar

  - debug:
      var: foo

  - debug:
      msg: "{{ hostvars['localhost']['foo'] }}"
...

 

This should return a fatal error like this since the "foo" variable was not created on localhost.

fatal: [server1.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'foo'\n\nThe error appears to be in 'testing.yml': line 10, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - debug:\n    ^ here\n"}

 

delegate_to and delegate_facts can be used to create the fact, variable, list, or dictionary on some other server, usually localhost.

---
- hosts: all
  tasks:
  - set_fact:
      foo: bar
    delegate_to: localhost
    delegate_facts: true

  - debug:
      msg: "{{ hostvars['localhost']['foo'] }}"
...

 

Which should now return the following, which show that the "foo" variable is available on localhost.

TASK [set_fact] 
ok: [server1.example.com -> localhost]

TASK [debug] 
ok: [server1.example.com] => {
    "msg": "bar"
}

 


Sometimes there is a scenario where you will want a managed node in your inventory to get a fact about the other managed nodes in your inventory. For example, the ansible_default_ipv4.address fact will display each managed nodes IP address. 

---
- hosts: all
  tasks:
  - name: display IP address
    debug:
      msg: "{{ ansible_default_ipv4.address }}"
...

 

The following should be returned. Notice the IP addresses are different, because the IP address of each managed node is being returned.

ok: [server1.example.com] => {
    "msg": "192.168.0.7"
}
ok: [server2.example.com] => {
    "msg": "192.168.0.11"
}
ok: [server3.example.com] => {
    "msg": "192.168.0.15"
}

 

hostvars can be used to get the fact of some other managed node in your inventory. Since hostvars displays facts, the gather_facts module must be set to true, which is the default setting of the gather_facts module. In other words, the gather_facts module must not be set to false.

---
- hosts: all
  tasks:
    - name: display server2 IP address
      debug:
        msg: "{{ hostvars['server2.example.com']['ansible_default_ipv4']['address'] }}"
...

 

Something like this should be returned. Notice that 192.168.0.11 is being returned, which is server2 IP address.

TASK [display server2 IP address] 
ok: [server1.example.com] => {
    "msg": "192.168.0.11"
}
ok: [server2.example.com] => {
    "msg": "192.168.0.11"
}
ok: [server3.example.com] => {
    "msg": "192.168.0.11"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 94ffa9 in the box below so that we can be sure you are a human.