ansible command 模块

文章目录

command 模块

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

我们可以使用 ansible-doc -s command 查看该模块支持的参数,command 是默认模块,可忽略 -m 选项。我们也可以修改默认模块,在 /etc/ansible/ansible.cfg 中修改默认参数

# default module name for /usr/bin/ansible
#module_name = command
[root@master ~]# 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:                 # Change into this directory before running the command.
      cmd:                   # The command to run.
      creates:               # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
      free_form:             # The command module takes a free form command to run. There is no actual parameter named 'free form'.
      removes:               # A filename or (since 2.0) glob pattern. If it already exists, this step *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.
      

参数解析:

参数说明
chdir在执行命令之前,通过 cd 进入到该参数指定的目录
creates在创建一个文件之前,先判断文件是否存在,如果存在则跳过前面的东西,如果不存在则执行前面的动作
free_form该参数即为输入的 linux 系统命令,实现远程执行和管理
removes判断一个文件是否存在,如果存在则执行前面的动作,如果不存在则跳过前面的内容(和上面的 creates 是相反的)
warn是否提供告警信息

注意:

  • 使用 command 模块时,不得出现 shell 变量 $name ,也不得使用特殊符号 > < | ; & 等,如果需要使用前面的特殊符号则可以使用 shell 模块来实现

案例:

  • 获取所有被管理机器的负载信息
[root@master ~]# ansible dong -m command -a "uptime"
192.168.169.162 | CHANGED | rc=0 >>
 12:00:51 up  1:17,  2 users,  load average: 0.03, 0.04, 0.05
192.168.169.161 | CHANGED | rc=0 >>
 12:00:51 up  1:18,  2 users,  load average: 0.20, 0.06, 0.05

在这里插入图片描述

  • 让客户端机器,先切换到 /tmp 目录下,然后打印出当前的目录信息
[root@master ~]# ansible dong -m command -a "pwd"		# 先看下默认工作目录是什么
192.168.169.161 | CHANGED | rc=0 >>
/root
192.168.169.162 | CHANGED | rc=0 >>
/root
[root@master ~]# ansible dong -m command -a "pwd chdir=/tmp"
192.168.169.162 | CHANGED | rc=0 >>
/tmp
192.168.169.161 | CHANGED | rc=0 >>
/tmp
[root@master ~]# 

在这里插入图片描述

  • creates 参数练习

该参数的作用是在创建一个文件之前,先判断文件是否存在,如果存在则跳过前面的东西,如果不存在则执行前面的动作

# 判断 /dong 文件是否存在,存在则不执行前面的 pwd 命令,不存在则执行 pwd 命令
[root@master ~]# ansible 192.168.169.161 -m command -a "pwd creates=/dong"
192.168.169.161 | CHANGED | rc=0 >>
/root
[root@master ~]# ansible 192.168.169.161 -m command -a "pwd creates=/opt"
192.168.169.161 | SUCCESS | rc=0 >>		# 返回命令执行成功
skipped, since /opt exists				# 提示命令跳过,/opt 目录已经存在

# 使用 cp 命令拷贝文件
[root@master ~]# ansible dong -a "cp /etc/passwd /root creates=/root/passwd"
192.168.169.162 | CHANGED | rc=0 >>

192.168.169.161 | CHANGED | rc=0 >>
  • removes 参数练习

判断一个文件是否存在,如果存在则执行前面的动作,如果不存在则跳过前面的内容(和上面的 creates 是相反的)

[root@master ~]# ansible 192.168.169.161 -m command -a "pwd removes=/dong"
192.168.169.161 | SUCCESS | rc=0 >>
skipped, since /dong does not exist
[root@master ~]# ansible 192.168.169.161 -m command -a "pwd removes=/opt"
192.168.169.161 | CHANGED | rc=0 >>
/root

  • warn 参数练习

是否提供告警信息

# 没有使用 warn 参数,在使用一些命令时(例如:chmod),系统会提示一些告警信息
[root@master ~]# ansible dong -a "chmod u+x /root/passwd"
[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.169.161 | CHANGED | rc=0 >>

192.168.169.162 | CHANGED | rc=0 >>

# 使用 warn=false 参数后则没有告警信息显示了
[root@master ~]# ansible dong -a "chmod u+x /root/passwd warn=false"
192.168.169.162 | CHANGED | rc=0 >>

192.168.169.161 | CHANGED | rc=0 >>

 
 
 
 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值