# How to loop into user defined variables and use them to generate file using jinja template in ansible?

**URL:** <https://forum.ansible.com/t/how-to-loop-into-user-defined-variables-and-use-them-to-generate-file-using-jinja-template-in-ansible/24746>\
**Category:** Ansible Project\
**Created:** [June 16, 2017, 12:33pm UTC](https://forum.ansible.com/t/how-to-loop-into-user-defined-variables-and-use-them-to-generate-file-using-jinja-template-in-ansible/24746 "2017-06-16T12:33:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rushyang\_Darji](https://avatars.discourse-cdn.com/v4/letter/r/5fc32e/32.png) [@Rushyang\_Darji](https://forum.ansible.com/u/Rushyang_Darji)\
**Post date:** [June 16, 2017, 12:33pm UTC](https://forum.ansible.com/t/how-to-loop-into-user-defined-variables-and-use-them-to-generate-file-using-jinja-template-in-ansible/24746/1 "2017-06-16T12:33:50Z")

</div>

Hi,

I have a roles/myrole/vars/main.yml as below:

HAProxy\_Vars:  
ADMIN: “{{ lookup(‘env’, ‘ADMIN’) }}”  
MANAGED1: “{{ lookup(‘env’, ‘MANAGED1’) }}”  
MANAGED2: “{{ lookup(‘env’, ‘MANAGED2’) }}”  
MANAGED1PORT: “{{ lookup(‘env’, ‘MANAGED1PORT’)|default(‘20300’,true) }}”  
MANAGED2PORT: “{{ lookup(‘env’, ‘MANAGED2PORT’)|default(‘20300’,true) }}”  
ENVHOME: “{{ lookup(‘env’, ‘HOME’) }}”

roles/myrole/tasks/main.yml as below:

# looping into HAProxy\_Vars to verify each and every value are assigned something otherwise it should fail.

- name: “Checking if all variables are assigned during haproxy-installation role”  
fail: msg=“Variable ‘{{ item }}’ is not defined”

when: item.value == “”  
with\_dict: “{{ HAProxy\_Vars }}”

# Below is giving me hard time. Any of above mentioned variable are not identified by ansible during generation of file using eg: {{ ENVHOME }} in jinja2 template.

- name: “Generating correct service file with jinja2 template”  
tags: “haproxy-service-jinja”  
template:  
src: haproxy.service.j2  
dest: /usr/lib/systemd/system/haproxy.service  
owner: root  
group: root  
mode: ‘0600’

It fails with:

TASK [haproxy-installation : Generating correct service file with jinja2 template] \*\*\*  
task path: /ossusr01/oss/users/acdoss07/ACDSOM/roles/haproxy-installation/tasks/main.yml:36  
fatal: [adminserver]: FAILED! =\> {“changed”: false, “failed”: true, “invocation”: {“module\_args”: {“dest”: “/usr/lib/systemd/system/haproxy.service”, “group”: “root”, “mode”: “0600”, “owner”: “root”, “src”: “haproxy.service.j2”}, “module\_name”: “template”}, “msg”: “AnsibleUndefinedVariable: ' **ENVHOME’ is undefined** ”}

What am I doing wrong here?

---

<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 20, 2017, 6:41pm UTC](https://forum.ansible.com/t/how-to-loop-into-user-defined-variables-and-use-them-to-generate-file-using-jinja-template-in-ansible/24746/2 "2017-06-20T18:41:00Z")

</div>

ENVHOME doesn't exist.  
Your varible is a dictionary called HAProxy\_Vars, so you need to use the complete name {{ HAProxy\_Vars.ENVHOME }}
