Bookmark this page

Guided Exercise: Configuring From Templates

In this exercise, you will configure a network device using a Jinja2 template.

Outcomes

You should be able to:

  • View the host name using an ad hoc command.

  • Add a variable and its value to support template-based configuration.

  • Create a Jinja2 template that builds configuration statements.

  • Perform a play that configures a device from a Jinja2 template.

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

  1. Execute an ad hoc command that displays the host name of VyOS device spine02.

    [student@workstation proj]$ ansible -m vyos_command \
    > -a "commands='sh host name'" spine02
    spine02 | SUCCESS => {
        "changed": false,
        "stdout": [
            "vyos"
        ],
        "stdout_lines": [
            [
                "vyos"
            ]
        ]
    }
  2. Add a variable and its value to support template-based configuration.

    1. Create the host_vars/spine02/ directory if it does not already exist.

      [student@workstation proj]$ mkdir -p host_vars/spine02
    2. Create a host variables file named host_vars/spine02/vars.yml. This file should set a variable named new_hostname to the value spineX1.

      [student@workstation proj]$ cat host_vars/spine02/vars.yml
      new_hostname: spineX1
  3. Create a Jinja2 template that builds configuration statements.

    1. Create a templates directory named j2/ if one does not already exist.

      [student@workstation proj]$ mkdir -p j2
    2. Create a Jinja2 template file named j2/vyos-configure.j2. This file should map variables to configuration statements.

      [student@workstation proj]$ cat j2/vyos-configure.j2
      set system host-name {{ new_hostname }}
  4. Perform a play that configures a device from a Jinja2 template.

    1. Compose a playbook named j2cfg-spine02.yml. It should contain a single task that uses the vyos_config module. It should source the configuration lines used by vyos_config from your Jinja2 template.

      [student@workstation proj]$ cat j2cfg-spine02.yml
      ---
      - name: play that configures spine02 using j2 template
        hosts: spine02
        vars:
          j2_template: j2/vyos-configure.j2
      
        tasks:
      
        - name: configure {{ inventory_hostname }}
          vyos_config:
            src: "{{ j2_template }}"
    2. Check the new playbook's syntax.

      [student@workstation proj]$ ansible-playbook --syntax-check j2cfg-spine02.yml
    3. Perform the play in j2cfg-spine02.yml.

      [student@workstation proj]$ ansible-playbook j2cfg-spine02.yml
  5. Confirm that the change occurred as desired, then change the host name to its original value.

    1. Execute an ad hoc command to confirm that the change occurred as expected.

      [student@workstation proj]$ ansible -m vyos_command \
      > -a "commands='sh host name'" spine02
      spine02 | SUCCESS => {
          "changed": false,
          "stdout": [
              "spineX1"
          ],
          "stdout_lines": [
              [
                  "spineX1"
              ]
          ]
      }
    2. Execute an ad hoc command to change the host name to its original value.

      [student@workstation proj]$ ansible -m vyos_system -a "host_name=vyos" spine02
      spine02 | SUCCESS => {
          "changed": true,
          "commands": [
              "set system host-name 'vyos'"
          ]
      }

This concludes the guided exercise.

Revision: do457-2.5-4693601