1

I have a json content on Consul that I import to my playbook. The json contains account names and passwords:

{
    "account_1": "password_1",
    "account_2": "password_2",
    "account_3": "password_3"
}

After registering it to variable I wish to be able to use every user name and password. Since these jsons may contain different numbers of entries, I was hoping I could use with_items for this. Can someone help me out?

Debugging the whole json works with no issues:

TASK [debug] **************************************************************************
ok: [localhost] => {
    "accountsDataFromConsul.data.Value": {
        "account_1": "password_1",
        "account_2": "password_2",
        "account_3": "password_3"
    }
}

I try to get to individual entries (user names and passwords). I tried the below code, but it returned only user names:

- debug:
    var: item
  with_items: "{{accountsDataFromConsul.data.Value}}"
TASK [debug] ******************************************************************************
ok: [localhost] => (item=account_1) => {
    "item": "account_1"
}
ok: [localhost] => (item=account_2) => {
    "item": "account_2"
}
ok: [localhost] => (item=account_3) => {
    "item": "account_3"
}

3 Answers 3

1

Map data is not iterable via ansible loops.

Take a look at the dict2items filter.

#!/usr/bin/env ansible-playbook
- name: Lets munge some data
  hosts: localhost
  gather_facts: false
  become: false
  vars:
    my_dict:
      key_one: value_one
      key_two: value_two
    my_json: '{"key_one": "value_one", "key_two": "value_two"}'
  tasks:
  - name: Iterate over a dict
    debug:
      msg: "{{ item.key }} {{ item.value }}"
    loop: "{{ my_dict | dict2items }}"

  - name: Iterate over json
    debug:
      msg: "{{ item.key }} {{ item.value }}"
    loop: "{{ my_json | from_json | dict2items }}"


PLAY [Lets munge some data] *****************************************************************************************************************************************************************************************************

TASK [Iterate over a dict] ******************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'key_one', 'value': 'value_one'}) => {
    "msg": "key_one value_one"
}
ok: [localhost] => (item={'key': 'key_two', 'value': 'value_two'}) => {
    "msg": "key_two value_two"
}

TASK [Iterate over json] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'key_one', 'value': 'value_one'}) => {
    "msg": "key_one value_one"
}
ok: [localhost] => (item={'key': 'key_two', 'value': 'value_two'}) => {
    "msg": "key_two value_two"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
2
  • Right, dict2items should work. Now I need to find a way to transform my json into dictionary.
    – KGAD
    May 29, 2019 at 7:52
  • Try using the from_json filter. "{{ json_data | from_json | dict2items }}"
    – Nick
    May 29, 2019 at 14:14
1

Thank you Vladimir and Nick. That works perfectly. The only thing I had to do was using from_json on my Consul output.

- set_fact:
    jsonvar: "{{accountsDataFromConsul.data.Value | from_json}}"

- debug:
    msg: "{{ item.key }} {{ item.value }}"
  loop: "{{jsonvar | dict2items}}"
1
  • Updated original to include json example
    – Nick
    May 29, 2019 at 14:18
0

to use every user name and password

The play below

vars:
  accountsDataFromConsul:
    data:
      Value:
        account_1: password_1
        account_2: password_2
        account_3: password_3
tasks:
  - debug:
      msg: "key: {{ item.key }} value: {{ item.value }}"
    loop: "{{ accountsDataFromConsul.data.Value|dict2items }}"

gives (abridged):

"msg": "key: account_1 value: password_1"
"msg": "key: account_3 value: password_3"
"msg": "key: account_2 value: password_2"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.