【问题标题】:Terminate ansible playbook based on shell output根据 shell 输出终止 ansible playbook
【发布时间】:2015-03-04 22:56:25
【问题描述】:

我有一个运行 shell 命令的 ansible playbook。如果输出中有特定消息,我需要终止 playbook。这是我尝试过的:

- name : Do foo
  shell: /bin/my_application arg1 arg2 arg3
  args:
    creates: /tmp/foo_1
  with_items: data_items
  register: foo_result

- debug: var=foo_result

- debug: var=foo_result
  when:
    - foo_result.results is defined
    - '"[Foo Bar Baz] Err Code abc123" in foo_result.results.stdout'
  failed_when: true

这里的意图是 if '"[Foo Bar Baz] Err Code abc123"' 出现在程序的输出中,我想打印 full 输出(其中包含非常有用的信息,但有 很多 信息,所以我不想一直打印它所有)然后中止剧本。 p>

不幸的是,这不太行。

第一个调试语句打印出类似这样的内容:

TASK: [do_stuff | debug var=foo_result] **************************** 
ok: [some-node] => {
    "foo_result": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "changed": true, 
[Snip..]
                "stderr": "", 
                "stdout": "Very large stack trace containing [Foo Bar Baz] Err Code abc123"
            }
        ]
    }
}

所以,我知道我感兴趣的错误消息stdout

但是第二个打印了这个:

TASK: [do_stuff | debug var=foo_result] **************************** 
fatal: [some-node] => error while evaluating conditional: "[Foo Bar Baz] Err Code abc123" in foo_result.results.stdout

我也尝试过使用foo_result.stdout,并将[] 转义为\[\] 在条件下,我已经尝试测试foo.results.stdout is defined,我'不知道为什么我会得到所有这些排列的条件评估错误。我本来预计会出现语法错误或其他东西......有没有更好的方法来做我在这里尝试做的事情(当命令的标准输出中有特定文本然后打印该标准输出时失败)?

(Ansible 版本为 1.6.10)

【问题讨论】:

    标签: ansible ansible-playbook


    【解决方案1】:

    你只需要更改第三个任务:

    - debug: var=foo_result
      failed_when: "'[Foo Bar Baz] Err Code abc123' in foo_result.stdout"
    

    适用于我的 ansible 1.8.4!

    【讨论】:

    • 据我所知,主要区别在于您将单引号换成了双引号。我试过这个但得到了同样的错误(除了不同的引号)。请注意,我们使用的是 Ansible 1.6.10...
    • @FrustratedWithFormsDesigner 您应该在原始问题中提及您的 Ansible。 1.6 太老了。你真的应该努力升级。
    • @Mxx:是的,升级会很好,但可能需要等待。我想我解决了我最初发布的问题,但出现了新问题......
    【解决方案2】:

    我的任务 yml 文件中似乎存在某种语法错误,但它们并未报告为“语法错误”。

    问题似乎是我是如何“和”条件的。我以为我可以单独列出它们,但似乎不是这样。

    这个任务:

    - debug: var=foo_result
      when:
        - foo_result.results is defined and "[Foo Bar Baz] Err Code abc123" in foo_result
    

    不会给我任何错误。 Ansible 没有在此代码上返回错误,但条件始终评估为“真”。但是,我认为这是一个不同的问题,我将为此发布一个新问题。


    我在这里发布了一个后续问题:Ansible condition always evaluates to false

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2013-12-09
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多