可以从清单文件获取主机的公共IP

问题描述 投票:0回答:1

我想获取清单文件中定义的主机的公共IP

[test-sites:children]
test-site-1

[test-site-1]
test-site-1-2 ipv4=192.168.0.1 ipv6=....

如何获取清单文件中定义的“ test-site-1-2”的ipv4和ipv6地址?我已经检查了this答案,但它给出了所有地址(公共和私有)。我只对清单文件中定义的ip感兴趣。

ansible ansible-inventory
1个回答
1
投票
[test-site-1]
test-site-1-2 ipv4=192.168.0.1 ipv6=....

问:“如何获取清单文件中定义的“ test-site-1-2”的ipv4和ipv6地址?“

A:如果剧本在“ test-site-1-2”上运行,只需直接引用变量即可。例如

- hosts: test-site-1-2
  tasks:
    - debug:
        var: ipv4
    - debug:
        var: ipv6

如果其他主机需要这些变量,则需要引用“ hostvars”。例如

- hosts: test-site-1
  tasks:
    - debug:
        var: hostvars['test-site-1-2'].ipv4
    - debug:
        var: hostvars['test-site-1-2'].ipv6

请参见Basic inventory

© www.soinside.com 2019 - 2024. All rights reserved.