文章目录
- ansible常用模块简介
- ansible常用模块之ping-连通节点
- ansible常用模块之command-执行远程命令
- ansible常用模块之raw-执行远程命令
- ansible常用模块之shell-执行远程命令或本机脚本
- ansible常用模块之script-远程执行本机脚本
- ansible常用模块之template-生成并传输模板
- ansible常用模块之yum-管理软件
- ansible常用模块之copy-复制文件或文本
- ansible常用模块之group-添加删除用户组
- ansible常用模块之user-管理受控机用户帐号
- ansible常用模块之service-管理受控机上的服务
ansible常用模块简介
ansible常用模块有:
- ping
- command
- raw
- shell
- script
- template
- yum
- copy
- user
- group
- service
ansible常用模块之ping-连通节点
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@control web001]# ansible web001 -m ping
web001 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
ansible常用模块之command-执行远程命令
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
[root@control web001]# ansible web001 -m command -a 'date'
web001 | CHANGED | rc=0 >>
Mon Jun 6 03:52:07 CST 2022
#使用command模块 -m command 可以省略
[root@control web001]# ansible web001 -a 'date'
web001 | CHANGED | rc=0 >>
Mon Jun 6 03:51:40 CST 2022
#查看并创建文件
[root@control web001]# ansible web001 -a 'ls'
web001 | CHANGED | rc=0 >>
anaconda-ks.cfg
passwd
[root@control web001]# ansible web001 -a 'touch 1234'
[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.
web001 | CHANGED | rc=0 >>
[root@control web001]# ansible web001 -a 'ls'
web001 | CHANGED | rc=0 >>
1234
anaconda-ks.cfg
passwd
#不支持重定向 与管道符
[root@control web001]# ansible web001 -a 'echo qwe > 1234'
web001 | CHANGED | rc=0 >>
qwe > 1234
[root@control web001]# ansible web001 -a 'cat 1234'
web001 | CHANGED | rc=0 >>
[root@control web001]# ansible web001 -a 'cat passwd |grep ss'
web001 | FAILED | rc=1 >>
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologincat: '|grep': No such file or directory
cat: ss: No such file or directorynon-zero return code
ansible常用模块之raw-执行远程命令
raw模块用于在远程主机上执行命令,其支持管道符与重定向
[root@control web001]# ansible web001 -m raw -a 'cat passwd |grep sshd'
web001 | CHANGED | rc=0 >>
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
Shared connection to web001 closed.
[root@control web001]# ansible web001 -m raw -a 'echo xixi >> 1234'
web001 | CHANGED | rc=0 >>
Shared connection