Search code examples
ansibleansible-2.xconsul

Ansible - using json imported from Consul


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"
}

Solution

  • 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