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

386 views
Skip to first unread message

Jason Harris

unread,
Jun 16, 2014, 1:46:29 PM6/16/14
to ansible...@googlegroups.com
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...


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

James Cammarata

unread,
Jun 16, 2014, 3:50:56 PM6/16/14
to ansible...@googlegroups.com
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.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/eb9ac1f7-5795-4a57-acc9-6f9f0605636c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Harris

unread,
Jun 16, 2014, 6:05:32 PM6/16/14
to ansible...@googlegroups.com
Thanks! That is what I needed to know!

Cheers,
   Jason

Brian Coca

unread,
Jun 16, 2014, 8:56:39 PM6/16/14
to ansible...@googlegroups.com

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

Brian Coca

Jason Harris

unread,
Jun 17, 2014, 12:12:37 PM6/17/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Jun 17, 2014, 3:17:11 PM6/17/14
to ansible...@googlegroups.com
It's also imaginary syntax.

  authorized_key: user={{item.name}} key="{{ lookup('file', item.file) }}"

Do not nest "{{" inside "{{", it's not a thing :)






Reply all
Reply to author
Forward
0 new messages