问题:

问题:

我使用以下剧本:

~ # cat play.yml

- name: MAIN PLAY
  hosts: localhost
  gather_facts: no

  vars:
    status:
      check1: present
      check2: ""

  tasks:
  - debug:
      var: status

  - name: Verify status
    set_fact:
      status_old: "{{status}}"
      status_new:
        check1: "{{'present' if status.check1 is defined and status.check1 != '' else 'absent'}}"
        check2: "{{'present' if status.check2 is defined and status.check2 != '' else 'absent'}}"
  
  - name: Combine facts
    set_fact:
      status: "{{status|combine(status_old, status_new, recursive=True) }}"

  - debug:
      var: status

输出:

PLAY [MAIN PLAY] **********************************************************************************************************************************************************************************************************************************************************

TASK [debug] **************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "status": {
        "check1": "present",
        "check2": ""
    }
}

TASK [Verify status] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Combine facts] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "status": {
        "check1": "present",
        "check2": "absent"
    }
}

正如您所看到的,这很有效,但是第二个任务name: Combine facts必须出现在剧中

问题:

有没有更好的方法来验证变量是否存在并且可以根据检测到的条件设置特定状态?
这里的目标是摆脱任务name: Combine facts并将所有内容设置在任务下name: Verify status,但不使用“块”

我也在尝试这样做:

  - name: Verify status
    set_fact:
      status: "{{ status |combine(status_old, status_new, recursive=True) }}"
      vars:
        status_old: "{{status}}"
        status_new:
          check1: "{{'present' if status.check1 is defined and status.check1 != '' else 'absent'}}"
          check2: "{{'present' if check2 is defined and check2 != '' else 'absent'}}"

但在输出中现在有变化

TASK [debug] **************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "status": {
        "check1": "present",
        "check2": ""
    }
}

如果我将“set_fact”设置为

- name: Verify status
  set_fact:
    status:
      check2: present

status.check1消失了

ok: [localhost] => {
    "status": {
        "check2": "present"
    }
}

有什么建议吗?

答案1

没有理由先设置status_old然后status_new组合它们。您可以简单地替换status您的状态中 的值set_fact。而不是这个:

  - name: Verify status
    set_fact:
      status_old: "{{status}}"
      status_new:
        check1: "{{'present' if status.check1 is defined and status.check1 != '' else 'absent'}}"
        check2: "{{'present' if status.check2 is defined and status.check2 != '' else 'absent'}}"
  
  - name: Combine facts
    set_fact:
      status: "{{status|combine(status_old, status_new, recursive=True) }}"

只需这样做:

  - name: Verify status
    set_fact:
      status:
        check1: "{{'present' if status.check1 is defined and status.check1 != '' else 'absent'}}"
        check2: "{{'present' if status.check2 is defined and status.check2 != '' else 'absent'}}"

相关内容