ansible-playbook 获取主机ip(多ip)

2020-08-14  本文已影响0人  随便写点什么就好

说明

我们用ansible的facts变量获取系统的信息是最方便的. 但是我们的服务器有存在多ip的情况.自己看一下
ansible 192.168.10.106 -m setup|more

看输出需要获取什么值

 "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.10.106", 
            "192.168.10.69", 
            "192.168.10.218"
        ], 
------------------------------------------------------------
        "ansible_default_ipv4": {
            "address": "192.168.10.106", 
            "alias": "ens192",
----------------------------------------------------------
        "ansible_ens192": {
            "active": true, 
            "device": "ens192",
            "ipv4": {
                "address": "192.168.10.106", 
                "broadcast": "192.168.10.255", 
                "netmask": "255.255.255.0", 
                "network": "192.168.10.0"
            }, 
            "ipv4_secondaries": [
                {
                    "address": "192.168.10.69", 
                    "broadcast": "192.168.10.255", 
                    "netmask": "255.255.255.0", 
                    "network": "192.168.10.0"
                }, 
                {
                    "address": "192.168.10.218", 
                    "broadcast": "192.168.10.255", 
                    "netmask": "255.255.255.0", 
                    "network": "192.168.10.0"
                }

所以获取ip的值就用对应的就可以了
ansible_all_ipv4_addresses #适用于多ip, 输出[u'192.168.10.106', u'192.168.10.69', u'192.168.10.218'], 如果是docker几十个ip,也很烦.
ansible_default_ipv4[address] #适用单ip
ansible_ens192[ipv4][address] #因为和网卡绑定,多系统版本的时候不通用

上一篇下一篇

猜你喜欢

热点阅读