ansible:获取ansible的主机信息

目录标题

1. 使用ansible_version获取ansible的版本信息

[root@server4 ~]# ansible testB -m debug -a "msg={ {ansible_version}}"
server3 | SUCCESS => { 
    "msg": { 
        "full": "2.9.6", 
        "major": 2, 
        "minor": 9, 
        "revision": 6, 
        "string": "2.9.6"
    }
}

2. 使用inventory_hostname获取主机信息

[root@server4 ~]# cat /etc/ansible/hosts
[test-group]
172.25.60.5
testB.westos.com ansible_host=172.25.60.3
testC ansible_host=172.25.60.6

[root@server4 ~]# ansible test-group -m debug -a "msg={ {inventory_hostname}}" # 获取主机信息
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to 
allow bad characters in group names by default, this will change, but still be 
user configurable on deprecation. This feature will be removed in version 2.10.
 Deprecation warnings can be disabled by setting deprecation_warnings=False in 
ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
172.25.60.5 | SUCCESS => { 
    "msg": "172.25.60.5"
}
testB.westos.com | SUCCESS => { 
    "msg": "testB.westos.com"
}
testC | SUCCESS => { 
    "msg": "testC"
}

3. 获取主机短信息:inventory_hostname_short

[root@server4 ~]# ansible test-group -m debug -a "msg={ {inventory_hostname_short}}"
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to 
allow bad characters in group names by default, this will change, but still be 
user configurable on deprecation. This feature will be removed in version 2.10.
 Deprecation warnings can be disabled by setting deprecation_warnings=False in 
ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
172.25.60.5 | SUCCESS => { 
    "msg": "172"
}
testB.westos.com | SUCCESS => { 
    "msg": "testB"
}
testC | SUCCESS => { 
    "msg": "testC"
}

4. 获取所有的groups清单信息

准备6台主机,并且免密(ssh-copy-id server2)
server4是ansible主机

[root@server4 ~]# vim /etc/ansible/hosts
172.25.60.2
testB.westos.com ansible_host=172.25.60.1
testC ansible_host=172.25.60.6

[testA]
test1 ansible_host=172.25.60.3
test2 ansible_host=172.25.60.5

[testB]
test3 ansible_host=172.25.60.4

[test:children]
testA
testB

[root@server4 ~]# ansible test1 -m debug -a "msg={ {groups}}"
test1 | SUCCESS => { 
    "msg": { 
        "all": [
            "172.25.60.2", 
            "testB.westos.com", 
            "testC", 
            "test1", 
            "test2", 
            "test3"
        ], 
        "test": [
            "test1", 
            "test2", 
            "test3"
        ], 
        "testA": [
            "test1", 
            "test2"
        ], 
        "testB": [
            "test3"
        ], 
        "ungrouped": [
            "172.25.60.2", 
            "testB.westos.com", 
            "testC"
        ]
    }
}

5. 获取其中一个groups的清单信息groups.testA

[root@server4 ~]# ansible test1 -m debug -a "msg={ {groups.testA}}"
test1 | SUCCESS => { 
    "msg": [
        "test1", 
        "test2"
    ]
}

6. 查看某台主机属于哪个分组group_names

[root@server4 ~]# ansible test1 -m debug -a "msg={ {group_names}}"
test1 | SUCCESS => { 
    "msg": [
        "test", 
        "testA"

7. 获取所有未分组的主机

[root@server4 ~]# ansible test1 -m debug -a "msg={ {groups.ungrouped}}"
test1 | SUCCESS => { 
    "msg": [
        "172.25.60.2", 
        "testB.westos.com", 
        "testC"
    ]
}

8. inventory_dir:获取默认清单的存放路径

[root@server4 ~]# ansible test2 -m debug -a "msg={ {inventory_dir}}"
test2 | SUCCESS => { 
    "msg": "/etc/ansible"
}
    原文作者:weixin_43384009
    原文地址: https://blog.csdn.net/weixin_43384009/article/details/105212267
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞