ansible模块--command模块

1.在远程主机上执行指定得命令 如:cat ls ,不能使用特殊得符号 :| > >>

https://www.cndba.cn/hbhe0316/article/16705
https://www.cndba.cn/hbhe0316/article/16705
[root@ansible ansible]# ansible -i hosts mysql -m command -a "hostname"
192.168.56.88 | CHANGED | rc=0 >>
mysql8
[root@ansible ansible]# ansible -i hosts mysql -m command -a "ip a"
192.168.56.88 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:dd:a6:08 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.88/24 brd 192.168.56.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::6ac2:72f:9232:585b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

[root@ansible ansible]# ansible -i hosts mysql -m command -a "ls -l"
192.168.56.88 | CHANGED | rc=0 >>
total 4
-rw-------. 1 root root 1715 Sep 12 14:36 anaconda-ks.cfg

2.如果使用ansible不支持的特殊字符,则抛错https://www.cndba.cn/hbhe0316/article/16705https://www.cndba.cn/hbhe0316/article/16705

https://www.cndba.cn/hbhe0316/article/16705
https://www.cndba.cn/hbhe0316/article/16705 https://www.cndba.cn/hbhe0316/article/16705 https://www.cndba.cn/hbhe0316/article/16705
[root@ansible ansible]# ansible -i hosts mysql -m command -a "ls -l | grep an"
192.168.56.88 | FAILED | rc=2 >>
ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access an: No such file or directorynon-zero return code

3.creates和removes
creates:当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行
removes:当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行https://www.cndba.cn/hbhe0316/article/16705https://www.cndba.cn/hbhe0316/article/16705

# /etc/fstab文件存在,则cat命令不执行
[root@ansible ansible]# ansible -i hosts mysql -m command -a "creates=/etc/fstab   cat /etc/fstab"
192.168.56.88 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists

# /etc/fstab文件存在,则cat命令执行
[root@ansible ansible]# ansible -i hosts mysql -m command -a "removes=/etc/fstab   cat /etc/fstab"
192.168.56.88 | CHANGED | rc=0 >>

#
# /etc/fstab
# Created by anaconda on Sun Sep 12 14:30:00 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=747ccae3-a4b8-4b11-b77f-cd3d704f0e57 /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

版权声明:本文为博主原创文章,未经博主允许不得转载。

ansible

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值