【问题标题】:Ansible Ignore errors in tasks and fail at end of the playbook if any tasks had errorsAnsible 忽略任务中的错误,如果任何任务有错误,则在剧本结束时失败
【发布时间】:2016-12-17 00:28:12
【问题描述】:

我正在学习 Ansible。我有一个清理资源的剧本,我希望剧本忽略每一个错误并一直持续到最后,如果有错误,最后会失败。

我可以忽略错误

  ignore_errors: yes

如果这是一项任务,我可以做类似(从 ansible 错误捕获)

- name: this command prints FAILED when it fails
  command: /usr/bin/example-command -x -y -z
  register: command_result
  ignore_errors: True

- name: fail the play if the previous command did not succeed
  fail: msg="the command failed"
  when: "'FAILED' in command_result.stderr"

最后我怎么会失败?我有几项任务,我的“何时”条件是什么?

【问题讨论】:

    标签: ansible ansible-playbook


    【解决方案1】:

    使用Fail 模块。

    1. 在出现错误时需要忽略的每个任务都使用 ignore_errors。
    2. 在任何任务执行失败时设置一个标志(例如,结果 = false)
    3. 在剧本的最后,检查是否设置了标志,并根据该标志,执行失败
    - fail: msg="The execution has failed because of errors."
      when: flag == "failed"
    

    更新:

    使用寄存器来存储您在示例中显示的任务的结果。然后,使用这样的任务:

    - name: Set flag
      set_fact: flag = failed
      when: "'FAILED' in command_result.stderr"
    

    【讨论】:

    • 谢谢!如何仅在 ansible 中的错误上设置标志?
    • 查看更新。基本上,您在每次任务执行后设置此标志。如果在这个剧本的最后,标志设置为失败,你的剧本就失败了。如果您的任何任务失败,它只会设置为失败。否则它不会被设置为失败并且剧本执行将成功。
    • 您可能想查看 failed_whencommand_result is failed 而不是字符串处理(它会为您提供更通用的其他命令)。
    • 我们有什么办法可以标记为失败而不是忽略并继续执行。
    【解决方案2】:

    您可以将所有可能失败的任务包装在块中,并将ignore_errors: yes 与该块一起使用。

    tasks:
      - name: ls
        command: ls -la
      - name: pwd
        command: pwd
    
      - block:
        - name: ls non-existing txt file
          command: ls -la no_file.txt
        - name: ls non-existing pic
          command: ls -la no_pic.jpg
        ignore_errors: yes 
    

    详细了解块中的错误处理here

    【讨论】:

    • 但是最后怎么会失败呢?我认为这不能回答问题。
    【解决方案3】:

    失败模块效果很好!谢谢。

    我必须在检查之前定义我的事实,否则我会得到一个未定义的变量错误。

    我在用引号和不带空格设置事实时遇到了问题。

    这行得通:

    set_fact: flag="failed"
    

    这引发了错误:

    set_fact: flag = failed 
    

    【讨论】:

      【解决方案4】:

      尝试failed_when

      - name: Fail task when the command error output prints FAILED
        ansible.builtin.command: /usr/bin/example-command -x -y -z
        register: command_result
        failed_when: "'FAILED' in command_result.stderr"
      

      【讨论】:

        【解决方案5】:

        我发现这很有帮助:

        https://medium.com/opsops/anternative-way-to-handle-errors-in-ansible-245a066c340

        在你的任务中你要注册的任务。

        register: some_name

        然后添加ignore_errors: yes

        然后使用set_fact获取各个寄存器属性:

        - set_fact:
            success: '{{ not([e1, e2]|map(attribute="failed")|max) }}'
        

        然后将其放在块的末尾:

        - name: Fail server build
          command: >
            bash scripts/test_file.sh
          when: success == false
          ignore_errors: yes
        

        上面的代码块只会在成功为false 时执行。关键是使用ignore_errors 并进行注册。从我发布的链接和我的测试中,无论是否失败,任务属性都会被注册。

        示例输出:

        PLAY [localhost] ***********************************************************************************************
        
        TASK [Gathering Facts] *****************************************************************************************
        ok: [localhost]
        
        TASK [Task 1 test] *********************************************************************************************
        fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["bash", "scripts/unknown_file.sh"], "delta": "0:00:00.004343", "end": "2021-10-20 14:20:59.320389", "msg": "non-zero return code", "rc": 127, "start": "2021-10-20 14:20:59.316046", "stderr": "bash: scripts/unknown_file.sh: No such file or directory", "stderr_lines": ["bash: scripts/unknown_file.sh: No such file or directory"], "stdout": "", "stdout_lines": []}
        ...ignoring
        
        TASK [Task 2 test] *********************************************************************************************
        changed: [localhost]
        
        TASK [set_fact] ************************************************************************************************
        ok: [localhost]
        
        TASK [Fail server build] ***************************************************************************************
        changed: [localhost]
        
        TASK [debug] ***************************************************************************************************
        ok: [localhost] => {
            "success": false
        }
        
        PLAY RECAP *****************************************************************************************************
        localhost                  : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1
        

        【讨论】:

          猜你喜欢
          • 2022-12-28
          • 1970-01-01
          • 2021-10-15
          • 2023-02-08
          • 1970-01-01
          • 2010-11-20
          • 1970-01-01
          • 2018-02-17
          相关资源
          最近更新 更多