# include lines from a text file inside a .j2 file

**URL:** <https://forum.ansible.com/t/include-lines-from-a-text-file-inside-a-j2-file/24554>\
**Category:** Ansible Project\
**Created:** [May 23, 2017, 7:24pm UTC](https://forum.ansible.com/t/include-lines-from-a-text-file-inside-a-j2-file/24554 "2017-05-23T19:24:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dalton\_Porter](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Dalton\_Porter](https://forum.ansible.com/u/Dalton_Porter)\
**Post date:** [May 23, 2017, 7:24pm UTC](https://forum.ansible.com/t/include-lines-from-a-text-file-inside-a-j2-file/24554/1 "2017-05-23T19:24:20Z")

</div>

I have a list of items in a text file that need to go into a specific location in the output file.

The text file is one item per line:  
user1  
user2  
…

The output file looks like  
Section1:

Section2:  
user=user1, task=shuffle  
user=user2, task=shuffle  
…  
Section3:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 23, 2017, 9:37pm UTC](https://forum.ansible.com/t/include-lines-from-a-text-file-inside-a-j2-file/24554/2 "2017-05-23T21:37:03Z")

</div>

You want to use a lookup:

vars:  
&nbsp;&nbsp;users: "{{ lookup('file', '/home/dalton/users.txt') }}"

tasks:  
&nbsp;&nbsp;- name: Template out  
&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: roles/sys/templates/users.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /opt/users.dat  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: yes

Your template would then look like:

Section1:  
Section2:  
{% for name in users.splitlines() %}  
user= {{ name }}, task=shuffle  
{% endfor %}  
Section3:

Dick
