# Trying to use lookup('file', ...) with an array of items.

**URL:** <https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474>\
**Category:** Ansible Project\
**Created:** [June 16, 2014, 5:46pm UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474 "2014-06-16T17:46:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@Jason\_Harris](https://forum.ansible.com/u/Jason_Harris)\
**Post date:** [June 16, 2014, 5:46pm UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474/1 "2014-06-16T17:46:29Z")

</div>

Hi All,

I am trying to load up a list of users and their keys. As in something like:

authorized\_key: user={{item.name}} key=“{{ lookup(‘file’, {{item.file}}) }}”  
with\_items:

- { name: “{{bill\_username}}”, file: ‘bill\_ras.pub’ }
- { name: “{{bob\_username}}”, file: ‘bob\_rsa.pub’ }
- { name: “{{kim\_username}}”, file: ‘kim\_rsa.pub’ }

This fails with:

failed: [deploytest] =\> (item={‘name’: u’billsmith’, ‘file’: ‘bill\_rsa.pub’}) =\> {“failed”: true, “item”: {“file”: “bill\_rsa.pub”, “name”: “billsmith”}}  
msg: Failed to lookup user {{item.name}}: 'getpwnam(): name not found: {{item.name}}

I have tried various permutations with quoting and can’t seem to get this to work. This looks like a related thread…

[https://groups.google.com/forum/#!msg/ansible-project/mNy0xhZ8sew/wKA4qKOM15sJ](https://groups.google.com/forum/#!msg/ansible-project/mNy0xhZ8sew/wKA4qKOM15sJ)

It is a strange error message that I don’t quite get. In any case how do I set a range of users and keys without repeating the line a number of times?

This is on ansible 1.6.3.

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 16, 2014, 7:50pm UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474/2 "2014-06-16T19:50:50Z")

</div>

This is a known issue when using with\_items and complex data structures, where the data structure is flattened to a string.

Do this instead:

- authorized\_key:  
user: “{{item[‘name’]}}”  
key: “{{ lookup(‘file’, {{item.file}}) }}”  
with\_items:

- { name: “{{bill\_username}}”, file: ‘bill\_ras.pub’ }

- { name: “{{bob\_username}}”, file: ‘bob\_rsa.pub’ }

- { name: “{{kim\_username}}”, file: ‘kim\_rsa.pub’ }

This preserves the data structure and works as intended.

---

<div class="post-metadata">

**Author:** ![Jason\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@Jason\_Harris](https://forum.ansible.com/u/Jason_Harris)\
**Post date:** [June 16, 2014, 10:05pm UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474/3 "2014-06-16T22:05:31Z")

</div>

Thanks! That is what I needed to know!

Cheers,  
Jason

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 17, 2014, 12:56am UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474/4 "2014-06-17T00:56:35Z")

</div>

Too many mustaches!!! You just use a bare item.file inside the looks.

Brian Coca

---

<div class="post-metadata">

**Author:** ![Jason\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@Jason\_Harris](https://forum.ansible.com/u/Jason_Harris)\
**Post date:** [June 17, 2014, 4:12pm UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474/5 "2014-06-17T16:12:37Z")

</div>

Thanks! Yes, good point and that worked (having the bare item whereas with the extra moustaches it didn’t work.)

So in the end the following works:

- authorized\_key:  
user: “{{item[‘name’]}}”  
key: “{{ lookup(‘file’, item.file }}”  
with\_items:

- { name: “{{bill\_username}}”, file: ‘bill\_ras.pub’ }

- { name: “{{bob\_username}}”, file: ‘bob\_rsa.pub’ }

- { name: “{{kim\_username}}”, file: ‘kim\_rsa.pub’ }

Thanks all!

Cheers,  
Jason

---

<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:** [June 17, 2014, 7:17pm UTC](https://forum.ansible.com/t/trying-to-use-lookup-file-with-an-array-of-items/15474/6 "2014-06-17T19:17:08Z")

</div>

It’s also imaginary syntax.

authorized\_key: user={{[item.name](http://item.name/)}} key=“{{ lookup(‘file’, item.file) }}”

Do not nest “{{” inside “{{”, it’s not a thing 🙂
