【问题标题】:Ansible - Iterate through array using with itemsAnsible - 使用项目迭代数组
【发布时间】:2019-10-29 11:10:04
【问题描述】:

我确实有一个简单的 json 文件,我需要从每个数组项中提取一组值,但在迭代过程中它失败了。

我的剧本看起来像:

code:

---

 - name: direct - this works like charm
   set_fact:
     bb: "{{ pr_json.json.issues[0].fields.customfield_11756.value }}"

 - debug:
     var: bb

 - name: via array - this is not working since iteration is not happening
   set_fact:
     dd_branch: "{{ pr_json.json.issues[{{ item }}].fields.customfield_11756.value }}"
   register: mass

 - debug:
     var: mass

获取输出为:


TASK [jira_update : direct - this works like charm] ********************************************************************************************************************
task path: /home/test/ansible_jira/roles/jira_update/tasks/call.yml:3
ok: [localhost] => {
    "ansible_facts": {
        "bb": "R4.19"
    },
    "changed": false
}

TASK [jira_update : debug] *********************************************************************************************************************************************
task path: /home/test/ansible_jira/roles/jira_update/tasks/call.yml:7
ok: [localhost] => {
    "bb": "R4.19"
}

TASK [jira_update : via array - this is not working since iteratoin is not happening] **********************************************************************************
task path: /home/test/ansible_jira/roles/jira_update/tasks/call.yml:10
fatal: [localhost]: FAILED! => {
    "msg": "template error while templating string: expected token ':', got '}'. String: {{ pr_json.json.issues[{{ item }}].fields.customfield_11756.value }}"
}

请告诉我们如何在每个序列上遍历数组变量值。

也试过了,但是请有人帮忙迭代数组值。

 - name: Create PR request in TEMS JIRA
   jira:
     uri: "{{ tems_jira }}"
     username: "{{ user }}"
     password: "{{ pass }}"
     operation: create
     project: PR
     issuetype: 'PR-Form'
     summary: "{{ pr_json.json| json_query('issues[].fields.summary') }}"
     description: "{{ pr_json.json | json_query('issues[].fields.description') }}"
   args:
     fields:
       customfield_10303:
         value: "{{ pr_json.json | json_query('issues[].fields.customfield_11756.value') }}"

【问题讨论】:

    标签: ansible ansible-2.x ansible-inventory ansible-facts ansible-template


    【解决方案1】:

    你需要将你的列表输入一个 with_items 迭代器。这就是为循环目的设置 item 变量的原因。

    - name: via array - this is not working since iteration is not happening
      set_fact:
        dd_branch: "{{ pr_json.json.issues[ item ].fields.customfield_11756.value }}"
      register: mass
      with_items:
        - 0
        - 1
    

    这将遍历 pr_json.json.issues 的所有列表项,这将使您可以更深入地了解您正在寻找的变量结构。您可以将许多其他因素输入到循环中,您可能会感兴趣,您可以在此处找到详细信息。

    https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

    【讨论】:

    • issues 对我来说是一个数组,我需要 pr_json.json.issues[0].fields.customfield_11756.value pr_json.json.issues[1].fields.customfield_11756.value 之类的东西我需要值在 ARRAY 内的 0 和 1 的迭代。正常的迭代会像你说的那样工作,但这不是
    • 好的,如果您需要专门遍历 0 和 1,那么您可以将它们用作列表,然后遍历它们。我将编辑我的答案以显示这一点。如果不总是定义 0 和 1,那么您可以在其中一个或另一个未定义时添加额外的 when 子句。
    • 感谢您的更新,但是您共享的那个已经尝试过,并且已经提出了相同的问题 :D :D。这不起作用并将错误抛出为-“致命:[localhost]:FAILED!=> {“msg”:“模板字符串时模板错误:预期令牌':',得到'}'。字符串:{{ pr_json.json.issues[{{ item }}].fields.customfield_11756.value }}" 非常感谢您的进一步帮助。
    • 我对解决方案进行了调整。我认为不需要围绕项目的额外花括号。试一试,让我知道它是否有效。
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多