返回

ansible.builtin.shell 模块“额外参数”错误:如何解决并理解

Linux

Ansible.builtin.shell 模块中的“额外参数”错误:解决方案与分析

问题

在使用 ansible.builtin.shell 模块时,您可能会遇到以下错误:

ERROR! this task 'ansible.builtin.shell:' has extra params, which is only allowed in the following modules: ...

此错误表明您在任务中提供了 args 参数,而 ansible.builtin.shell 模块不支持此参数。

问题原因

ansible.builtin.shell 模块的设计中不包含 args 参数。因此,在任务中使用它会导致错误。

解决方法

要解决此问题,请从任务中删除 args 参数。修改后的任务示例:

- name: Test - Install Package
    ansible.builtin.shell: |
      ./test_uninstall.sh
      ./test_install.sh
    chdir: "{{ test_temp_dir }}"
    creates: "{{ test_bin_path }}"
    register: is_test_installed
    until: 
      - is_test_installed.rc is defined
      - is_test_installed.rc|int == 0

通过删除 args 参数,任务现在使用默认的可执行文件(/bin/sh)和当前工作目录(CWD)运行命令。

其他注意事项

  • chdir 参数用于将 CWD 更改为 test_temp_dir 目录。
  • creates 参数用于检查在运行命令后是否创建了 test_bin_path 文件。

结论

了解并解决 ansible.builtin.shell 模块中的“额外参数”错误对于正确使用该模块至关重要。通过删除 args 参数,您可以确保任务成功运行并实现预期结果。

常见问题解答

1. 除了 args 参数之外,还有哪些其他参数会导致此错误?

任何在 ansible.builtin.shell 模块中不受支持的参数都可能导致此错误。

2. 如何查看 ansible.builtin.shell 模块支持的参数列表?

您可以使用 ansible-doc 命令查看模块的文档,其中包括支持的参数列表。

3. 如何处理其他可能导致此错误的参数?

如果您在任务中使用了不支持的参数,请从任务中删除该参数。

4. 为什么 ansible.builtin.shell 模块不支持 args 参数?

ansible.builtin.shell 模块的设计旨在使用管道操作符 (|) 来执行命令,因此不需要 args 参数。

5. 此错误是否会在 Ansible 的不同版本中出现?

此错误可能会出现在 Ansible 的不同版本中,具体取决于所用模块的版本。