【发布时间】:2021-01-26 22:55:51
【问题描述】:
我正在尝试在 ansible 剧本中迭代主机变量,它似乎对几个主机变量都工作得很好,除了我需要的一个:ansible_host
如果我尝试:
{% for host in groups['all'] %}
server {{ host }} {{ hostvars[host] }}
{% endfor %}
它为每个服务器输出: 服务器 server2 {'ansible_verbosity': 0,, '...'ansible_check_mode': False, 'ansible_run_tags': [u'all'], 'ansible_skip_tags': [], u'ansible_host': u'10.192.11.17', 'ansible_version': {'major': 2, 'full': '2.9.11', 'string': '2.9.11', 'minor': 9, 'revision': 11}}
包含所有变量。
现在如果我尝试
{% for host in groups['all'] %}
server {{ host }} {{ hostvars[host]['verbosity'] }}
{% endfor %}
它会让我得到 ansible_verbosity 就好了。但我找不到获取 ansible_host 的方法
{% for host in groups['all'] %}
server {{ host }} {{ hostvars[host]['ansible_host'] }}
{% endfor %}
我认为问题可能与 ansible_host 是一个 unicode var ( u'ansible_host' ) 的事实有关。我这么说是因为其他 2 个 unicode vars 因同样的问题而失败,而其他所有的 unicode vars 都可以正常工作。
知道如何获取该变量吗?
【问题讨论】:
-
用例是什么?
ansible_host应始终包含在hostvars中是否有任何实际原因? -
我被告知这些服务器不能在 DNS 上有条目。不是我的电话。