Bookmark this page

Lab: Automating Simple Network Operations

The process of configuring network devices is illustrated with simple changes in this lab.

In this lab, you will automate simple operations on network devices.

Outcomes

You should be able to:

  • Consult local (ansible-doc) or web-based documentation to determine correct configuration statements to use with a given os_config module.

  • Configure devices using statements embedded in a playbook.

  • Configure devices by sourcing statements from a file.

  • Generate configuration files using Jinja2 and apply the changes to devices.

Open a terminal window on the workstation VM.

  1. Create a playbook named rename-spine01.yml. Use os_config statements in the playbook to change the local host name of spine01 on the network device to spineA1. Do not forget to use the os_config module that matches the device. Include a task that backs up the original configuration before the change is applied.

    [student@workstation proj]$ cat rename-spine01.yml
    ---
    - name: Lab 5, spine01 => spineA1
      hosts: spine01
      vars:
        new_hostname: spineA1
    
      tasks:
    
      - name: change the hostname
        vyos_config:
          backup: yes
          lines: set system host-name {{ new_hostname }}
  2. Perform the play in the rename-spine01.yml playbook.

    [student@workstation proj]$ ansible-playbook rename-spine01.yml
  3. Execute an ad hoc command to confirm that the host name of spine01 is now spineA1. Also verify that the original device configuration was backed up.

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

    [student@workstation proj]$ ansible -m vyos_config \
    > -a "lines='set system host-name spine01'" spine01
  5. Create a directory named config/.

    [student@workstation proj]$ mkdir config
  6. Create a static configuration file named config/spine02.cfg. Put in this file this VyOS configuration statement that sets the host name to spineB2: set system host-name spineB2.

    [student@workstation proj]$ cat config/spine02.cfg
    set system host-name spineB2
  7. Create a playbook named rename-spine02.yml. Include in this playbook a task that sources configuration statements from the config/spine02.cfg file.

    [student@workstation proj]$ cat rename-spine02.yml
    ---
    - name: Lab 5, spine02 => spineB2
      hosts: spine02
      vars:
        config_filepath: ./config
    
      tasks:
    
      - name: change the hostname
        vyos_config:
          src: "{{ config_filepath }}/{{ inventory_hostname }}.cfg"
          backup: yes
  8. Perform the play in the rename-spine02.yml playbook. If you receive an error regarding the file path, make sure you have placed spine02.cfg into the coding directory.

    [student@workstation proj]$ ansible-playbook rename-spine02.yml
  9. Execute an ad hoc command to confirm that the host name of spine02 is now spineB2.

    [student@workstation proj]$ ansible -m vyos_command \
    > -a "commands='sh host name'" spine02
  10. Execute an ad hoc command to change the host name back to spine02.

    [student@workstation proj]$ ansible -m vyos_config \
    > -a "lines='set system host-name spine02'" spine02
  11. Create the file host_vars/cs01/vars.yml if it does not already exist. Add a YAML mapping to the host_vars/cs01/vars.yml file that maps the variable name new_hostname to the value csA1.

    [student@workstation proj]$ cat host_vars/cs01/vars.yml
    new_hostname: csA1
  12. Create a Jinja2 template named ios-cs-hostname.j2 that consists of the IOS command to set the host name. When setting the host name with Ansible, it makes sense to use the Ansible magic variable inventory_hostname, but for the purpose of this exercise, use the variable new_hostname instead. The IOS command to set the host name is hostname hostname.

    [student@workstation proj]$ cat j2/ios-cs-hostname.j2
    hostname {{ new_hostname }}
  13. Create a playbook named rename-cs01.yml. The play in this playbook should contain a task that uses the ios_config module, in which the lines used by ios_config are sourced from the ios-cs-hostname.j2 template.

    [student@workstation proj]$ cat rename-cs01.yml
    ---
    - name: a play that uses a Jinja2 template and host vars to rename cs01
      hosts: cs01
    
      tasks:
    
      - name: configure host
        ios_config:
          src: j2/ios-cs-hostname.j2
  14. Perform the play in the rename-cs01.yml playbook.

    [student@workstation proj]$ ansible-playbook rename-cs01.yml
  15. Execute an ad hoc command and confirm that the host name of cs01 is now csA1.

    [student@workstation proj]$ ansible -m ios_command \
    > -a "commands='sh run | include hostname'" cs01
    cs01 | SUCCESS => {
        "changed": false,
        "stdout": [
            "hostname csA1"
        ],
        "stdout_lines": [
            [
                "hostname csA1"
            ]
        ]
    }
  16. Execute an ad hoc command to change the host name back to cs01.

    [student@workstation proj]$ ansible -m ios_config -a "lines='hostname cs01'" cs01

This concludes the lab.

Revision: do457-2.5-4693601