# jinja2 template with {% include %}

**URL:** <https://forum.ansible.com/t/jinja2-template-with-include/13424>\
**Category:** Ansible Project\
**Created:** [September 5, 2013, 11:23pm UTC](https://forum.ansible.com/t/jinja2-template-with-include/13424 "2013-09-05T23:23:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [September 5, 2013, 11:23pm UTC](https://forum.ansible.com/t/jinja2-template-with-include/13424/1 "2013-09-05T23:23:50Z")

</div>

I’m trying to use ansible’s template module to assemble and authorized\_keys file, but it’s not working. I have a task:

- shell: ls sshkeys/\*  
register: keys
- template: src=authorized\_keys.j2 dest=/tmp/authorized\_keys

My authorized\_keys.j2 looks like this:

{% for key in keys.stdout\_lines %}  
command=“/path/script” {% include ‘{{ key }}’ %}  
{% endfor %}

This is failing with:

TASK: [template src=authorized\_keys.j2 dest=/tmp/authorized\_keys] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost] =\> {‘msg’: ‘{{ key }}’, ‘failed’: True}  
fatal: [localhost] =\> {‘msg’: ‘{{ key }}’, ‘failed’: True}

I don’t understand this error. Any help would be appreciated.

Regards,

Anand

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [September 5, 2013, 11:33pm UTC](https://forum.ansible.com/t/jinja2-template-with-include/13424/2 "2013-09-05T23:33:21Z")

</div>

Hi Anand, Your first task:

- shell: ls sshkeys/\*  
register: keys

is running on the remote host, the ansible client. But your second task:

- template: src=authorized\_keys.j2 dest=/tmp/authorized\_keys

it running on the ansible “server”. So it’s not going to have access to the keys.

If you want the first command to run on the ansible server you could make it a “local\_action” task.

I have a feelign you can probably use a lookup plugin to do this better though:

[http://www.ansibleworks.com/docs/playbooks2.html#lookup-plugins-accessing-outside-data](http://www.ansibleworks.com/docs/playbooks2.html#lookup-plugins-accessing-outside-data)

Romeo

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [September 5, 2013, 11:47pm UTC](https://forum.ansible.com/t/jinja2-template-with-include/13424/3 "2013-09-05T23:47:09Z")

</div>

Hi Romeo,

Thank you for your reply. Actually, I _am_ running this locally, with “ansible-playbook test.yml -c local”, so the keys are available.

In a different playbook, I am using the authorized\_key module with the file lookup plugin to populate users’ files with their keys. However, I have this other use case where I need to assemble an authorized\_keys file with the SSH command option before the key to restrict what some users are allowed to run. I don’t know if it can be done with lookup plugins, so if it’s possible with my example, I’d appreciate an example. And that’s why I was trying to use a template instead.

Anand

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev2](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Anand\_Buddhdev2](https://forum.ansible.com/u/Anand_Buddhdev2)\
**Post date:** [September 6, 2013, 9:56am UTC](https://forum.ansible.com/t/jinja2-template-with-include/13424/4 "2013-09-06T09:56:00Z")

</div>

Ok guys, I figured this out eventually. Now I’m doing:

- authorized\_key: user=blah key=“command="/path/to" {{ item }}”  
with\_file: keys.stdout\_lines

It works 🙂 No templates needed!
