Ansible cannot get inventory_hostname

I am trying to get the short name of a working server.

I have this in jinja2:

ServerAlias graphite.{{ hostvars[inventory_hostname] }} ServerAlias graphite.{{ hostvars[inventory_hostname] }}.{{dc}}.{{subnet}} 

The whole globe of facts spills above all, not just a short name.

Here is what hosts.yaml looks like:

 graphite.experimental.com dc=lv1 subnet=coupons.lan 
+5
source share
1 answer

What you want to use is simply {{ inventory_hostname }} (or {{ inventory_hostname_short }} for the short name).

The hostvars object is a way to access the variables of every host that Ansible knows about. Thus, hostvars[inventory_hostname] will provide you with an object containing all known facts about the current host, hostvars['foo'] will provide you with an object containing all known facts about the host "foo", etc.

Suppose you have a host group called "db_servers" and you want to create a list of the IP addresses of all these hosts in the template. Here's how you do it:

 {% for host in groups['db_servers'] %} {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }} {% endfor %} 
+11
source

Source: https://habr.com/ru/post/1214041/


All Articles