在 ansible 中使用先前分配的 set_fact 的值在 jira 任务的 when 条件下即使满足条件也会跳过

In ansible using the value of a previously assigned set_fact in a when conditional for jira task skips even though condition is met

在我的 ansible 剧本中,我试图根据条件关闭 jira 票证 - 如果我的 set_fact 的值是 == 0,循环遍历 Jira 票证密钥列表并关闭它们。该任务是 运行 在我的清单中定义的主机上(不是本地主机)。

这是我的剧本:

# Get the length of x
  - name: Find the length of x
    set_fact:
      length_of_x: "{{ x | length }}"

  - name: Printing the length of x
    debug:
      var: length_of_x

# If x is an empty list, move JIRA tickets to "Closed" status and skip next steps
  - name: Close JIRA ticket
    jira:
      uri: 'https://ip.ip.ip.ip'
      username: 'foo'
      password: 'foo'
      validate_certs: false
      project: test
      issue: "{{ item.ticket_key }}"
      operation: transition
      status: Closed
    loop: "{{ ticket_key }}"
    when: **length_of_x == 0**    
    run_once: true
    register: jira_close_results

  - name: Print the complete response
    debug: var=jira_close_results

我预计,因为我有 x 作为一个空列表,length_of_x == 0,因此 jira 票将被关闭。相反,我得到这个:

TASK [Printing x] ******************************************
ok: [host] => {
    "x": []
}

TASK [Find the length of x] **********************************
ok: [host]

TASK [Printing the length of x] **********************************
ok: [host] => {
    **"length_of_x": "0"**
}

TASK [Close JIRA ticket] *****************************
skipping: [host] => (item=test-100) 

TASK [Print the complete response] *********************************************
ok: [host] => {
    "jira_close_results": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "item": "test-100", 
                "skip_reason": "Conditional result was False", 
                "skipped": true
            }
        ]
    }
}

我尝试了很多不同的方法来定义 when 条件,但没有任何效果,每次都会跳过。提前感谢您的帮助。

好的, 将 when 语句更改为以下内容,

when: length_of_x: "0" # To consider 0 as a string
or
when: length_of_x|int: 0 # To parse length_of_x as integer

原因,Ansible returns set_fact

的字符串

Reference