# Loop of JSON called by Ansible task - Jinja2

**URL:** <https://forum.ansible.com/t/loop-of-json-called-by-ansible-task-jinja2/23292>\
**Category:** Ansible Developer\
**Created:** [December 20, 2016, 9:36am UTC](https://forum.ansible.com/t/loop-of-json-called-by-ansible-task-jinja2/23292 "2016-12-20T09:36:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hector\_Marina1](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@Hector\_Marina1](https://forum.ansible.com/u/Hector_Marina1)\
**Post date:** [December 20, 2016, 9:36am UTC](https://forum.ansible.com/t/loop-of-json-called-by-ansible-task-jinja2/23292/1 "2016-12-20T09:36:30Z")

</div>

##### ISSUE TYPE

- Feature Idea

##### COMPONENT NAME

Template module and URI module

##### ANSIBLE VERSION

```auto
ansible 2.1.1.0

```

##### OS / ENVIRONMENT

Centos 6.7

##### SUMMARY

I need to pass to a template several variables, from 2 different tasks with a loop (with\_items). This template is a JSON wich is used by another task (URI)

##### STEPS TO REPRODUCE

```auto
- name: Task1, loop 2 times
shell: jq -r ".collection|.[]|select(.name==\"{{item.vlan}}\").id" /tmp/temporal4
register: id_vlan
with_items: "{{ nic | default () }}"

```

```auto
- name: Task2, loop 2 times
shell: jq ".collection|.[]|select(.ip==\"{{item.item.ip}}\").id" /tmp/vlan{{item.stdout}}
register: id_ip
with_items: "{{id_vlan.results}}"

```

Once these tasks are executed, we have 2 variables (id\_vlan / id\_ip), with 2 values ​​stored in each one.

Now, i have another task, wich does a call API, using a JSON. I show you the task, and the formar of the JSON.

```auto
- name: API CALL
uri:
url: "https://xxxx/api/virtualmachines/"
method: POST
force_basic_auth: yes
validate_certs: no
body: "{{ lookup('template','/tmp/requestpayload.j2') }}"
body_format: json
status_code: 201
dest: "/tmp/temporal"

```

```auto
},
"title":"{{ip}}",
"rel":"",
"type":"application/v+json",
"href": "https://xxxxx/api/{{id_vlan.results}}/{{id_ip.results}}"
},

```

##### EXPECTED RESULTS

The expected results its the execution of the task ‘API CALL’ using the json with the differents values of the vars **{{id\_vlan.results}}** and **{{id\_ip.results}}** , for example:

```auto
},
"title":"192.168.1.1",
"rel":"",
"type":"application/v+json",
"href": "https://xxxxx/api/12345/6789"
},

```

and

```auto
},
"title":"192.168.1.2",
"rel":"",
"type":"application/v+json",
"href": "https://xxxxx/api/2222/1111"
},

```

##### ACTUAL RESULTS

I have not actual results because i dont know how to do it. At this moment because i have using the tasks with no loops, in other words, im using until now the JSON with only one value of each variable.  
I think that i need a bucle for of jinja2, but i dont know how to apply it.

I hope you unterstand me 🙂
