# Convention over configuration or generating list fact from list variable

**URL:** <https://forum.ansible.com/t/convention-over-configuration-or-generating-list-fact-from-list-variable/15608>\
**Category:** Ansible Project\
**Created:** [July 4, 2014, 11:11am UTC](https://forum.ansible.com/t/convention-over-configuration-or-generating-list-fact-from-list-variable/15608 "2014-07-04T11:11:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro\_Romano](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Pedro\_Romano](https://forum.ansible.com/u/Pedro_Romano)\
**Post date:** [July 4, 2014, 11:11am UTC](https://forum.ansible.com/t/convention-over-configuration-or-generating-list-fact-from-list-variable/15608/1 "2014-07-04T11:11:25Z")

</div>

I want to create users and then authorise their respective ssh public keys.

Following the mantra of _convention over configuration_, the public key filename can be easily derived from the user name. However, I find myself having to explicitly declare the public key filename for each user because I can’t find a way of generating the public key filenames from the user names (from what I could find out, set\_fact doesn’t really support this), and I have to use with\_file or a lookup + with\_items, which don’t really allow me to interpolate the user name variable inline.

I would suppose this is a fairly common use case, if one is aiming to avoid “code duplication”.

Does anyone have any pointers on how to best deal with cases like this?

–Pedro.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [July 4, 2014, 12:19pm UTC](https://forum.ansible.com/t/convention-over-configuration-or-generating-list-fact-from-list-variable/15608/2 "2014-07-04T12:19:02Z")

</div>

- name: Set Authorized keys for named user  
action:  
¦ ¦ ¦ module: authorized\_key  
¦ ¦ ¦ user: “{{ item }}”  
¦ ¦ ¦ key: “{{ lookup(‘file’, ‘ssh\_pub\_keys/’ + item + ‘.pub’) }}”  
¦ ¦ ¦ manage\_dir: yes  
¦ ¦ ¦ state: present  
with\_flattened: real\_users  
when: user[item] is defined and user[item] != "

---

<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:** [July 4, 2014, 12:32pm UTC](https://forum.ansible.com/t/convention-over-configuration-or-generating-list-fact-from-list-variable/15608/3 "2014-07-04T12:32:16Z")

</div>

Might be useful sharing your datastructure with that example Serge, so people can see how it fits in. In particular I’m not sure where the variable “user” comes in from your example.

In any case, I do agree this is the good part:

“{{ lookup(‘file’, ‘ssh\_pub\_keys/’ + item + ‘.pub’) }}”

This can be simplified by assigning it to a variable somewhere, as Ansible knows how to lazy-evaluate things such as this, and it won’t “compress” down into the actual value until item is in scope.

user\_key: “{{ lookup(‘file’, ‘ssh\_pub\_keys/’ + item + ‘.pub’) }}”

{{ user\_key }}

etc, which would make for a cleaner playbook by a small margin.

---

<div class="post-metadata">

**Author:** ![Pedro\_Romano](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Pedro\_Romano](https://forum.ansible.com/u/Pedro_Romano)\
**Post date:** [July 4, 2014, 12:42pm UTC](https://forum.ansible.com/t/convention-over-configuration-or-generating-list-fact-from-list-variable/15608/4 "2014-07-04T12:42:36Z")

</div>

Thanks Serge and Michael!

I hadn’t remembered Jinja2 would actually evaluate and expression like ‘ssh\_pub\_keys/’ + item + ‘.pub’ without any issues.

And Michael’s tip about the lazy evaluation of variables is very interesting and actually more widely applicable than the present concrete case, and it does make for a slightly cleaner playbook.

–Pedro.
