I want to generate a file which is the same on two hosts and contains both host names, e.g.:
resource r0 {
on host01 {
device /dev/drbd0;
}
on host02 {
device /dev/drbd0;
}
}
I tried to achieve this with a template such as the following:
resource r0 {
on {{ hostvars[groups['hosts'][0]]['ansible_nodename'] }} {
device /dev/drbd0;
}
on {{ hostvars[groups['hosts'][1]]['ansible_nodename'] }} {
device /dev/drbd0;
}
}
This works for the first variable, but the list doesn't have a second element, so the second variable is unknown.
How do I get a list of the hosts so that I can select each host individually from the list?
Edit: The relevant bit of the inventory is
[hosts]
host01
host02
Edit:
My answer below works if I just need the host name. However, I also need the IP address of a certain interface.
How do I access something like the information in hostvars
but for a host
other than the current one?