Ansible array manipulation

248 views
Skip to first unread message

Michael Liu

unread,
Apr 17, 2015, 4:43:31 PM4/17/15
to ansible...@googlegroups.com
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 method=GET HEADER_api_key="xxxxxxxxxxxx"
  register: reg_endpoint1

- name: get data from endpoint 2
  uri: url=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
    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 }}
    }
  }
}'

==========================


My output error contains:

<server1> REMOTE_MODULE uri method=PUT url=https://someapi.com/endpoint3.json HEADER_api_key="xxxxxxxxxxxx" HEADER_Content-Type="application/json" body='{ "something1": { "otherthing2": { "thatotherthing3": [13651171, 13651172] + [ 12994448 ] } } }'


As you can see the array is malformed from the set_fact task:

[13651171, 13651172] + [ 12994448 ]

Question:
How do I make {{ updated_array }} look like this:

[13651171, 13651172, 12994448 ]

Thanks!

Matt Martz

unread,
Apr 17, 2015, 4:59:36 PM4/17/15
to ansible...@googlegroups.com
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 {{ }}

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c0b1f256-2e28-44ec-af89-069f3e33815b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Michael Liu

unread,
Apr 17, 2015, 5:15:08 PM4/17/15
to ansible...@googlegroups.com
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.

Matt Martz

unread,
Apr 17, 2015, 5:21:32 PM4/17/15
to ansible...@googlegroups.com
The best link for jinja2 template docs is: 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.


"+ 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.


For more options, visit https://groups.google.com/d/optout.

Michael Liu

unread,
Apr 17, 2015, 5:28:13 PM4/17/15
to ansible...@googlegroups.com
Thanks.  I need to brush up on my jinjansiblethon.
Reply all
Reply to author
Forward
0 new messages