# How can i load a vars file in inventory which "prepares" a large number of variables via jinja2 expressions ?

**URL:** <https://forum.ansible.com/t/how-can-i-load-a-vars-file-in-inventory-which-prepares-a-large-number-of-variables-via-jinja2-expressions/24863>\
**Category:** Ansible Project\
**Created:** [June 30, 2017, 2:09pm UTC](https://forum.ansible.com/t/how-can-i-load-a-vars-file-in-inventory-which-prepares-a-large-number-of-variables-via-jinja2-expressions/24863 "2017-06-30T14:09:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [June 30, 2017, 2:09pm UTC](https://forum.ansible.com/t/how-can-i-load-a-vars-file-in-inventory-which-prepares-a-large-number-of-variables-via-jinja2-expressions/24863/1 "2017-06-30T14:09:22Z")

</div>

For some servers in an inventory, i assign a range of ports. I want to use these ports in my playbooks while keeping them portable and compatible with any configuration. I am thinking of using variable for ports whose values are resolved via some script in inventory. Something like this:

–In Playbooks–  
`portValueUser: p1 hostName: localhost:p2 someURL: http://hostname:p3`

–For first inventory, values of p1, p2, p3…—  
`p1: 5101 p2: 5102 p3 5103 ... ... p100: 5200`

–For seconds inventory, values of p1, p2, p3…—  
`p1: 6201 p2: 6202 p3 6203 ... ... p100: 6300`

This way, i can use a series of port values in my playbook which are defined by inventory(host vars). I am trying to figure out a way to really apply some logic in constructing these vars  
Something like, in inventory/group\_vars/all/load.yaml.j2  
(please ignore the syntax errors, this is for illustration)

`

{% if a \> b %}

{% for i = 1 to 100 and val = 5101 to 5200 %}

p{{i}} = val

{% endfor %}

{% else %}

{% for i = 1 to 100 and val = 6201 to 6300 %}

p{{i}} = val

{% endfor %}

{% endif %}

`

Remember the 2 main questions here:  
1 - Can i really construct a large number of variables with loop like this ?  
2 - How can i load or rather resolve a vars file like above via the inventory ?

---

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [June 30, 2017, 3:10pm UTC](https://forum.ansible.com/t/how-can-i-load-a-vars-file-in-inventory-which-prepares-a-large-number-of-variables-via-jinja2-expressions/24863/2 "2017-06-30T15:10:00Z")

</div>

I do something similar in a role by generating a YML file, which gets included later on by include\_vars.

This are the includes in the main.yml of the role:

- include: generate\_vars.yml
- include\_vars: generated\_vars.yml

And this is the task in generate\_vars.yml:

shell: |-  
exec \>generated\_vars.yml  
echo —  
output\_some\_data  
run\_once: true  
check\_mode: false

I do the generation just once and disable check\_mode to avoid errors by undefined variables during checks, because in check mode shell scripts are not executed.
