0

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?

loris
  • 450
  • 8
  • 20

1 Answers1

0

The following does what I want:

resource r0 {
  on {{ groups['hosts'][0] }} {
    device    /dev/drbd0;
  }
  on {{ groups['hosts'][1] }} {
    device    /dev/drbd0;
  }
}    

Edit:

To access variables of hosts other than the current one, such as IP addresses, the playbook must include the other hosts in a hosts instruction. I was testing with a playbook which only ran for a single host and so was unable to access information about the other host. You can still run a test like this, but you need a dummy action over the other hosts, as described here: https://serverfault.com/a/639269/144800, in order to collect the facts for the other hosts.

Community
  • 1
  • 1
loris
  • 450
  • 8
  • 20