跳到主要内容

ansible模块-命令模块

ansible查看帮助方法

# 查看所有模块帮助信息
ansible-doc -l

# 指定查看某个模块参数用法
ansible-doc copy

1.command模块 不支持管道 不支持通配符 默认模块

# 默认模块,执行命令,以下两个命令本质相同 
ansible all -m command -a "hostname"
ansible all -a hostname

command模块不支持管道

$ ansible all -m command -a "ifconfig|grep eth0"
devops01 | FAILED | rc=2 >>
[Errno 2] No such file or directory
devops02 | FAILED | rc=2 >>
[Errno 2] No such file or directory
devops03 | FAILED | rc=2 >>
[Errno 2] No such file or directory

2.shell模块 支持管道 支持通配符

$ ansible all -m shell -a "ifconfig|grep eth0"
devops01 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
devops02 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
devops03 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

3.raw/branch模块 支持管道 支持通配符

$ ansible all -m raw/branch -a "ifconfig|grep eth0" 
devops01 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
Shared connection to devops01 closed.

devops03 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
Shared connection to devops03 closed.

devops02 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
Shared connection to devops02 closed.
Right Bottom Gif
Right Top GIF