【问题标题】:Ansible getting public ip of host from inventory fileAnsible 从清单文件中获取主机的公共 IP
【发布时间】:2020-04-07 14:59:06
【问题描述】:

我想获取清单文件中定义的主机的公共 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 的答案,但它提供了所有地址(公共和私人)。我只对库存文件中定义的 ips 感兴趣。

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:
    [test-site-1]
    test-site-1-2 ipv4=192.168.0.1 ipv6=....
    

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

    答:如果 playbook 在“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

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2019-03-13
      • 2019-10-06
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多