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.
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'" spine02spine02 | SUCCESS => { "changed": false, "stdout": [ "vyos" ], "stdout_lines": [ [ "vyos" ] ] }
Add a variable and its value to support template-based configuration.
Create the host_vars/spine02/ directory if it does not already exist.
[student@workstation proj]$mkdir -p host_vars/spine02
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.ymlnew_hostname: spineX1
Create a Jinja2 template that builds configuration statements.
Create a templates directory named j2/ if one does not already exist.
[student@workstation proj]$mkdir -p j2
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.j2set system host-name {{ new_hostname }}
Perform a play that configures a device from a Jinja2 template.
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 }}"
Check the new playbook's syntax.
[student@workstation proj]$ansible-playbook --syntax-check j2cfg-spine02.yml
Perform the play in j2cfg-spine02.yml.
[student@workstation proj]$ansible-playbook j2cfg-spine02.yml
Confirm that the change occurred as desired, then change the host name to its original value.
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'" spine02spine02 | SUCCESS => { "changed": false, "stdout": [ "spineX1" ], "stdout_lines": [ [ "spineX1" ] ] }
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" spine02spine02 | SUCCESS => { "changed": true, "commands": [ "set system host-name 'vyos'" ] }
This concludes the guided exercise.