Use dict2items with subelements

328 views
Skip to first unread message

Nicholas Henry

unread,
Oct 8, 2018, 6:59:49 PM10/8/18
to Ansible Development
I'm new to Ansible and am having trouble figuring out how to properly do this...

I've got a list of users and under them is a list of git config settings that I want Ansible to setup for me. As an example, I've got something like this:

    ---
    vars
:
      user_list
:
       
- name: john.doe
          git_config
:
           
- user.name: John Doe
           
- user.email: john.doe@example.com
           
- user.signingkey: 0A46826A
       
- name: jane.doe
          git_config
:
           
- user.name: Jane Doe
           
- diff.tool: meld


I can get most of the way there with this

    - name: just testing
      debug
:
        msg
: "User: {{ item.0.name }}, Key: {{ item.1 | dict2items }}"
      loop
: "{{ user_list | subelements('git_config') }}"


Which gives me this:

    TASK [role_under_test : just testing] ******************************************
    ok: [localhost] => (item=[{'name': 'john.doe', 'git_config': [{'user.name': 'John Doe'}, {'user.email': 'john...@example.com'}, {'user.signingkey': '0A46826A'}]}, {'user.name': 'John Doe'}]) => {
        "msg": "User: john.doe, Key: [{'key': 'user.name', 'value': 'John Doe'}]"
    }
    ok: [localhost] => (item=[{'name': 'john.doe', 'git_config': [{'user.name': 'John Doe'}, {'user.email': 'john...@example.com'}, {'user.signingkey': '0A46826A'}]}, {'user.email': 'john...@example.com'}]) => {
        "msg": "User: john.doe, Key: [{'key': 'user.email', 'value': 'john...@example.com'}]"
    }
    ok: [localhost] => (item=[{'name': 'john.doe', 'git_config': [{'user.name': 'John Doe'}, {'user.email': 'john...@example.com'}, {'user.signingkey': '0A46826A'}]}, {'user.signingkey': '0A46826A'}]) => {
        "msg": "User: john.doe, Key: [{'key': 'user.signingkey', 'value': '0A46826A'}]"
    }
    ok: [localhost] => (item=[{'name': 'jane.doe', 'git_config': [{'user.name': 'Jane Doe'}, {'diff.tool': 'meld'}]}, {'user.name': 'Jane Doe'}]) => {
        "msg": "User: jane.doe, Key: [{'key': 'user.name', 'value': 'Jane Doe'}]"
    }
    ok: [localhost] => (item=[{'name': 'jane.doe', 'git_config': [{'user.name': 'Jane Doe'}, {'diff.tool': 'meld'}]}, {'diff.tool': 'meld'}]) => {
        "msg": "User: jane.doe, Key: [{'key': 'diff.tool', 'value': 'meld'}]"
    }


What I want is to be able to extract the key and value somehow (I need to know the user's name so that I can become the user and then I need to loop through all of the git config key/values so that I can use Ansible's git config module: https://docs.ansible.com/ansible/2.4/git_config_module.html):

    "msg": "User: jane.doe, Key: diff.tool, Value: meld"

with something like this

    - name: just testing
      debug
:
        msg
: "User: {{ item.0.name }}, Key: {{ item.1.key }}, Value: {{ item.1.value }}"


Please see ansible-role-git to get a better idea of what I'm trying to do.
Reply all
Reply to author
Forward
0 new messages