Ansible模块(command)不支持的参数

96次阅读
没有评论

问题描述

在使用Ansible任务中运行shell命令时遇到了错误。他想要运行的任务和shell命令如下:

- name: Set File Watch Limit
  ansible.builtin.shell:
    cmd: "echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p"

但是,他遇到了以下错误:

fatal: [...]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (command) module: cmd Supported parameters include: _raw_params, _uses_shell, argv, chdir, creates, executable, removes, stdin, stdin_add_newline, strip_empty_ends, warn"}

他想知道如何让Ansible运行这个命令。如果他手动登录到服务器并执行命令,它可以正常工作。

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

根据回答1,您可以使用shell模块来运行shell命令,而不是使用ansible.builtin.shell模块。以下是修改后的任务:

- name: Set File Watch Limit
  shell: "echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p"

这样修改后,您应该能够成功运行命令。

方案2

如果您仍然遇到问题,可以尝试使用command模块来运行命令。以下是修改后的任务:

- name: Set File Watch Limit
  command: "echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p"

使用command模块也可以成功运行命令。
请注意,ansible.builtin.shell模块是Ansible 2.9及更早版本中的一种方法,而shell模块和command模块是Ansible 2.10及更高版本中的替代方法。根据您使用的Ansible版本,选择适合您的模块即可。
以上是解决这个问题的两种方案,您可以根据您的需求选择其中一种来运行命令。

正文完