Bookmark this page

Guided Exercise: Creating a Network Automation Role

Create an Ansible Role that can back up the configuration of managed network nodes made by different vendors.

Outcomes

  • Create a role that backs up configuration files for Junos and IOS managed network nodes, and then stores those backup files in a separate location.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise. This command also creates a project directory with the files needed for the exercise.

[student@workstation ~]$ lab start simplify-roles

Instructions

  1. Open VS Code and switch to the Terminal tab, or open a GNOME terminal, and create the directory structure for a role named net_backup. The role must include the meta, tasks, and vars directories.

    Run the following commands to create the role and display the initial role structure and files:

    [student@workstation ~]$ cd /home/student/simplify-roles
    [student@workstation simplify-roles]$ mkdir roles; cd roles
    [student@workstation roles]$ ansible-galaxy init net_backup
    - Role net_backup was created successfully
    [student@workstation roles]$ rm -rvf \
    net_backup/{files,defaults,templates,handlers,tests}
    ...output omitted...
    [student@workstation roles]$ tree net_backup/
    net_backup/
    ├── README.md
    ├── meta
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    └── vars
        └── main.yml
    
    3 directories, 4 files
  2. Open the /home/student/simplify-roles directory in VS Code.

    • Create the roles/net_backup/tasks/junos_netconf_enable.yml task file to enable NETCONF on the Junos managed nodes.

    • Create the roles/net_backup/tasks/backup_junos.yml task file to back up the configurations on the Junos managed nodes.

    • Create the roles/net_backup/tasks/backup_ios.yml task file to back up the configurations on the IOS managed nodes.

    1. Open VS Code and then click FileOpen Folder.

    2. Navigate to Homesimplify-roles and click Open.

      Note

      If prompted, select Trust the authors of all files in the parent folder 'student', and then click Yes, I trust the authors.

    3. Create a task file named roles/net_backup/tasks/junos_netconf_enable.yml to enable NETCONF on the Junos managed nodes.

      The completed file must consist of the following content:

      ---
      - name: Enable the netconf service on port 830
        vars:
          ansible_connection: ansible.netcommon.network_cli
        junipernetworks.junos.junos_netconf:
          netconf_port: 830
          state: present
    4. Create a task file named roles/net_backup/tasks/backup_junos.yml to back up the configurations on the Junos managed nodes.

      The completed file must consist of the following content:

      ---
      - name: Backup Junos configurations
        junipernetworks.junos.junos_config:
          backup: true
          backup_options:
            dir_path: "{{ backup_path }}"
            filename: "{{ inventory_hostname }}.cfg"
    5. Create a task file roles/net_backup/tasks/backup_ios.yml to back up the configurations on the IOS managed nodes.

      The completed file must consist of the following content:

      ---
      - name: Backup IOS XE configurations
        cisco.ios.ios_config:
          backup: true
          backup_options:
            dir_path: "{{ backup_path }}"
            filename: "{{ inventory_hostname }}.cfg"
  3. Create a task file named roles/net_backup/tasks/junos.yml. Add two tasks to the file to include the junos_netconf_enable.yml and backup_junos.yml task files.

    The completed file must consist of the following content:

    ---
    - name: Enable NETCONF
      ansible.builtin.include_tasks:
        file: junos_netconf_enable.yml
    
    - name: Create Junos backup files
      ansible.builtin.include_tasks:
        file: backup_junos.yml
  4. Create the roles/net_backup/tasks/ios.yml file. Add a task to the file that includes the tasks/backup_ios.yml task file.

    The completed file must consist of the following content:

    ---
    - name: Create IOS backup files
      ansible.builtin.include_tasks:
        file: tasks/backup_ios.yml
  5. Define the role variable called backup_path in the roles/net_backup/vars/main.yml file. Set the backup_path variable to the backups value.

    The completed file must consist of the following content:

    ---
    # vars file for net_backup
    backup_path: backups
  6. Edit the roles/net_backup/tasks/main.yml task file to dynamically include the junos.yml and ios.yml task files.

    Use the tree command to display the final role structure and files.

    1. Edit the roles/net_backup/tasks/main.yml task file to dynamically include the junos.yml and ios.yml task files.

      The completed file must consist of the following content:

      ---
      # tasks file for net_backup
      - name: Backup network device configs
        ansible.builtin.include_tasks:
          file: "{{ ansible_network_os.split('.')[2] }}.yml"
    2. Use the tree command to display the final role structure and files:

      [student@workstation simplify-roles]$ tree roles/net_backup
      roles/net_backup
      ├── README.md
      ├── meta
      │   └── main.yml
      ├── tasks
      │   ├── backup_ios.yml
      │   ├── backup_junos.yml
      │   ├── ios.yml
      │   ├── junos.yml
      │   ├── junos_netconf_enable.yml
      │   └── main.yml
      └── vars
          └── main.yml
      
      3 directories, 9 files
  7. Edit the roles/net_backup/meta/main.yml file to document your role. Add your name, a role description, and, optionally, your company name:

    galaxy_info:
      author: your name
      description: your role description
      company: your company (optional)
    ...output omitted...

    Note

    Normally, you should also modify the README.md file to document your role.

    That step is skipped in this exercise to save time.

  8. Create a playbook named net_backup.yml that uses the net_backup role, and then run the playbook.

    1. Create a playbook in the /home/student/simplify-roles directory named net_backup.yml that uses the net_backup role. Add the following content to the playbook:

      ---
      - name: Back up managed network node configurations
        hosts:
          - junos
          - ios
        gather_facts: false
        tasks:
          - name: Backup network managed node configurations
            ansible.builtin.include_role:
              name: net_backup
            when: ansible_network_os is defined
    2. Run the net_backup.yml playbook:

      [student@workstation simplify-roles]$ ansible-navigator run net_backup.yml
      ...output omitted...
  9. Display the contents of some files generated by the playbook to verify that it was successful, or review them in VS Code.

    [student@workstation simplify-roles]$ head backups/iosxe1.lab.example.com.cfg
    ...output omitted...
    [student@workstation simplify-roles]$ head backups/junos2.lab.example.com.cfg
    ...output omitted...
  10. Click FileClose Folder in VS Code to close the /home/student/simplify-roles directory, or run the cd command in a GNOME terminal to return to the student home directory:

    [student@workstation simplify-roles]$ cd

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish simplify-roles

Revision: do457-2.3-7cfa22a