# Complex data types with nested lists

**URL:** <https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189>\
**Category:** Ansible Project\
**Created:** [May 7, 2014, 5:56pm UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189 "2014-05-07T17:56:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Pennypacker](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@Bruce\_Pennypacker](https://forum.ansible.com/u/Bruce_Pennypacker)\
**Post date:** [May 7, 2014, 5:56pm UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189/1 "2014-05-07T17:56:49Z")

</div>

We have a role that defines user accounts as follows:

users:

- username: user1  
comment: User 1  
uid: 3001  
ssh\_key: “xxx”

- username: user2  
comment: User 2  
uid: 3002  
ssh\_key: “yyy”

Users are then grouped by name into other lists:

regular\_users:

- user1
- user2  
…

ops\_users:

- user3
- user4  
…

Our hosts then have a fact called user\_roles that’s a list of each group of users and individual usernames to create accounts for. We then have tasks defined like this:

- name: add users  
user: name={{ item.username }}  
comment=“{{ item.comment }}”  
uid={{ item.uid }}  
when: item.username in lookup(‘flattened’,user\_roles)

with\_items: users

- name: add SSH keys  
authorized\_key: user={{ item.username }}  
key=“{{ item.ssh\_key }}”  
when: item.username in lookup(‘flattened’,user\_roles) and item.ssh\_key is defined  
with\_items: users

All of this is working great. We’re managing about 50 different user accounts over 80 servers in varying groups without any difficulty. But now I’d like to be able to extend this to support multiple SSH keys for each user. So I’d like to be able to define a user along these lines:

- username: userFoo  
comment: User Foo  
uid: 4321  
ssh\_keys: [‘xxx’, ‘yyy’, ‘zzz’]

But if I do this then I’m at a complete loss of how to rewrite the authorized\_key task to handle it properly. At first glance I would expect that I’d need to use with\_nested but I’m not entirely sure how to go about doing it. I tried variations of this, but haven’t gotten anything to work:

- name: add multiple SSH keys  
authorized\_key: user={{ item[0].username }}  
key=“{{ item.[1] }}”  
when: item[0].username in lookup(‘flattened’,user\_roles) and item[0].ssh\_keys is defined  
with\_nested:
- users
- item[0].ssh\_keys

Is there a better way of managing a dynamic list of ssh keys? Or am I just trying something that’s too complex for Ansible to handle cleanly?

-Bruce

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 7, 2014, 6:13pm UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189/2 "2014-05-07T18:13:42Z")

</div>

I’d recommend storing you keys in files, and using lookup(‘file’, …)

The. You can store multiple keys in a single file.

Additionally you could just store multiple keys as a string with new lines in your current data structure and not try using a list.

---

<div class="post-metadata">

**Author:** ![Bruce\_Pennypacker](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@Bruce\_Pennypacker](https://forum.ansible.com/u/Bruce_Pennypacker)\
**Post date:** [May 7, 2014, 6:32pm UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189/3 "2014-05-07T18:32:27Z")

</div>

Ah, thanks! All the documentation for the authorized\_key module implies that it works on just one key at a time, not a file/string that contains multiple keys. That documentation should really be updated…

-Bruce

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 10, 2014, 12:01am UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189/4 "2014-05-10T00:01:05Z")

</div>

Patches would be accepted! 🙂

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 10, 2014, 1:46pm UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189/5 "2014-05-10T13:46:48Z")

</div>

You can store multiple keys in a single file, but that means that you would have to manually create a separate file for each key combination you want to deploy. Instead, take a look at my pull request for with\_nested () and the example I have given in my previous post at the “Nested looping with hash/dict so I can override values” thread. It would allow you to do what you want more cleanly.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [May 11, 2014, 9:19pm UTC](https://forum.ansible.com/t/complex-data-types-with-nested-lists/15189/6 "2014-05-11T21:19:55Z")

</div>

If you specify user SSH keys as a list of keys, you can use them like this using **with\_subelements** :

`- name: User SSH keys`  
`authorized_key: user="{{ item.0.username }}" key="{{ item.1 }}"`  
`when: item.0.username in users_add`  
`with_subelements:`  
`- users`  
`- ssh_key`

This works for me without a problem. Just modify the when: condition to be like your one.
