Ansible中ad-hoc命令格式如下:ansible <pattern_goes_here> -m <module_name> -a <arguments>,Patterns用来指定目标主机,对于Patterns的用法如下
inventory文件如下:
[test]
192.168.144.129
192.168.144.130
[test1]
192.168.144.129
[test2]
192.168.144.130
1、all代表inventory文件中的所有主机
[root@client01 ansible]# ansible all -m ping
192.168.144.130 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
2、IP或IP通配主机名
[root@client01 ansible]# ansible 192.168.144.129 -m ping
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
[root@client01 ansible]#
[root@client01 ansible]# ansible 192.168.144.* -m ping
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.144.130 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
3、一个组
[root@client01 ansible]# ansible test -m ping
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.144.130 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
4、多个组同时操作
[root@client01 ansible]# ansible test1:test2 -m ping
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.144.130 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
5、在组中选择对应编号的主机
[root@client01 ansible]# ansible test[0] -m ping
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
6、主机和组同时操作
[root@client01 ansible]# ansible 192.168.144.129:test2 -m ping
192.168.144.130 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}
192.168.144.129 | SUCCESS => {
"changed": false,
"failed": false,
"ping": "pong"
}