Ansibile常用模块

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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值