1.ansible常用模块有:
- ping
- yum
- template
- copy
- user
- group
- service
- raw
- command
- shell
- script
ansible常用模块raw、command、shell的区别:
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
2. ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@ansible ansible]# ansible all -m ping
web.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3.ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
#查看受控机tmp目录下的文件
[root@ansible ansible]# ansible all -a 'ls /tmp'
web.example.com | CHANGED | rc=0 >>
ansible_command_payload_tdefdja5
systemd-private-eab7ffb825514f3ba217c051a7d97e02-chronyd.service-iOeHYz
# 在受控机tmp目录下创建一个hehe
[root@ansible ansible]# ansible all -a 'touch /tmp/hehe'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'. 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.
web.example.com | CHANGED | rc=0 >>
#查看是否创建成功
[root@ansible ansible]# ansible all -a 'ls /tmp'
web.example.com | CHANGED | rc=0 >>
ansible_command_payload_e6tmdo8q
hehe
systemd-private-eab7ffb825514f3ba217c051a7d97e02-chronyd.service-iOeHYz
#command模块不支持管道符
[root@ansible ansible]# ansible all -a "echo 'hello world' > /tmp/hehe"
web.example.com | CHANGED | rc=0 >>
hello world > /tmp/hehe
[root@ansible ansible]# ansible all -a 'cat /tmp/hehe'
web.example.com | CHANGED | rc=0 >>
#切换到受管机查看未发现 hehe文件内有内容
[root@web ~]# cat /tmp/hehe
[root@web ~]#
#不支持重定向
[root@ansible ansible]# ansible all -a 'ps -ef|grep vsftpd'
web.example.com | FAILED | rc=1 >>
erro