# Use a loop with\_items into a body command of uri ansible module

**URL:** <https://forum.ansible.com/t/use-a-loop-with-items-into-a-body-command-of-uri-ansible-module/28888>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [October 29, 2018, 7:36am UTC](https://forum.ansible.com/t/use-a-loop-with-items-into-a-body-command-of-uri-ansible-module/28888 "2018-10-29T07:36:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alberto\_Jimenez\_Loza](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Alberto\_Jimenez\_Loza](https://forum.ansible.com/u/Alberto_Jimenez_Loza)\
**Post date:** [October 29, 2018, 7:36am UTC](https://forum.ansible.com/t/use-a-loop-with-items-into-a-body-command-of-uri-ansible-module/28888/1 "2018-10-29T07:36:28Z")

</div>

I use a loop with\_items, but I want to put the variable into a body of a json and it doesn´t works.

- name: Associate subscriptions ak-{{tenant}}-RHEL\_7  
uri:  
url: “[https://xxxxxxxx/katello/api/activation\_keys/{{ak\_id[0]}](https://xxxxxxxx/katello/api/activation_keys/%7B%7Bak_id%5B0%5D%7D)}/add\_subscriptions”  
user: “{{user}}”  
password: “{{password}}”  
method: PUT  
return\_content: yes  
force\_basic\_auth: yes  
validate\_certs: no  
body\_format: json  
body: ‘{“subscription\_id”: “{{item}}” }’  
with\_items:
- 340
- 343  
status\_code: [200,201,202,204,301,401]

What is it wrong? → body: ‘{“subscription\_id”: “{{item}}” }’

The task includes an option with an undefined variable. The error was: ‘item’ is undefined

The error appears to have been in ‘/awx/scripts/scm/ca/satellite/ak/ca\_sat\_ak\_put\_subscriptions.yml’: line 48, column 5, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Associate subscriptions ak-{{tenant}}-RHEL\_7-{{tecnologia}}\_LB  
^ here  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

---

<div class="post-metadata">

**Author:** ![Mohan\_L](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Mohan\_L](https://forum.ansible.com/u/Mohan_L)\
**Post date:** [October 29, 2018, 10:32am UTC](https://forum.ansible.com/t/use-a-loop-with-items-into-a-body-command-of-uri-ansible-module/28888/2 "2018-10-29T10:32:15Z")

</div>

It works to me.

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [October 29, 2018, 11:06am UTC](https://forum.ansible.com/t/use-a-loop-with-items-into-a-body-command-of-uri-ansible-module/28888/3 "2018-10-29T11:06:47Z")

</div>

It works to me.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 29, 2018, 1:22pm UTC](https://forum.ansible.com/t/use-a-loop-with-items-into-a-body-command-of-uri-ansible-module/28888/4 "2018-10-29T13:22:45Z")

</div>

> It works to me.

Because you use double quotes.

> ---
> 
> - hosts: all  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Generate responce with given status code  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uri:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: "[https://httpbin.org/post&quot](https://httpbin.org/post&quot);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method: POST  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body\_format: json  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: "{ \"codes\": {{ item }} }"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return\_content: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 100  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 200  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: apiresponce
> 
> \>  
> \> I use a loop with\_items, but I want to put the variable into a body of a  
> \> json and it doesn´t works.  
> \>  
> \>  
> \> - name: Associate subscriptions ak-{{tenant}}-RHEL\_7  
> \> uri:  
> \> url: "  
> \> [https://xxxxxxxx/katello/api/activation\_keys/\{\{ak\_id\[0\]\}\](https://xxxxxxxx/katello/api/activation_keys/%5C%7B%5C%7Bak%5C_id%5C%5B0%5C%5D%5C%7D%5C)}/add\_subscriptions  
> \> "  
> \> user: "{{user}}"  
> \> password: "{{password}}"  
> \> method: PUT  
> \> return\_content: yes  
> \> force\_basic\_auth: yes  
> \> validate\_certs: no  
> \> body\_format: json  
> \> body: '{"subscription\_id": "{{item}}" }'

You need to use double quotes instead of single quotes.
