Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from json within ansible

I have an ansible playbook that creates some IAM users in AWS. I'd like the playbook to return the username and access keys details for each account created.

Registering the output from the task is fairly straightforward:

  - name: Create IAM users
    iam:
      iam_type: user
      name: "{{ item }}"
      state: present
      access_key_state: create
    with_items:
       - "user1"
       - "someotheruser"
       - "user3"
    register: users

I'm finding doing something with that output tricky. The Json it produces is as follows: (slightly truncated to reduce the length here)

ok: [localhost] => {
    "users": {
        "changed": true,
        "msg": "All items completed",
        "results": [
                "user_meta": {
                    "access_keys": [
                        {
                            "access_key_id": "key1",
                            "access_key_selector": "HMAC",
                            "create_date": "2016-05-19T08:37:11.007Z",
                            "secret_access_key": "secretkey1",
                            "status": "Active",
                            "user_name": "user1"
                        }
                    ],
                }
            },
            {
                "user_meta": {
                    "access_keys": [
                        {
                            "access_key_id": "key2",
                            "access_key_selector": "HMAC",
                            "create_date": "2016-05-19T08:37:13.391Z",
                            "secret_access_key": "secretkey2",
                            "status": "Active",
                            "user_name": "someotheruser"
                        }
                    ],
                }
            },
            {
                "user_meta": {
                    "access_keys": [
                        {
                            "access_key_id": "key3",
                            "access_key_selector": "HMAC",
                            "create_date": "2016-05-19T08:37:16.243Z",
                            "secret_access_key": "secretkey3",
                            "status": "Active",
                            "user_name": "user3"
                        }
                    ],
                }
            }
        ]
    }
}

I've want to display this neatly on completion of the playbook, the closest I've come to getting what I'm after is use debug as follows:

  - debug: var="users.results[0].user_meta.access_keys"
  - debug: var="users.results[1].user_meta.access_keys"
  - debug: var="users.results[2].user_meta.access_keys"

However that feels lame. What if I add user 4 and 5? I'd like to be able to refer to the entire results array so that it will work no matter how many users I add to the playbook.

Is there a way to achieve this?

As a side note, I've also tried to use from_json, e.g.

- debug: msg="{{ (users.stdout|from_json).results }}"

but I don't know if I'm hitting a bug with ansible 2.0.1.0 or if I'm using it wrong but I could only ever get "type error. expected string or buffer"

Any thoughts gladly received.

like image 977
Ansiballer Avatar asked Nov 01 '25 00:11

Ansiballer


2 Answers

You can iterate through the registered variable as well.

Something like this should work:

 - name      : debug access keys
   debug     : var="{{ item.user_meta.access_keys }}"
   with_items: users.results
like image 94
ydaetskcoR Avatar answered Nov 04 '25 11:11

ydaetskcoR


In case someone else wants to do the same thing...

I found the best solution for me was to count the number of objects in the array using 'length' and the then set the range that debug should iterate over.

I subtract "1" from the value returned since the first object in the array is referenced by "0".

  - name: Record Users Access Keys
    debug: var=users.results[{{ item }}].user_meta.access_keys
    with_sequence: start=0 end={{users.results|length -1}}

Hope that helps someone looking to do something similar.

like image 40
Ansiballer Avatar answered Nov 04 '25 13:11

Ansiballer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!