ansible.builtin.command 模块 - 在目标上执行命令

注意

此模块是 ansible-core 的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使没有指定 collections 关键字,您也可以使用简短的模块名称 command。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.command 以便轻松链接到模块文档,并避免与可能具有相同模块名称的其他集合冲突。

概要

  • ansible.builtin.command 模块接受命令名称,后跟一个空格分隔的参数列表。

  • 给定的命令将在所有选定的节点上执行。

  • 命令不会通过 shell 处理,因此 $HOSTNAME 等变量和 "*""<"">""|"";""&" 等操作将无法正常工作。如果您需要这些功能,请使用 ansible.builtin.shell 模块。

  • 要创建比使用空格分隔参数的命令任务更易于阅读的命令任务,请使用 args 任务关键字 传递参数,或使用 cmd 参数。

  • 必须提供自由格式命令或 cmd 参数,请参阅示例。

  • 对于 Windows 目标,请使用 ansible.windows.win_command 模块。

注意

此模块具有相应的 操作插件

参数

参数

注释

argv

列表 / 元素=字符串

将命令作为列表而不是字符串传递。

使用 argv 来避免对可能被错误解释的值进行引用(例如“用户名”)。

只能提供字符串(自由格式)或列表(argv)形式,不能同时提供两种形式。必须提供其中一种。

chdir

路径

在运行命令之前切换到此目录。

cmd

字符串

要运行的命令。

创建

路径

文件名或(从 2.0 开始)通配符模式。如果匹配的文件已存在,则不会运行此步骤。

在检查 removes 之前检查此项。

expand_argument_vars

布尔值

在 ansible-core 2.16 中添加

扩展作为变量存在的参数,例如 $HOME 将在传递给要运行的命令之前进行扩展。

设置为 false 以禁用扩展并将该值视为文字参数。

选择

  • 错误

  • true ← (默认)

自由格式

字符串

命令模块接受自由格式字符串作为要运行的命令。

没有名为 free_form 的实际参数。

删除

路径

文件名或(从 2.0 开始)通配符模式。如果匹配的文件存在,则会运行此步骤。

在检查 creates 之后检查此项。

标准输入

字符串

将命令的标准输入直接设置为指定的值。

stdin_add_newline

布尔值

在 Ansible 2.8 中添加

如果设置为 true,则在标准输入数据末尾追加换行符。

选择

  • 错误

  • true ← (默认)

strip_empty_ends

布尔值

在 Ansible 2.8 中添加

从结果中标准输出/标准错误的末尾删除空行。

选择

  • 错误

  • true ← (默认)

属性

属性

支持

描述

check_mode

支持:部分

虽然命令本身是任意的,不能受检查模式语义的约束,但它添加了 creates/removes 选项作为变通方法

可以在检查模式下运行,并在不修改目标的情况下返回更改状态预测,如果不受支持,则将跳过操作。

diff_mode

支持:

将在差异模式下返回有关更改内容(或可能需要在检查模式下更改的内容)的详细信息

平台

平台: posix

可以对其进行操作的目标操作系统/系列

原始

支持:完全

指示操作是否采用“原始”或“自由格式”字符串作为选项,以及它是否有其自身的特殊解析方式

注意

注意

  • 如果您想通过 shell 运行命令(例如您正在使用 <>| 等),您实际上想要的是 ansible.builtin.shell 模块。解析 shell 元字符会导致在引用不正确的情况下执行意外的命令,因此在可能的情况下使用 ansible.builtin.command 模块更安全。

  • createsremoveschdir 可以指定在命令之后。例如,如果您只想在某个特定文件不存在的情况下运行命令,请使用以下方法。

  • 当传递 createsremoves 时支持检查模式。如果在检查模式下运行并且指定了这些选项,则模块将检查文件是否存在并报告正确的更改状态。如果未提供这些选项,则将跳过任务。

  • 自版本 2.4 以来,executable 参数已删除。如果您需要此参数,请改用 ansible.builtin.shell 模块。

  • 对于 Windows 目标,请使用 ansible.windows.win_command 模块。

  • 要重新启动系统,请使用 ansible.builtin.rebootansible.windows.win_reboot 模块。

  • 如果命令返回非 UTF-8 数据,则必须将其编码以避免出现问题。这可能需要使用 ansible.builtin.shell,以便可以将输出通过 base64 进行管道传输。

另请参见

另请参见

ansible.builtin.raw

执行一个低级命令。

ansible.builtin.script

在传输后在远程节点上运行本地脚本。

ansible.builtin.shell

在目标上执行 shell 命令。

ansible.windows.win_command

关于 **ansible.windows.win_command** 模块的官方文档。

示例

- name: Return motd to registered var
  ansible.builtin.command: cat /etc/motd
  register: mymotd

# free-form (string) arguments, all arguments on one line
- name: Run command if /path/to/database does not exist (without 'args')
  ansible.builtin.command: /usr/bin/make_database.sh db_user db_name creates=/path/to/database

# free-form (string) arguments, some arguments on separate lines with the 'args' keyword
# 'args' is a task keyword, passed at the same level as the module
- name: Run command if /path/to/database does not exist (with 'args' keyword)
  ansible.builtin.command: /usr/bin/make_database.sh db_user db_name
  args:
    creates: /path/to/database

# 'cmd' is module parameter
- name: Run command if /path/to/database does not exist (with 'cmd' parameter)
  ansible.builtin.command:
    cmd: /usr/bin/make_database.sh db_user db_name
    creates: /path/to/database

- name: Change the working directory to somedir/ and run the command as db_owner if /path/to/database does not exist
  ansible.builtin.command: /usr/bin/make_database.sh db_user db_name
  become: yes
  become_user: db_owner
  args:
    chdir: somedir/
    creates: /path/to/database

# argv (list) arguments, each argument on a separate line, 'args' keyword not necessary
# 'argv' is a parameter, indented one level from the module
- name: Use 'argv' to send a command as a list - leave 'command' empty
  ansible.builtin.command:
    argv:
      - /usr/bin/make_database.sh
      - Username with whitespace
      - dbname with whitespace
    creates: /path/to/database

- name: Run command using argv with mixed argument formats
  ansible.builtin.command:
    argv:
      - /path/to/binary
      - -v
      - --debug
      - --longopt
      - value for longopt
      - --other-longopt=value for other longopt
      - positional

- name: Safely use templated variable to run command. Always use the quote filter to avoid injection issues
  ansible.builtin.command: cat {{ myfile|quote }}
  register: myoutput

返回值

常见的返回值已在 此处 文档化,以下字段是此模块特有的

描述

cmd

列表 / 元素=字符串

任务执行的命令。

返回: 始终

示例: ["echo", "hello"]

delta

字符串

命令执行时间差。

返回: 始终

示例: "0:00:00.001529"

end

字符串

命令执行结束时间。

返回: 始终

示例: "2017-09-29 22:03:48.084657"

msg

布尔值

changed

返回: 始终

示例: true

rc

整数

命令返回值(0 表示成功)。

返回: 始终

示例: 0

start

字符串

命令执行开始时间。

返回: 始终

示例: "2017-09-29 22:03:48.083128"

stderr

字符串

命令标准错误。

返回: 始终

示例: "ls cannot access foo: No such file or directory"

stderr_lines

列表 / 元素=字符串

命令标准错误按行拆分。

返回: 始终

示例: [{"u'ls cannot access foo": "No such file or directory'"}, "u'ls \u2026'"]

stdout

字符串

命令标准输出。

返回: 始终

示例: "Clustering node rabbit@slave1 with rabbit@master \u2026"

stdout_lines

列表 / 元素=字符串

命令标准输出按行拆分。

返回: 始终

示例: ["u'Clustering node rabbit@slave1 with rabbit@master \u2026'"]

作者

  • Ansible 核心团队

  • 迈克尔·德汉