不能忽略 shell 命令的错误

cannot ignore errors with shell command

我正在使用 Python 3.10.1 并且我已经使用 PIP 版本 21.2.4 安装了 Ansible

ansible --versionreturns以下

ansible [core 2.12.1]
  config file = /Users/user_name/Projects/support/ansible/ansible.cfg
  configured module search path = ['/Users/user_name/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/user_name/.pyenv/versions/3.10.1/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/user_name/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/user_name/.pyenv/versions/3.10.1/bin/ansible
  python version = 3.10.1 (main, Jan  3 2022, 06:39:07) [Clang 13.0.0 (clang-1300.0.29.30)]
  jinja version = 3.0.3
  libyaml = True

在我的剧本中,我能够 运行 除了具有忽略错误指令的任何 shellcommand 模块调用之外的所有任务。

这是违规条目:

  - name: RBENV init
    become_user: deploy
    become: true
    command:
      chdir: /home/deploy/
      cmd: rbenv init
      ignore_errors: yes

哪个returns:

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

有趣的是,如果我删除 ignore_errors: yes 命令会执行,但随后会因非零响应而阻塞 rbenv init returns.

我是 Ansible 的新手,当我 google 该错误消息时,我收到了大约 5 年和 6 年前的帖子,其中用户使用的是当时过时的 Ansible 版本。我使用的是当前版本吗?我该如何确定?

根据Error handling in playbooks的文档和错误信息

Unsupported parameters for (ansible.legacy.command) module: ignore_errors. Supported parameters include: warn, strip_empty_ends, _uses_shell, argv, _raw_params, chdir, creates, stdin, removes, executable, stdin_add_newline.

你的缩进不正确。所以错误只是由打字错误引起的,您可以改用

- name: RBENV init
  become_user: deploy
  become: true
  command:
    chdir: /home/deploy/
    cmd: rbenv init
  ignore_errors: yes

您还可以查看

引用:“如果你想执行更复杂的命令,例如用管道连接的命令,可以使用它。

关于

... but then chokes on the non-zero response rbenv init returns.

一般来说,对于您的命令,您可以引入 failed_when 而不是完全忽略错误。