ansible.builtin.command 模块 - 在目标上执行命令
注意
此模块是 ansible-core
的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使没有指定 collections 关键字,您也可以使用简短的模块名称 command
。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.command
以便轻松链接到模块文档,并避免与可能具有相同模块名称的其他集合冲突。
概要
ansible.builtin.command 模块接受命令名称,后跟一个空格分隔的参数列表。
给定的命令将在所有选定的节点上执行。
命令不会通过 shell 处理,因此
$HOSTNAME
等变量和"*"
、"<"
、">"
、"|"
、";"
和"&"
等操作将无法正常工作。如果您需要这些功能,请使用 ansible.builtin.shell 模块。必须提供自由格式命令或
cmd
参数,请参阅示例。对于 Windows 目标,请使用 ansible.windows.win_command 模块。
注意
此模块具有相应的 操作插件。
参数
参数 |
注释 |
---|---|
在运行命令之前切换到此目录。 |
|
要运行的命令。 |
|
文件名或(从 2.0 开始)通配符模式。如果匹配的文件已存在,则不会运行此步骤。 在检查 |
|
扩展作为变量存在的参数,例如 设置为 选择
|
|
命令模块接受自由格式字符串作为要运行的命令。 没有名为 |
|
文件名或(从 2.0 开始)通配符模式。如果匹配的文件存在,则会运行此步骤。 在检查 |
|
将命令的标准输入直接设置为指定的值。 |
|
如果设置为 选择
|
|
从结果中标准输出/标准错误的末尾删除空行。 选择
|
属性
属性 |
支持 |
描述 |
---|---|---|
可以在检查模式下运行,并在不修改目标的情况下返回更改状态预测,如果不受支持,则将跳过操作。 |
||
支持:无 |
将在差异模式下返回有关更改内容(或可能需要在检查模式下更改的内容)的详细信息 |
|
平台: posix |
可以对其进行操作的目标操作系统/系列 |
|
支持:完全 |
指示操作是否采用“原始”或“自由格式”字符串作为选项,以及它是否有其自身的特殊解析方式 |
注意
注意
如果您想通过 shell 运行命令(例如您正在使用
<
、>
、|
等),您实际上想要的是 ansible.builtin.shell 模块。解析 shell 元字符会导致在引用不正确的情况下执行意外的命令,因此在可能的情况下使用 ansible.builtin.command 模块更安全。creates
、removes
和chdir
可以指定在命令之后。例如,如果您只想在某个特定文件不存在的情况下运行命令,请使用以下方法。当传递
creates
或removes
时支持检查模式。如果在检查模式下运行并且指定了这些选项,则模块将检查文件是否存在并报告正确的更改状态。如果未提供这些选项,则将跳过任务。自版本 2.4 以来,
executable
参数已删除。如果您需要此参数,请改用 ansible.builtin.shell 模块。对于 Windows 目标,请使用 ansible.windows.win_command 模块。
要重新启动系统,请使用 ansible.builtin.reboot 或 ansible.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
返回值
常见的返回值已在 此处 文档化,以下字段是此模块特有的
键 |
描述 |
---|---|
任务执行的命令。 返回: 始终 示例: |
|
命令执行时间差。 返回: 始终 示例: |
|
命令执行结束时间。 返回: 始终 示例: |
|
changed 返回: 始终 示例: |
|
命令返回值(0 表示成功)。 返回: 始终 示例: |
|
命令执行开始时间。 返回: 始终 示例: |
|
命令标准错误。 返回: 始终 示例: |
|
命令标准错误按行拆分。 返回: 始终 示例: |
|
命令标准输出。 返回: 始终 示例: |
|
命令标准输出按行拆分。 返回: 始终 示例: |