1、ping模块
ping模块是用来检查控制节点和受控节点是否畅通,pong表示为通
[root@localhost .ssh]# ansible all -m ping
192.168.35.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2、command、raw、shell模块
Command、raw、shell都是远程主机上执行的命令,都支持在受控主机上执行受控主机上的脚本,但command是系统默认模块,且不能使用管道符、重定向。而raw和shell是支持管道服务和重定向的。在日常中尽量少用raw和shell模块,使用默认的command模块。三者都不具备幂等性
幂等性:(执行一次或多次结果是相同的,如果这一步做过了直接做下一步)
2.1 command模块
//在/home/目录下创建一个名为lry的文件
[root@localhost ansible]# ansible all -a"touch /home/lry"
192.168.35.136 | CHANGED | rc=0 >>
[root@lry ~]# ll /home/ | grep lry
-rw-r--r-- 1 root root 0 7月 18 15:17 lry
//查看/home/目录中的所有文件
[root@localhost ansible]# ansible all -a "ls /home/"
192.168.35.136 | CHANGED | rc=0 >>
123
123
123
abc
// command不支持管道符和重定向
[root@localhost ansible]# ansible all -a 'ps -ef|grep vsftpd'
192.168.35.136 | FAILED | rc=1 >>
error: unsupported SysV option
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).non-zero return code
2.2 raw模块
执行脚本文件
[root@localhost ansible]# ansible all -m raw -a"/home/www.sh"
192.168.35.136 | CHANGED | rc=0 >>
hello world
Shared connection to 192.168.35.136 closed.
支持管道符和重定向
[root@localhost ansible]# ansible all -m raw -a "ls /home | grep www.sh"
192.168.35.136 | CHANGED | rc=0 >>
www.sh
Shared connection to 192.168.35.136 closed.
[root@localhost ansible]# ansible all -m raw -a"echo 'lry' > /home/123"
192.168.35.136 | CHANGED | rc=0 >>
Shared connection to 192.168.35.136 closed.
[root@lry home]# cat 123
lry
2.3 shell模块
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。shell模块亦支持管道与重定向。
在受控主机上执行脚本文件
[root@localhost ansible]# ansible all -m shell -a"/home/www.sh"
192.168.35.136 | CHANGED | rc=0 >>
hello world
支持管道符和重定向
[root@localhost ansible]# ansible all -m shell -a"echo 'lry123' > /home/456"
192.168.35.136 | CHANGED | rc=0 >>
3、script模块
script模块用于在受控机上执行主控机上的脚本
[root@localhost ansible]# vim script.sh
[root@localhost ansible]# chmod +x script.sh
[root@localhost ansible]# ll
总用量 32
-rw-r--r-- 1 root root 20025 7月 17 10:58 ansible.cfg
-rw-r--r-- 1 root root 1016 6月 23 08:12 hosts
-rw-r--r-- 1 root root 18 7月 18 14:41 inventory
drwxr-xr-x 2 root root 6 7月 16 11:32 mulu
drwxr-xr-x 2 root root 6 6月 23 08:12 roles
-rwxr-xr-x 1 root root 19 7月 18 16:13 script.sh
[root@localhost ansible]# ansible all -m script -a"/etc/ansible/script.sh" //在受控制主机上执行控制节点的脚本
192.168.35.