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 719 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 
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值