Bookmark this page

Guided Exercise: Configuring Settings From Templates

This guided exercise illustrates how you can start with a simple Jinja2 template and add onto that to compose more complicated sequences of configuration statements. In the previous exercise, you set the local host name of a device. In this exercise, you add configuration statements that set the domain name and name servers.

In this exercise, you will configure system settings for network devices from templates.

Outcomes

You should be able to:

  • Set variables or confirm that variables are already set to support template-based configuration.

  • Create or update Jinja2 templates that build configuration statements.

  • Perform a multivendor play that configures devices from Jinja2 templates.

Open a terminal window on the workstation VM and change to the ~/proj/ directory.

  1. Set variables or confirm that variables are already set to support template-based configuration. Verify that the domain_name and nameservers variables are set as indicated below in the group_vars/network/vars.yml file.

    [student@workstation proj]$ cat group_vars/network/vars.yml
    ansible_connection: network_cli
    domain_name: lab.example.com
    nameservers:
    - 8.8.8.8
    - 8.8.4.4
  2. Create or update Jinja2 templates that build configuration statements.

    1. Create a Jinja2 template named j2/vyos-config.j2 that contains the following:

      set system host-name {{ inventory_hostname }}
      set system domain-name {{ domain_name }}
      {% for nameserver in nameservers %}
      set system name-server {{ nameserver }}
      {% endfor %}
    2. Create a Jinja2 template named j2/ios-config.j2 that contains the following:

      hostname {{ inventory_hostname }}
      ip domain-name {{ domain_name }}
      {% for nameserver in nameservers %}
      ip name-server {{ nameserver }}
      {% endfor %}
  3. Perform a multivendor play that configures devices from Jinja2 templates.

    1. Compose a multivendor playbook named j2cfg.yml that sources configuration statements from the vendor-specific Jinja2 templates you created. The names of the template files were made variables here to put them at the top of the play to make it easy to change the values in case they are ever renamed or moved. This also makes it possible to reuse the text of the task blocks in other playbooks.

      [student@workstation proj]$ cat j2cfg.yml
      ---
      - name: configure devices using j2 templates
        hosts: network
        vars:
          vyos_template: j2/vyos-config.j2
          ios_template: j2/ios-config.j2
      
        tasks:
      
        - name: configure {{ inventory_hostname }}
          vyos_config:
            src: "{{ vyos_template }}"
          when: ansible_network_os == 'vyos'
      
        - name: configure {{ inventory_hostname }}
          ios_config:
            src: "{{ ios_template }}"
          when: ansible_network_os == 'ios'
    2. Perform the play found in your new playbook, limiting the change to spine01.

      [student@workstation proj]$ ansible-playbook -l spine01 j2cfg.yml
    3. Execute ad hoc commands or a play from an existing playbook to verify that the result is as desired.

      [student@workstation proj]$ ansible-playbook -l spine01 \
      > multi-vendor-host-dnsinfo1.yml
      
      
      PLAY [multi-vendor play to display host name, domain name, nameservers] ********
      
      TASK [ios host, domain, nameserver] ********************************************
      skipping: [spine01]
      
      TASK [display ios host settings] ***********************************************
      skipping: [spine01]
      
      TASK [vyos host, domain, nameserver] *******************************************
      ok: [spine01]
      
      TASK [display vyos host settings] **********************************************
      ok: [spine01] => {
          "result.stdout": [
              "spine01",
              "lab.example.com",
              "name-server 8.8.8.8\n    name-server 8.8.4.4"
          ]
      }
      
      PLAY RECAP *********************************************************************
      spine01                    : ok=2    changed=0    unreachable=0    failed=0

This concludes the guided exercise.

Revision: do457-2.5-4693601