# Ansible iterate from file and populate template

**URL:** <https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561>\
**Category:** Ansible Project\
**Created:** [June 13, 2019, 1:06pm UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561 "2019-06-13T13:06:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicola\_Contu](https://avatars.discourse-cdn.com/v4/letter/n/4bbf92/32.png) [@Nicola\_Contu](https://forum.ansible.com/u/Nicola_Contu)\
**Post date:** [June 13, 2019, 1:06pm UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561/1 "2019-06-13T13:06:00Z")

</div>

Hello,  
I have a template with few variables to be populated, then I am shipping that template populated to a another server.

Here is my playbook :

- hosts: dev  
become: yes  
become\_method: sudo  
vars:  
username: “{{ username }}”  
ad\_username: “{{ ad\_username }}”  
vhost\_id: “{{ vhost\_id }}”  
tasks:
- name: Ansible Template  
template:  
src: /usr/local/cmd-sysadmin/automation/ansible/cmd/files/templates/apache\_vhost\_api\_private\_public.j2  
dest: /usr/local/apache2.4/conf/vhosts/v2/{{ vhost\_id }}\_{{ username }}\_apis.vhost.conf
- name: Add vhost to httpd.conf  
shell: echo “Include conf/vhosts/v2/{{ vhost\_id }}\_{{ username }}\_apis.vhost.conf” \>\> /usr/local/apache2.4/conf/httpd.conf

It works well if I call it from command line and I pass all variable via -e switch.  
Something like : ansible-playbook playbooks/apache/create\_new\_vhost\_from\_template\_dev.yml -K -e “username=xxxx ad\_username=xxxx.xxx vhost\_id=60”

As I have too many vhosts to be created, I got a file from where I would like to read content, iterate it, populate variables and push the file.

The file is like this :

5;yyy;yyyyy  
70;zzzz;zzz

I can modify the file to include the header, to be , delimited… whatever.

Is there anyone that can explain how to do this?

It’s hard for me to find something like this on the documentation.

Can anyone help?

Thanks

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [June 13, 2019, 1:50pm UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561/2 "2019-06-13T13:50:48Z")

</div>

Hi Nicola,

> As I have too many vhosts to be created, I got a file from where I would  
> like to read content, iterate it, populate variables and push the file.
> 
> The file is like this :  
> 5;yyy;yyyyy  
> 70;zzzz;zzz
> 
> I can modify the file to include the header, to be , delimited.. whatever.

Since this is basically a CSV file, you might have a look at the  
read\_csv module.

[https://docs.ansible.com/ansible/latest/modules/read\_csv\_module.html](https://docs.ansible.com/ansible/latest/modules/read_csv_module.html)

This is only available in Ansible 2.8 though, for older versions you  
might use the csvfile lookup.

[https://docs.ansible.com/ansible/latest/plugins/lookup/csvfile.html](https://docs.ansible.com/ansible/latest/plugins/lookup/csvfile.html)

Regards  
Sebastian

---

<div class="post-metadata">

**Author:** ![Nicola\_Contu](https://avatars.discourse-cdn.com/v4/letter/n/4bbf92/32.png) [@Nicola\_Contu](https://forum.ansible.com/u/Nicola_Contu)\
**Post date:** [June 13, 2019, 2:16pm UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561/3 "2019-06-13T14:16:33Z")

</div>

Hey Sebastian,  
thanks for your reply.

But once I read from the csv how do I get the variables populated and how to iterate?

I have something like this now

- hosts: dev  
become: yes  
become\_method: sudo  
vars:  
username: “{{ users.0 }}”  
ad\_username: “{{ users.1 }}”  
vhost\_id: “{{ users.2 }}”  
tasks:
- name: Read users from CSV file and return a list  
read\_csv:  
path: /home/ncontu/users.csv  
delimiter: ‘;’  
register: users
- name: Ansible Template  
template:  
src: /usr/local/cmd-sysadmin/automation/ansible/cmd/files/templates/apache\_vhost\_api\_private\_public.j2  
dest: /usr/local/apache2.4/conf/vhosts/v2/{{ vhost\_id }}\_{{ username }}\_apis.vhost.conf  
loop: users
- name: Add vhost to httpd.conf  
shell: echo “Include conf/vhosts/v2/{{ vhost\_id }}\_{{ username }}\_apis.vhost.conf” \>\> /usr/local/apache2.4/conf/httpd.conf  
loop: users

---

<div class="post-metadata">

**Author:** ![Nicola\_Contu](https://avatars.discourse-cdn.com/v4/letter/n/4bbf92/32.png) [@Nicola\_Contu](https://forum.ansible.com/u/Nicola_Contu)\
**Post date:** [June 14, 2019, 9:51am UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561/4 "2019-06-14T09:51:49Z")

</div>

I found the way to loop and get variables.

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 14, 2019, 5:11pm UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561/5 "2019-06-14T17:11:05Z")

</div>

It's a common courtesy to also provide the solution.

Think about the next person that has the same challenge as you only to find your post that basically only say solved it.

---

<div class="post-metadata">

**Author:** ![Angel\_Rengifo\_Cancin](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Angel\_Rengifo\_Cancin](https://forum.ansible.com/u/Angel_Rengifo_Cancin)\
**Post date:** [August 16, 2019, 8:37pm UTC](https://forum.ansible.com/t/ansible-iterate-from-file-and-populate-template/30561/6 "2019-08-16T20:37:43Z")

</div>

That’s exactly what I’m looking for: how he solved his issue. Unfortunately, he only wanted to solve his problem, but he didn’t care about others having the same problem
