Ansible 在两种情况下,应检查注册变量的哪些属性

Ansible 在两种情况下,应检查注册变量的哪些属性,ansible,attributes,runtimeexception,.when,Ansible,Attributes,Runtimeexception,.when,如果两个条件中只有一个是真的,并且具有相同的注册变量,我们如何检查注册变量 下面是我的剧本,它只执行两个shell模块中的一个 - name: Check file shell: cat /tmp/front.txt register: myresult when: Layer == 'front' - name: Check file shell: cat /tmp/back.txt register: myresult when: Layer ==

如果两个条件中只有一个是真的,并且具有相同的注册变量,我们如何检查注册变量

下面是我的剧本,它只执行两个shell模块中的一个

- name: Check file
    shell: cat /tmp/front.txt
    register: myresult
  when: Layer == 'front'

- name: Check file
    shell: cat /tmp/back.txt
    register: myresult
  when: Layer == 'back'

- debug:
    msg: data was read from back.txt and print whatever
  when: Layer == 'back' and myresult.rc != 0

- debug:
    msg: data was read from front.txt and print whatever
  when: Layer == 'front' and myresult.rc != 0
按以下步骤运行上述剧本:

ansible playbook test.yml-e Layer=“front”

我确实收到一个错误,说myresult没有属性
rc
。根据满足的条件打印debug one语句的最佳方式是什么


我尝试了
myresult已更改
,但这也无济于事。请您建议。

使用
忽略错误:true
并更改任务顺序。尝试如下

  - name: Check file
    shell: cat /tmp/front.txt
    register: myresult
    when: Layer == 'front'
  - debug:
     msg: data was read from front.txt and print whatever
    when: not myresult.rc
    ignore_errors: true


  - name: Check file
    shell: cat /tmp/back.txt
    register: myresult
    when: Layer == 'back'
  - debug:
     msg: data was read from back.txt and print whatever
    when: not myresult.rc
    ignore_errors: true

而不是
debug:
如果我有
fail:
模块,如果条件满足,该模块应该失败,那么我们将如何解决这个问题<代码>忽略错误我想不会有帮助。有什么线索吗?不确定你的要求是什么。但据我所知,尝试使用failed_when而不是when,如下所示-debug:msg:data从front.txt读取,并在when:not myresult.rc打印任何失败的_