您的位置:首页 > 其它

Ansible 笔记 (2) - Ad-hoc 命令

2017-03-05 18:57 260 查看
先使用 ansible-doc 获取帮助文档

[root@localhost ~]# ansible-doc ping
> PING    (/usr/lib/python2.7/site-packages/ansible-2.3.0-py2.7.egg/ansible/modules/system/ping.py)

A trivial test module, this module always returns `pong' on successful contact. It does not make sense in
playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable python is
configured. This is NOT ICMP ping, this is just a trivial test module.

EXAMPLES:
# Test we can logon to 'webservers' and execute python with json lib.
ansible webservers -m ping

MAINTAINERS: Ansible Core Team, Michael DeHaan

METADATA:
Status: ['stableinterface']
Version: 1.0
Supported_by: core


命令格式 Usage: ansible <host-pattern> [options]

显示 hosts

[root@localhost ~]# ansible all --list-hosts
hosts (2):
192.168.34.129
192.168.34.130
[root@localhost ~]# ansible webserver --list-hosts
hosts (2):
192.168.34.129
192.168.34.130
[root@localhost ~]# ansible 192.168.34.129:192.168.34.130 --list-hosts
hosts (2):
192.168.34.129
192.168.34.130


指定 inventory(hosts) 执行命令,使用 ansible -m 指定模块 -a 指定参数 -o 单行输出

[root@localhost ~]# ansible  all -m ping
192.168.34.130 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.34.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@localhost ~]# ansible  webserver  -m ping
192.168.34.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.34.130 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@localhost ~]# ansible  192.168.34.129  -m ping
192.168.34.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@localhost ~]# ansible  192.168.34.129:192.168.34.130  -m ping
192.168.34.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.34.130 | SUCCESS => {
"changed": false,
"ping": "pong"
}


[root@localhost ~]# ansible  192.168.34.129:192.168.34.130  -m command -a uptime -o
192.168.34.129 | SUCCESS | rc=0 | (stdout)  05:08:05 up  1:53,  2 users,  load average: 0.08, 0.13, 0.13
192.168.34.130 | SUCCESS | rc=0 | (stdout)  05:08:04 up  1:54,  2 users,  load average: 0.14, 0.10, 0.09


-m 默认模块是 command,这里可以忽略。同时我们可以自己编写脚本指定“动态 inventory”,具体的写法请自行百度。这个脚本需要支持两个参数, --list/-l 和 --host/-H,并且返回 json 格式的数据。

需要注意的是如果“被控机”开启了 selinux,则在使用 copy 模块时需要在“被控机”上安装 libselinux-python,才能保证正常执行 copy 模块
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: