签到成功

知道了

CNDBA社区CNDBA社区

ansible模块--command模块

2021-11-14 09:03 896 0 原创 ansible
作者: hbhe0316

1.在远程主机上执行指定得命令 如:cat ls ,不能使用特殊得符号 :| > >>http://www.cndba.cn/hbhe0316/article/16705

http://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不支持的特殊字符,则抛错

http://www.cndba.cn/hbhe0316/article/16705
http://www.cndba.cn/hbhe0316/article/16705
http://www.cndba.cn/hbhe0316/article/16705
http://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:当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行

http://www.cndba.cn/hbhe0316/article/16705
http://www.cndba.cn/hbhe0316/article/16705http://www.cndba.cn/hbhe0316/article/16705
http://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

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1039606次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ