跳转至

4. command模块

2. command模块

1. 查看参数

ansible-doc -s command
root@4c16g:/etc/ansible# 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.

2. 案例

#1. 执行命令,返回结果
ansible aliyun -a "uptime"
ansible aliyun -a "pwd"

#2. creates
ansible aliyun -a "pwd creates=/chaoge"
# creates参数:先判断文件夹是否存在,存在则不执行

#3. removes
ansible aliyun -a "ls /opt removes=/chaoge"
# removes参数:/opt目录中存在,则执行删除

#4. warn
ansible aliyun -a "chmod 000 /data warn=False"
# warn参数:忽略警告信息

#5. chdir
ansible aliyun -a "pwd chdir=/tmp/"
# 先切换到指定目录,再执行动作

最后更新: 2022-02-25 03:53:42