ansible的command模块

command模块

作用:在远程节点上执行一个命令

#查看该模块支持的参数
[root@m01 ~]# ansible-doc -s command 
- name: Execute commands on targets
  command:
      argv:                  # Passes the command as a list rather than a string. Use `argv' to avoid
                               quoting values that would otherwise be
                               interpreted incorrectly (for example
                               "user name"). Only the string or the list
                               form can be provided, not both.  One or
                               the other must be provided.
      chdir:                 # 在执行命令之前,先通过cd进入该参数指定的目录
      cmd:                   # The command to run.
      creates:               # 在创建一个文件之前,判断该文件是否存在,如果存在了则跳过前面的动作;如果不存在则执行前面的动作
      free_form:             # 该参数可以输入任何的系统命令,实现远程执行和管理
                               parameter named 'free form'.
      removes:               # 定义一个文件是否存在,如果存在了则执行前面的动作;如果不存在则跳过动作
                               *will* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      stdin_add_newline:     # If set to `yes', append a newline to stdin data.
      strip_empty_ends:      # Strip empty lines from the end of stdout/stderr in result.
      warn:                  # Enable or disable task warnings.

command模块是ansible的默认基本模块,也可以省略不写,但是要注意如下的坑

• 使用command模块,不得出现shell变量$name,也不得出现特殊符号> < | ; &这些符号command都不认识,如果你想要前面指定的变量,特殊符号,请使用shell模块,command模块就不适合了

command模块案例

获取所有被管理机器的负载信息

[root@m01 ~]# ansible change -m command -a "uptime"
192.168.200.5 | CHANGED | rc=0 >>
 10:23:03 up  2:06,  2 users,  load average: 0.00, 0.01, 0.05
192.168.200.6 | CHANGED | rc=0 >>
 10:23:03 up  2:06,  2 users,  load average: 0.00, 0.01, 0.05

让客户端机器,先切换到/tmp目录下,然后打印当前的工作目录

[root@m01 ~]# ansible change -m command -a "pwd chdir=/tmp/"
192.168.200.5 | CHANGED | rc=0 >>
/tmp
192.168.200.6 | CHANGED | rc=0 >>
/tmp

练习creates参数

该参数作用是判断该文件是否存在,存在则跳过,不存在则执行

[root@m01 ~]# ansible change -m command -a "pwd creates=/change"
192.168.200.5 | CHANGED | rc=0 >>
/root
192.168.200.6 | CHANGED | rc=0 >>
/root
[root@m01 ~]# ansible change -m command -a "pwd creates=/opt"
192.168.200.5 | SUCCESS | rc=0 >>
skipped, since /opt exists
192.168.200.6 | SUCCESS | rc=0 >>
skipped, since /opt exists

/目录下没有change目录,所以执行了pwd;/目录下有opt,所以跳过

参数removes实践,存在则执行,不存在则跳过

[root@m01 ~]# ansible change -a "ls /opt removes=/change"
192.168.200.6 | SUCCESS | rc=0 >>
skipped, since /change does not exist
192.168.200.5 | SUCCESS | rc=0 >>
skipped, since /change does not exist
[root@m01 ~]# ansible change -a "ls /root removes=/etc"
192.168.200.6 | CHANGED | rc=0 >>
anaconda-ks.cfg
mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
192.168.200.5 | CHANGED | rc=0 >>
anaconda-ks.cfg
mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz

warn参数,是否提供告警信息

[root@m01 ~]# ansible change -m command -a "chmod 000 /etc/hosts"
[WARNING]: Consider using the file module with mode rather than running 'chmod'.  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.
192.168.200.6 | CHANGED | rc=0 >>

192.168.200.5 | CHANGED | rc=0 >>

 没有添加warn参数,出现了告警信息

下面添加一下warn参数,关闭告警信息

[root@m01 ~]# ansible change -m command -a "chmod 000 /etc/hosts warn=False"
192.168.200.6 | CHANGED | rc=0 >>

192.168.200.5 | CHANGED | rc=0 >>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值