代码之家  ›  专栏  ›  技术社区  ›  Akif

Ansible shell模块无法识别失败任务的“failed”属性

  •  3
  • Akif  · 技术社区  · 8 年前

    以下代码结果 'dict object' has no attribute 'failed'

    - hosts: localhost
      tasks:
        - shell: "not_available_command"
          register: module_result
          until: module_result.failed == false
          retries: 2
          delay: 1
    

    结果:

    fatal: [localhost]: FAILED! => {
    "failed": true, 
    "msg": "The conditional check 'module_result.failed == false' failed. The error was: error while evaluating conditional (module_result.failed == false): 'dict object' has no attribute 'failed'"
    

    }

    但是当我改变了 until 作为:

          until: module_result.stderr == ""
    

    它成功重试。我用 -vvv

    fatal: [localhost]: FAILED! => {
    "attempts": 2, 
    "changed": true, 
    "cmd": "not_available_command", 
    "delta": "0:00:00.010290", 
    "end": "2017-09-25 17:28:14.078318", 
    "failed": true, 
    "invocation": {
        "module_args": {
            "_raw_params": "not_available_command", 
            "_uses_shell": true, 
            "chdir": null, 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "warn": true
        }
    }, 
    "rc": 127, 
    "start": "2017-09-25 17:28:14.068028", 
    "stderr": "/bin/sh: 1: not_available_command: not found", 
    "stderr_lines": [
        "/bin/sh: 1: not_available_command: not found"
    ], 
    "stdout": "", 
    "stdout_lines": []
    

    }

    failed stderr 失败 尝试定义时无法识别 直到 语法。

    get\u url模块:

    - hosts: localhost
      tasks:
        - get_url:
            url: "http://unavailable_file_on_the_web.txt"
            dest: "{{ playbook_dir }}"
          register: module_result
          until: module_result.failed == false
          retries: 2
          delay: 1
    

    这一次 until: module_result.failed == false 语法工程; 'failed' 属性已识别。

    那么,如何在模块上识别这个问题呢?这似乎是一个错误吗?

    1 回复  |  直到 8 年前
        1
  •  4
  •   Konstantin Suvorov    8 年前

    是的,这看起来像是一个bug或不符合预期的行为: failed 在中间尝试期间不填充字段。你可以用 ANSIBLE_DEBUG=1 :

    {
      "changed": true,
      "end": "2017-09-25 13:01:17.269225",
      "stdout": "",
      "cmd": "not_available_command",
      "rc": 127,
      "start": "2017-09-25 13:01:17.257604",
      "stderr": "/bin/sh: not_available_command: command not found",
      "delta": "0:00:00.011621",
      "invocation": {
        "module_args": {
          "warn": true,
          "executable": null,
          "_uses_shell": true,
          "_raw_params": "not_available_command",
          "removes": null,
          "creates": null,
          "chdir": null
        }
      },
      "warnings": []
    }
    

    在表2.4中 字段存在。

    供您参考 task result tests 失败 succeeded
    你可以在你的 when/until

    until: module_result | succeeded