文章目录
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 >>