在任务中使用时,注册命令出现问题

时间:2019-05-09 19:40:11

标签: ansible

我在同一本剧本中有两个相同的任务:

  1. when: var == "true"
  2. when: var == "false"

两个任务都使用register: result,但是第一个失败,第二个成功。

我尝试使用block:而不是when:,并且行为相同。

bug-when.yml

---

- hosts: localhost
  tasks:
    - name: when true
      debug:
        msg: "this is true"
      register: result
      when: var == "true"

     - name: when false
       debug:
         msg: "this is false"
       register: result
       when: var == "false"

     - name: print result
       debug:
         msg: "{{ result }}"

运行它的示例:

ansible-playbook bug-when.yml -e var=true


PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Thursday 09 May 2019  18:51:35 +0000 (0:00:02.018)       0:00:02.018 **********
ok: [localhost]

TASK [when true] ***************************************************************
Thursday 09 May 2019  18:51:35 +0000 (0:00:00.437)       0:00:02.456 **********
ok: [localhost] => {
    "msg": "this is true"
}

TASK [when false] **************************************************************
Thursday 09 May 2019  18:51:35 +0000 (0:00:00.027)       0:00:02.483 **********
skipping: [localhost]

TASK [print result] ************************************************************
Thursday 09 May 2019  18:51:36 +0000 (0:00:00.023)       0:00:02.506 **********
ok: [localhost] => {
    "msg": {
        "changed": false,
        "skip_reason": "Conditional check failed",
        "skipped": true
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

运行它的第二个示例:

ansible-playbook bug-when.yml -e var=false


PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Thursday 09 May 2019  18:52:01 +0000 (0:00:02.019)       0:00:02.019 **********
ok: [localhost]

TASK [when true] ***************************************************************
Thursday 09 May 2019  18:52:01 +0000 (0:00:00.453)       0:00:02.472 **********
skipping: [localhost]

TASK [when false] **************************************************************
Thursday 09 May 2019  18:52:01 +0000 (0:00:00.024)       0:00:02.497 **********
ok: [localhost] => {
    "msg": "this is false"
}

TASK [print result] ************************************************************
Thursday 09 May 2019  18:52:02 +0000 (0:00:00.028)       0:00:02.525 **********
ok: [localhost] => {
    "msg": {
        "changed": false,
        "msg": "this is false"
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

2 个答案:

答案 0 :(得分:1)

要扩展@ b.enoit.be所说的话:

当您有这样的任务时:

- name: some task
  debug:
    msg: this is an example
  when: false
  register: result

这将更新result 即使任务被跳过。这是允许您在后续任务中查看此任务是否被跳过的

- name: check if task was skipped
  debug:
    msg: previous task was skipped
  when: result is skipped

考虑在每个任务中注册一个不同的变量,然后:

- name: when true
  debug:
    msg: "this is true"
  register: result1
  when: var == "true"

 - name: when false
   debug:
     msg: "this is false"
   register: result2
   when: var == "false"

 - name: print result
   debug:
     msg: "{{ result1.msg if result2 is skipped else result2.msg }}"

答案 1 :(得分:0)

您在此处看到的行为之所以发生是因为任务总是被注册的,这主要是因为您可以自行引用一个任务注册器,并且如果任务并非总是进行自身注册,则此行为将失败。

因此,您需要做的是拥有两个不同的寄存器句柄,并对它们的结果和属性skipped进行操作,以正确显示消息。

这是剧本:

---
- hosts: localhost
  tasks:
    - name: when true
      debug:
        msg: "this is true"
      register: result_is_true
      when: var == "true"

    - name: when false
      debug:
        msg: "this is false"
      register: result_is_false
      when: var == "false"

    - name: print result
      debug:
        msg: "{{ result_is_true if result_is_false is skipped else result_is_false }}"

这是var is true

时的运行
$ ansible-playbook so.yml -e var=true

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [host1]

TASK [when true] *******************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is true"
}

TASK [when false] ******************************************************************************************************************************************************************************************
skipping: [host1]

TASK [print result] ****************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": {
        "changed": false,
        "failed": false,
        "msg": "this is true"
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************************
host1                      : ok=3    changed=0    unreachable=0    failed=0  

这是var is false

时的结果
$ ansible-playbook so.yml -e var=false

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [host1]

TASK [when true] *******************************************************************************************************************************************************************************************
skipping: [host1]

TASK [when false] ******************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is false"
}

TASK [print result] ****************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": {
        "changed": false,
        "failed": false,
        "msg": "this is false"
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************************
host1                      : ok=3    changed=0    unreachable=0    failed=0  

不用说:我想您会简化MCVE的问题,但是您的操作实际上可以像

---
- hosts: localhost
  tasks:
    - name: print result
      debug:
        msg: "{{ 'this is true' if var == true else 'this is false' }}"

哪个运行:

$ ansible-playbook so.yml -e var=false

PLAY [localhost] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************
ok: [host1]


TASK [print result] ********************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is false"
}

PLAY RECAP *****************************************************************************************************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0   


$ ansible-playbook so.yml -e var=true

PLAY [localhost] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************

ok: [host1]

TASK [print result] ********************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is true"
}

PLAY RECAP *****************************************************************************************************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0  

这里提供了一个供参考的Jinja内联if-expression问题:https://stackoverflow.com/a/14215034/2123530