Ansible 笔记 (2) - Ad-hoc 命令
2017-03-05 18:57
260 查看
先使用 ansible-doc 获取帮助文档
命令格式 Usage: ansible <host-pattern> [options]
显示 hosts
指定 inventory(hosts) 执行命令,使用 ansible -m 指定模块 -a 指定参数 -o 单行输出
-m 默认模块是 command,这里可以忽略。同时我们可以自己编写脚本指定“动态 inventory”,具体的写法请自行百度。这个脚本需要支持两个参数, --list/-l 和 --host/-H,并且返回 json 格式的数据。
需要注意的是如果“被控机”开启了 selinux,则在使用 copy 模块时需要在“被控机”上安装 libselinux-python,才能保证正常执行 copy 模块
[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 模块
相关文章推荐
- Ansible4:Ad-hoc与命令执行模块
- Ansible常用Ad-Hoc命令介绍
- 【Ansible 文档】【译文】Ad-Hoc 命令介绍
- Ansible之四:Ad-hoc与命令执行模块
- ansible小结(六)Ad-hoc与commands模块
- Ansible 系列之 Ad-Hoc介绍及使用
- 自动化运维之ansible的ad-hoc
- ansible的ad-hoc模式
- ansible 基本概念,ad-hoc操作
- ansible 基本概念,ad-hoc操作
- Ansible Ad-Hoc,facts,role,Galaxy简介
- Ansible 系列之 Ad-Hoc介绍及使用
- Ansible 系列之 Ad-Hoc介绍及使用
- java分布式系统部署学习(六)ansible Ad-hoc与commands模块
- 自动化工具ansible基本命令和使用方式学习笔记
- Ansible4:Ad-hoc与命令执行模块
- Ansible 系列之 Ad-Hoc介绍及使用
- ansible 执行动作(ad-hoc和playbook)两种方式
- Ansible4:Ad-hoc与命令执行模块
- ansible 基本概念,ad-hoc操作