Ansible常用模块
1.ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
查看受控主机的/tmp目录内容
[root@localhost ansible]# ansible 192.168.197.135 -a 'ls /tmp'
192.168.197.135 | CHANGED | rc=0 >>
anaconda.log
ansible_command_payload_nyl6ebha
dbus.log
ifcfg.log
ks-script-eqcj5uwm
packaging.log
program.log
sensitive-info.log
storage.log
systemd-private-5aaad234b0114baeb9b7cced185df64a-colord.service-z7MHDH
systemd-private-5aaad234b0114baeb9b7cced185df64a-ModemManager.service-UfFF4T
systemd-private-5aaad234b0114baeb9b7cced185df64a-rtkit-daemon.service-gn3Rg3
tracker-extract-files.0
vmware-root_1002-2957518059
vmware-root_1013-4290232108
vmware-root_1017-4248221863
vmware-root_995-4257069371
在受控主机的/tmp目录下新建一个文件test
[root@localhost ansible]# ansible 192.168.197.135 -a 'touch /tmp/test'
[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.
192.168.197.135 | CHANGED | rc=0 >>
chird
[root@localhost ansible]# ansible 192.168.197.135 -m command -a "chdir=/abc ls -l"
192.168.197.135 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x. 2 root root 6 7月 19 03:48 12
2.ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
[root@localhost ansible]# ansible 192.168.197.135 -m raw -a 'echo "hello world" > /tmp/test'
192.168.197.135 | CHANGED | rc=0 >>
Shared connection to 192.168.197.135 closed.
[root@localhost ansible]# ansible 192.168.197.135 -a 'cat /tmp/test'
192.168.197.135 | CHANGED | rc=0 >>
hello world
支持管道符
[root@localhost ansible]# ansible 192.168.197.135 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.197.135 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.197.135 closed.
3.ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
[root@master ansible]# ansible 192.168.197.135 -m shell -a "yum list | grep httpd"
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command
because yum is insufficient you can