Search code examples
ansiblejinja2cisco

Ansible & Jinja 2 templates - vars .yml additions


First spin with Ansible and I'm trying to do a relatively basic thing. I have created a Jinja2 template to output a configuration for a Nexus switch. It seems to work fine. I am following the folder structure where the top directory is the 'role' and within that directory are subdirectories of 'tasks' , 'templates', and 'vars'. Under the 'vars' directory I have one file named main.yml as I understand it should be. What I'd like to have is multiple vars files that contain various different elements so I can make the whole thing more modular.

Here are a few configs I have currently:

The playbook I run in the top directory:

    ---
    - name: Apply tor role   
      gather_facts: no   
      hosts: localhost

      roles:
        - tor

tor/tasks/main.yml file

    - name: Generate Configs
      template: src={{ item.profile }}.j2 dest=staging/drc/{{ item.hostname }}/{{ item.hostname }}.cfg
      with_items: "{{ switches }}"

And a snippet of the main.yml file in the vars directory:

---
  switches:
    - hostname: 5K01
      profile: nxos_template
      mgmt_ip: 10.20.90.9/24
      vlans:
      - {vlan_name: LegacyData, vlan_number: '5'}
      - {vlan_name: Voice, vlan_number: '20'}
      - {vlan_name: Data_Center, vlan_number: '50'}
      - {vlan_name: Vmotion, vlan_number: '51'}
      - {vlan_name: Citrix, vlan_number: '52'}
      - {vlan_name: Citrix_XEN, vlan_number: '56'}
      - {vlan_name: Citrix_Green_Infrastructure, vlan_number: '61'}
      - {vlan_name: Citrix_Red_Ingrastructure, vlan_number: '62'}
      - {vlan_name: Citrix_Green_XenApp, vlan_number: '63'}
      - {vlan_name: Citrix_Yellow, vlan_number: '64'}
      - {vlan_name: Citrix_Red_Desktops, vlan_number: '68'}
      - {vlan_name: Zerto-VRA-Network, vlan_number: '90'}
      - {vlan_name: Management, vlan_number: '92'}
      - {vlan_name: PCI_FW_Intermediate_Network, vlan_number: '121'}
      features:
        - nxapi
        - interface-vlan
        - hsrp
        - lacp
        - vpc
        - eigrp
        - npiv
        - fcoe
        - fport-channel-trunk
        - dhcp
        - fex
        - lldp

I would like to remove the vlans: dict to another vars file named vlans.yml. I figure I'd place that in the same directory as the current main.yml vars file and call the vlans.yml file out somewhere. I've tried to use the 'include_vars:' option but it won't take, specifically gives me an error when it is used within the same block as the 'template:' action.


Solution

  • Answering your question, yes. You can move those info without problems to a new file.

    Lets suppose you have a main.yml and vlans.yml vars file and this is your playbook as you posted:

    ---
    - name: Apply tor role   
      gather_facts: no   
      hosts: localhost
      roles:
        - tor
    

    This should be your file structure:

    $ tree roles/
    roles/
    └── tor
        ├── tasks
        │   └── main.yml
        └── vars
            ├── main.yml
            └── vlans.yml
    

    And for your role tor, you need to import that vlans.yml vars:

    - name: include vlans variable file
      include_vars: vars/vlans.yml
    
    - name: Generate Configs
      template: src={{ item.profile }}.j2 dest=staging/drc/{{ item.hostname }}/{{ item.hostname }}.cfg
      with_items: "{{ switches }}"