ansible无法获得inventory_hostname

Sim*_*eth 5 jinja2 ansible ansible-playbook

我正在尝试获取正在处理的服务器的简称.

我在jinja2中有这个:

ServerAlias graphite.{{ hostvars[inventory_hostname] }}
ServerAlias graphite.{{ hostvars[inventory_hostname] }}.{{dc}}.{{subnet}}
Run Code Online (Sandbox Code Playgroud)

上面只是溢出了整个事实,而不仅仅是短名称.

这就是hosts.yaml的样子:

graphite.experimental.com dc=lv1 subnet=coupons.lan
Run Code Online (Sandbox Code Playgroud)

Bru*_*e P 14

你想要使用的只是{{ inventory_hostname }}(或{{ inventory_hostname_short }}简称).

hostvars对象是一种访问Ansible知道的每个主机的变量的方法.因此,hostvars[inventory_hostname]将为您提供包含有关当前主机的所有已知事实的对象,hostvars['foo']将为您提供包含有关主机'foo'等所有已知事实的对象.

假设您有一组名为"db_servers"的主机,并且您希望生成模板中所有这些主机的IP地址列表.这是你如何做到这一点:

{% for host in groups['db_servers'] %}
   {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)