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:
- 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)
- 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.
- 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:
- How to get 'some_data' for every host?
- 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