You can see that I access the same facts in debug and in set_fact. The first task works perfectly but the second one fails.
fatal: [host1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'. 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'\n\nThe error appears to be in '/home/raspbeguy/repo/infra/ansible/roles/myrole/tasks/main.yml': line 10, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Register private IP addresses of the cluster\n ^ here\n"}
before trying with set_fact I was trying to use it in a template, didn’t work either.
I’m using the last version of ansible available on pip:
What do you mean by running it in a loop? gather_facts doesn’t take any host argument (I guess it gather facts from all hosts) so I don’t see how and why running it in a loop. Either way, ansible gather facts already at the playbook start, and even after I added this task manually it won’t work either.
@bcoca@chris that works, thanks ! I had to add delegate_facts: true for it to work.
However, it means that each host has to initiate fact gathering about every other hosts targeted by the playbook. This makes a complexity of n^2, which is kind of problematic…
The group var cluster_name is here to let the hosts know what ansible group represent their cluster.
Is there a way to limit fact gathering for each host to the hosts that are in the same cluster? In the example I wish that host-a-1 gathers facts only for the 3 hosts in its cluster, and not the 6 total hosts when I run the playbook without limit.
This is already what I have.
Actually I’m not sure, because for now I have only one cluster, but I think the loop though groups[cluster_name] will just tell each hosts in group cluster_name to gather facts about all hosts in the inventory, it won’t limit the taget of the gathering.
Also I have no idea what will happen when there will be more than one cluster, so with multiple values of cluster_name. Will there be as many loops as there are cluster_name values?
I’ll setup a second test cluster next week to find out.