Get the hostname from the hosts file

2019-02-28 09:10发布

问题:

I would like to get another host from one of my templates.

Given the following hosts file:

[vm]
vm_hostname

[monitoring]
monitoring_hostname

How can I access monitoring_hostname from a template?

I tried:

monitor: {{ hostvars['monitoring'][0] }}

NB: I can't just use monitor: monitoring_hostname because my hosts file is dynamically generated.

回答1:

You want to use groups magic variable, not hostvars:

monitor: "{{ groups['monitoring'][0] }}"


标签: ansible