3

I've faced an unexpected problem with Ansible. Here's simplified example.

I have defined some global variables in groups_vars/all file like this:

---
node01: {ipv4_address: '10.10.10.1', some_info: data}

And use it like this in inventory file:

[physical-hosts]
phyn01 node="{{ node01 }}" ansible_ssh_host="{{ node.ipv4_address }}"

The funny part is that Ansible can ssh on each of the hosts and get the facts. But I can not get the value of 'node' variable for each host when executing playbook (I have additional data there).

The working example:

- hosts: physical-hosts
  tasks:
    - name: get node variable for current host
      debug: var=node

The output is in this case:

TASK: [get node variable for current host] ************************************
ok: [phyn01] => {
    "node": {
        "ipv4_address": "10.10.10.1",
        "some_info": "data"
    }
}

But I can't get the same if I use the following:

- hosts: physical-hosts
  tasks:
  - debug: var=hostvars.{{item}}.node
    with_items: groups['physical-hosts']

It reports as a wrong answer the following:

TASK: [debug var=hostvars.{{item}}.node] **************************************
ok: [phyn01] => (item=phyn01) => {
    "hostvars.phyn01.node": "{{ node01 }}",
    "item": "phyn01"
}

Summary:

  1. I need to access 'some_data' for each host in a group without redefining the same variables once more for each host individually (lots of code duplication => lots of errors)
  2. As you can see from example they way I want it to work seems to be clear. And it work when we are connecting to the host (ansible_ssh_host resolves correctly) and individual 'var=node' also resolves correctly. Facts are delivered of course.
  3. This way doesn't work only when I try to get this data for the whole group and it seems that I'm using some wrong syntax.

So the questions are:

  1. How to get 'some_data' for every host?
  2. How to define host=hostN in a correct way? I need to use the same constructions as node.some_data for each host and I must define ansible_ssh_host each time because the same hosts may be in different groups (with different data).

Thanks for attention

upd: I was writing from memory, so there were bunch of mistypes. Now the output and typos are real and fixed

1 Answers1

0

for point 3, or your first question: In order to gain access to hosts data in a group, you can use hostvars, you first have to create a task for all hosts or the group in interest. for example an internal role that simple do trivial thing on all hosts. This role then forces gather facts on all hosts and add each to the hostvars group. remember to enable gather facts and not to limit your playbook run or tagging that misses this role/task. also if your vars are not simple vars like lists or dicts its better to put them host_vars files.

It has been discussed before in the list: https://groups.google.com/forum/#!msg/Ansible-project/f90Y4T4SJfQ/L1YomumcPEQJ

didn't understand your second question, your inventory is working just fine.

Walid
  • 857
  • 11
  • 15