# Ansible array manipulation

**URL:** <https://forum.ansible.com/t/ansible-array-manipulation/18008>\
**Category:** Ansible Project\
**Created:** [April 17, 2015, 8:43pm UTC](https://forum.ansible.com/t/ansible-array-manipulation/18008 "2015-04-17T20:43:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Liu1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Michael\_Liu1](https://forum.ansible.com/u/Michael_Liu1)\
**Post date:** [April 17, 2015, 8:43pm UTC](https://forum.ansible.com/t/ansible-array-manipulation/18008/1 "2015-04-17T20:43:31Z")

</div>

I’m having an issue adding an element to an array. What i need to do is…

1. get an array list of IDs from an api using the uri module (no issues here)
2. add an element to this array (issue here!)
3. send a PUT with the updated array using uri (no issues here)

====tasks start====

- name: get data from endpoint 1  
uri: url=[https://someapi.com/endpoint1.json](https://someapi.com/endpoint1.json) method=GET HEADER\_api\_key=“xxxxxxxxxxxx”  
register: reg\_endpoint1

- name: get data from endpoint 2  
uri: url=[https://someapi.com/endpoint2.json](https://someapi.com/endpoint2.json) method=GET HEADER\_api\_key=“xxxxxxxxxxxx”  
register: reg\_endpoint2

- name: make an array element from id from endpoint 1, then add this to endpoint 2 array, save as updated\_array  
set\_fact: updated\_array=“{{ reg\_endpoint2.json.array\_with\_many\_elements }} + [{{ reg\_endpoint1.json.id\_list[0].id }} ]”

- name: put data with updated\_array  
uri:  
method=PUT  
url=[https://someapi.com/endpoint3.json](https://someapi.com/endpoint3.json)  
HEADER\_api\_key=“xxxxxxxxxxxx”  
HEADER\_Content-Type=“application/json”  
body={{ lookup(‘template’,‘/full/path/to/file.json.j2’) }}

====tasks end====

====/full/path/to/file.json.j2====

‘{  
“something1”: {  
“otherthing2”: {  
“thatotherthing3”: {{ updated\_array }}  
}  
}  
}’

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 17, 2015, 8:58pm UTC](https://forum.ansible.com/t/ansible-array-manipulation/18008/2 "2015-04-17T20:58:51Z")

</div>

This isn’t tested, but I believe it would look something like:

“{{ reg\_endpoint2.json.array\_with\_many\_elements + [reg\_endpoint1.json.id\_list[0].id] }}”

You need to perform the concatenation inside of a single {{ }}

---

<div class="post-metadata">

**Author:** ![Michael\_Liu1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Michael\_Liu1](https://forum.ansible.com/u/Michael_Liu1)\
**Post date:** [April 17, 2015, 9:15pm UTC](https://forum.ansible.com/t/ansible-array-manipulation/18008/3 "2015-04-17T21:15:07Z")

</div>

Thank you so much. It’s been hard trying to find any documentation on this. Any links to good docs about this topic? I feel like I’ll be playing around a lot with json, arrays, and uri module in Ansible.

I own you a beer! Next time you visit SF.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 17, 2015, 9:20pm UTC](https://forum.ansible.com/t/ansible-array-manipulation/18008/4 "2015-04-17T21:20:47Z")

</div>

The best link for jinja2 template docs is: [http://jinja.pocoo.org/docs/dev/templates/](http://jinja.pocoo.org/docs/dev/templates/)

Other than that, it’s a little bit of python, a little bit of jinja2 and some other filters and such added by ansible.

In this specific case: [http://jinja.pocoo.org/docs/dev/templates/#math](http://jinja.pocoo.org/docs/dev/templates/#math)

“+ Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way”

Anything that happens inside of {{ }} or {% %} is jinja2, outside of those, it’s just text, and not evaluated.

---

<div class="post-metadata">

**Author:** ![Michael\_Liu1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Michael\_Liu1](https://forum.ansible.com/u/Michael_Liu1)\
**Post date:** [April 17, 2015, 9:28pm UTC](https://forum.ansible.com/t/ansible-array-manipulation/18008/5 "2015-04-17T21:28:13Z")

</div>

Thanks. I need to brush up on my jinjansiblethon.
