模块相关命令
(1)列出 ansible 支持的模块
ansible-doc -l
(2)查看该模块的帮助信息
ansible-doc ping # 查看 ping 模块的帮助信息
以下实验均是在 IP 地址为: 192.168.91.128 的虚拟机上操作的。
/etc/ansible/hosts 文件中配置如下:
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
[Client]
192.168.91.130
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
## db-[99:101]-node.example.com
常用模块示例
ping 模块
检查指定节点机器能否连通,如果主机在线,则回复pong。
例如:
[root@xdhuxc ~]# ansible Client -m ping
192.168.91.130 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@xdhuxc ~]#
远程命令模块(command、script、shell)
command 作为 ansible 的默认模块,可以运行远程权限范围所有的 shell 命令,不支持管道符。
例如:
ansible Client -m command -a "free -h" # 查看 Client 分组主机内存使用情况。
执行结果为:
[root@xdhuxc ~]# ansible Client -m command -a "free -h"
192.168.91.130 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 976M 153M 599M 6.7M 223M 638M
Swap: 2.0G 0B 2.0G
script 的功能是在远程主机执行主控端存储的 shell 脚本文件,相当于 scp + shell 组合。
例如:
ansible Client -m script -a "/root/xdhuxc.sh string buffer" # 远程执行本地脚本。
xdhuxc.sh 脚本的内容为:
[root@xdhuxc ~]# pwd
/root
[root@xdhuxc ~]# ls
xdhuxc.sh
[root@xdhuxc ~]# cat xdhuxc.sh
#!/usr/bin/env bash
echo "Hello Shell."
echo $1
echo $2 >> /root/abc.sh
执行结果为:
[root@xdhuxc ~]# ansible Client -m script -a "/root/xdhuxc.sh string buffer"
192.168.91.130 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.91.130 closed.\r\n",
"stdout": "Hello Shell.\r\nstring\r\n",
"stdout_lines": [
"Hello Shell.",
"string"
]
}
在 192.168.91.130上,在 root 目录下,会产生 abc.sh 文件。
[root@xdhuxc ~]# pwd
/root
[root@xdhuxc ~]# ls
abc.sh anaconda-ks.cfg ~None
[root@xdhuxc ~]# cat abc.sh
buffer
[root@xdhuxc ~]#
~None 目录下
[root@xdhuxc ~None]# pwd
/root/~None
[root@xdhuxc ~None]# tree -a
.
└── .ansible
└── tmp
2 directories, 0 files
shell 的功能是执行远程主机上的 shell 脚本文件,支持管道符。
例如:
ansible Client -m shell -a "/root/xdhuxc.sh" # 执行远程机器上的脚本。
在 root 目录下创建 xdhuxc.sh 文件,
[root@xdhuxc ~]# pwd
/root
[root@xdhuxc ~]# ll
total 12
-rw-r--r-- 1 root root 7 Jun 11 22:19 abc.sh
-rw-------. 1 root root 1424 Apr 9 21:56 anaconda-ks.cfg
drwx------ 3 root root 22 Jun 10 11:55 ~None
-rw-r--r-- 1 root root 43 Jun 11 22:34 xdhuxc.sh # xdhuxc.sh 没有执行权限。
[root@xdhuxc ~]# cat xdhuxc.sh
#!/usr/bin/env bash
echo -n "Hello World!"
在 xdhuxc.sh 没有被赋予执行权限时,执行 ansible 命令。
[root@xdhuxc ~]# ansible Client -m shell -a "/root/xdhuxc.sh"
192.168.91.130 | FAILED | rc=126 >>
/bin/sh: /root/xdhuxc.sh: Permission deniednon-zero return code
很明显,xdhuxc.sh 没有可执行权限导致的,修改该命令,重新执行,命令执行成功。
[root@xdhuxc ~]# ansible Client -m shell -a "chmod +x /root/xdhuxc.sh ; /root/xdhuxc.sh"
[WARNING]: Consider using the file module with mode rather than running chmod. If you need to use command because
file is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to
get rid of this message.
192.168.91.130 | SUCCESS | rc=0 >>
Hello World!
copy 模块
copy 模块用于实现主控端向目标机器复制文件,类似于 scp 功能。
例如:
ansible Client -m copy -a "src=/root/xdhuxc.sh dest=/tmp/ owner=root group=root mode=0755" # 向 Client 组中主机复制 xdhuxc.sh 到 /tmp 目录下,文件属主、属组都为 root,权限为:0755
执行结果如下:
[root@xdhuxc ~]# pwd
/root
[root@xdhuxc ~]# ls
xdhuxc.sh
[root@xdhuxc ~]# ansible Client -m copy -a "src=/root/xdhuxc.sh dest=/tmp/ owner=root group=root mode=0755"
192.168.91.130 | SUCCESS => {
"changed": true,
"checksum": "406eb4b32e5fe321308cfd40365f765685d64d58",
"dest": "/tmp/xdhuxc.sh",
"gid": 0,
"group": "root",
"md5sum": "1abf4cb799e8858ab96bb7035837f248",
"mode": "0755",
"owner": "root",
"size": 72,
"src": "~None/.ansible/tmp/ansible-tmp-1528728245.0-152168028566440/source",
"state": "file",
"uid": 0
}
在 192.168.91.130 机器上
[root@xdhuxc ~None]# pwd
/root/~None
[root@xdhuxc ~None]# tree -a
.
└── .ansible
└── tmp
└── ansible-tmp-1528728229.03-131422320501562
3 directories, 0 files
[root@xdhuxc ~None]# ls /tmp
xdhuxc.sh
stat 模块
stat 模块用于获取远程文件状态信息,atime/ctime/mtime/md5/uid/gid等信息。
例如:
ansible Client -m stat -a "path=/etc/sysctl.conf"
执行结果如下:
[root@xdhuxc ~]# ansible Client -m stat -a "path=/etc/resolv.conf"
192.168.91.130 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1528725838.9349997,
"attr_flags": "",