Pass variables to template file from Ansible.

22 views
Skip to first unread message

Mo A

unread,
Jan 15, 2020, 3:00:21 PM1/15/20
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages