由于运行时错误,Ansible故障模块无法正常工作

时间:2020-03-13 04:51:55

标签: ansible runtime-error

我有一本剧本,其任务是greps为文件中的字符串。如果找到该字符串,则该块应失败。但是,在抢救失败模块内部,它无法打印消息和错误:

这是我的剧本:

---

- name: "Play 1"
  hosts: localhost
  tasks:
   - block:
      - name: "Search for IP"
        command: "grep -w {{ source_host }} {{ playbook_dir }}/allhost.hosts"
        register: command_result
        failed_when: command_result.rc == 0


     rescue:
      - name: Print custom conditional debug message
        fail:
          msg: >-
            {{
              command_result.rc == 0 |
              ternary(
                "This IP On-Boarded.",
                "The DB is not reachable."
              )
            }}

我尝试更改

command_result.rc == 0 |

收件人

command_result.stdout is search ( source_host ) |

但这也无济于事。

考虑到command_result.rc = 0;,我期望“此IP已内置”。要打印,但是没有打印。您能告诉我我的剧本是什么问题吗?

输出:

TASK [Search for IP] *****************************
[1;30mtask path: /tmp/filegaurd.yml:20[0m
[0;34mUsing module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py[0m
[0;34mPipelining is enabled.[0m
[0;34m<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: user1[0m
[0;34m<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'[0m
[0;31mfatal: [localhost]: FAILED! => {[0m
[0;31m    "changed": true, [0m
[0;31m    "cmd": [[0m
[0;31m        "grep", [0m
[0;31m        "-w", [0m
[0;31m        "10.9.9.91", [0m
[0;31m        "/tmp/allhost.hosts"[0m
[0;31m    ], [0m
[0;31m    "delta": "0:00:00.096103", [0m
[0;31m    "end": "2020-03-13 07:25:43.705938", [0m
[0;31m    "failed_when_result": true, [0m
[0;31m    "invocation": {[0m
[0;31m        "module_args": {[0m
[0;31m            "_raw_params": "grep -w 10.9.9.91 /tmp/allhost.hosts", [0m
[0;31m            "_uses_shell": false, [0m
[0;31m            "argv": null, [0m
[0;31m            "chdir": null, [0m
[0;31m            "creates": null, [0m
[0;31m            "executable": null, [0m
[0;31m            "removes": null, [0m
[0;31m            "stdin": null, [0m
[0;31m            "stdin_add_newline": true, [0m
[0;31m            "strip_empty_ends": true, [0m
[0;31m            "warn": true[0m
[0;31m        }[0m
[0;31m    }, [0m
[0;31m    "rc": 0, [0m
[0;31m    "start": "2020-03-13 07:25:43.609835", [0m
[0;31m    "stderr": "", [0m
[0;31m    "stderr_lines": [], [0m
[0;31m    "stdout": "10.9.9.91 USERID=user1 files_list=/tmp/winstone4603745991442278706.jar,/tmp/winstone5835113081224811756.jar", [0m
[0;31m    "stdout_lines": [[0m
[0;31m        "10.9.9.91 USERID=user1 files_list=/tmp/winstone460.jar,/tmp/winstone56.jar"[0m
[0;31m    ][0m
[0;31m}[0m


TASK [Print custom conditional debug message] **********************************
[1;30mtask path: /tmp/file.yml:28[0m
[0;31mfatal: [localhost]: FAILED! => {[0m
[0;31m    "changed": false, [0m
[0;31m    "msg": "False "[0m
[0;31m}[0m


PLAY RECAP *********************************************************************
[0;31mlocalhost[0m                  : [0;32mok=1   [0m changed=0    unreachable=0    [0;31mfailed=1   [0m skipped=0    [0;32mrescued=1   [0m ignored=0

1 个答案:

答案 0 :(得分:1)

jinja过滤器运算符|==更具关联性,实际评估是:

command_result.rc == (0 | ternary("This IP On-Boarded.", "The DB is not reachable."))

等效于:

command_result.rc == "The DB is not reachable."

始终等于False,这就是为什么要得到输出"msg": "False "的原因。 要解决此问题,您必须在测试周围使用括号:

(command_result.rc == 0) | ternary("This IP On-Boarded.", "The DB is not reachable.")

请注意,整个过滤器应位于同一行。