# Pass variables to template file from Ansible.

**URL:** <https://forum.ansible.com/t/pass-variables-to-template-file-from-ansible/31985>\
**Category:** Ansible Project\
**Created:** [January 15, 2020, 8:00pm UTC](https://forum.ansible.com/t/pass-variables-to-template-file-from-ansible/31985 "2020-01-15T20:00:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mo\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mo_a/32/8571_2.png) [@Mo\_A](https://forum.ansible.com/u/Mo_A)\
**Post date:** [January 15, 2020, 8:00pm UTC](https://forum.ansible.com/t/pass-variables-to-template-file-from-ansible/31985/1 "2020-01-15T20:00:20Z")

</div>

Hi,

I’m using Ansible 2.9 and I’m using roles as well. I have the following variable defined in my “role-directory”/var/main.yml file:

routers:  
bbrs:

- BBR-ROUTER01
- BBR-ROUTER02  
bprs:
- BPR-ROUTER01
- BPR-ROUTER02

BBRS and BPRS are a specific type of hardware type. So depending on the hardware type a unique configuration file needs to be created for the routers associated with a specific hardware type.

For example, BBR-ROUTERs will need to have a configuration file assigned that is different than BPR-ROUTERs.

If I manually create the source template file for each router type and then call that template file to generate the config it works as expected. But I want to be able to have Ansible generate the source template files automatically based on the hardware types that are configured in the main.yml file. Then I would use those dynamically created source template files to create the appropriate config for each router belonging to a specific hardware type.

This is what I have in my create template file task:

- name: Create Template Files  
template:  
src: “bb-build-templates.j2” ---------\> **I WANT TO PASS A VARIABLE TO THE SOURCE TEMPLATE FILE SO THAT IT CAN TAKE PROPER ACTION**  
dest: “{{ templates\_dir }}/testing-{{ role\_name }}-{{ item.key }}-test\_conf.j2”  
loop: “{{ routers|dict2items }}”  
tags:
- create\_templates

Thanks,  
Mo
