在两个 Ansible when 条件下应该检查注册变量的什么属性

What attribute should be checked for a registered variable under two Ansible when conditions

如果两个条件中只有一个被证明具有相同的注册变量,我们如何检查注册变量?

下面是我的剧本,它只执行两个 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"

我收到错误消息说我的结果没有属性 rc。根据满足的条件打印 debug one 语句的最佳方法是什么?

我尝试了 myresult is changed 但这也无济于事。能不能推荐一下。

使用ignore_errors: 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