Bookmark this page

Guided Exercise: Configuring a Multi-vendor Network

Include and import playbooks and tasks for different network vendors in a top-level Ansible Playbook.

Outcomes

  • Create a playbook that uses conditionals to select the appropriate task file.

  • Create task files to encourage reusing code.

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-files

Instructions

  1. Open the /home/student/simplify-files directory in VS Code and create the backup.yml playbook to create backup files of the managed nodes in the junos and ios inventory groups.

    1. Open VS Code and then click FileOpen Folder.

    2. Navigate to Homesimplify-files 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 the backup.yml file and target the managed nodes in the junos and ios inventory groups. Do not gather facts for the managed nodes. Add the following lines to the playbook:

      ---
      - name: Back up all managed network nodes
        hosts:
          - junos
          - ios
        gather_facts: false 1
        tasks:

      1

      Fact gathering fails for the Juniper Junos managed nodes unless the NETCONF service is running on those nodes.

    4. Add a block to the bottom of the play that targets the Juniper Junos managed nodes. Add two tasks to include the specified task files:

      ---
      - name: Back up all managed network nodes
        hosts:
          - junos
          - ios
        gather_facts: false
        tasks:
          - name: Juniper Junos block
            when: ansible_network_os == 'junipernetworks.junos.junos'
            block:
              - name: Enable NETCONF
                ansible.builtin.include_tasks: tasks/junos_netconf_enable.yml 1
      
              - name: Create Junos backup files
                ansible.builtin.include_tasks: tasks/junos_backup_create.yml 2

      1

      Most tasks for the Juniper Junos managed nodes require that the NETCONF service is running. This task file enables the NETCONF service.

      2

      This task file performs the necessary steps to create the backup files.

  2. Create the tasks directory and the task files specified in the backup.yml playbook.

    1. Switch to the Terminal tab in VS Code, or change to the /home/student/simplify-files directory in a GNOME terminal:

      [student@workstation ~] cd ~/simplify-files
    2. Use the mkdir command to create the tasks directory:

      [student@workstation simplify-files]$ mkdir tasks
    3. Create the tasks/junos_netconf_enable.yml task file with the following content:

      ---
      - name: Enable NETCONF service
        vars:
          ansible_connection: ansible.netcommon.network_cli
        junipernetworks.junos.junos_netconf:
          netconf_port: 830
          state: present
    4. Create the tasks/junos_backup_create.yml task file with the following content:

      ---
      - name: Create the backup file
        junipernetworks.junos.junos_config: 1
          backup: true
        register: create_file
      
      - name: Show create_file
        ansible.builtin.debug:
          var: create_file 2

      1

      This task uses the junipernetworks.junos.junos_config module with the default values for the backup_options parameter. These values include the directory where the module stores the backups and the name of each backup file.

      2

      If you register the output of the Create the backup file task, then you can display the registered variable using the ansible.builtin.debug module. Ultimately, this task file must create symbolic links that point to the latest backup file for each managed node.

  3. Run the backup.yml playbook. Using the information displayed in the Show create_file task, update the tasks/junos_backp_create.yml task file to create a symbolic link that points to the latest backup file.

    1. Run the backup.yml playbook and examine the output of the Show create_file task:

      [student@workstation simplify-files]$ ansible-navigator run backup.yml
      ...output omitted...
      TASK [Show create_file] ********************************************************
      ok: [junos1.lab.example.com] => {
          "create_file": {
              "backup_path": "/home/student/simplify-files/backup/junos1.lab.example.com_config.2023-08-16@14:12:46", 1
              "changed": true,
              "date": "2023-08-16",
              "failed": false,
              "filename": "junos1.lab.example.com_config.2023-08-16@14:12:46", 2
              "shortname": "/home/student/simplify-files/backup/junos1.lab.example.com_config", 3
              "time": "14:12:46"
          }
      }
      ok: [junos2.lab.example.com] => {
          "create_file": {
              "backup_path": "/home/student/simplify-files/backup/junos2.lab.example.com_config.2023-08-16@14:12:46",
              "changed": true,
              "date": "2023-08-16",
              "failed": false,
              "filename": "junos2.lab.example.com_config.2023-08-16@14:12:46",
              "shortname": "/home/student/simplify-files/backup/junos2.lab.example.com_config",
              "time": "14:12:46"
          }
      }
      ...output omitted...

      1

      The value of the create_file['backup_path'] variable contains the absolute path to the created backup file.

      2

      The value of the create_file['filename'] variable contains the name of the created backup file.

      3

      The value of the create_file['shortname'] variable matches the value of the create_file['backup_path'] variable without the date and time stamp.

    2. Update the tasks/junos_backup_create.yml task file. Add a task that creates a symbolic link to the most recent backup file. You might also update the Show create_file task so that the task is skipped unless you run the playbook in verbose mode:

      ---
      - name: Create the backup file
        junipernetworks.junos.junos_config:
          backup: true
        register: create_file
      
      - name: Show create_file
        ansible.builtin.debug:
          var: create_file
          verbosity: 1 1
      
      - name: Create or update the 'latest' link
        ansible.builtin.file:
          path: "{{ create_file['shortname'] }}.latest" 2
          src: "{{ create_file['filename'] }}" 3
          state: link 4
          force: true 5

      1

      Skip this task unless you run the playbook with at least one -v option.

      2

      Create the symbolic link by appending .latest to the value of the create_file['shortname'] variable.

      3

      The task creates a symbolic link that uses a relative path to a file in the same directory as the link. (Optional) Use the value of the create_file['backup_path'] variable to create a symbolic link that uses an absolute path.

      4

      Create a symbolic link.

      5

      Because this example uses a relative path, you must force the creation of the symbolic link.

    3. Run the backup.yml playbook:

      [student@workstation simplify-files]$ ansible-navigator run backup.yml
      ...output omitted...
      TASK [Create or update the 'latest' link] **************************************
      changed: [junos2.lab.example.com]
      changed: [junos1.lab.example.com]
      
      PLAY RECAP *********************************************************************
      iosxe1.lab.example.com     : ok=0    changed=0    unreachable=0    failed=0  ...
      iosxe2.lab.example.com     : ok=0    changed=0    unreachable=0    failed=0  ...
      junos1.lab.example.com     : ok=6    changed=2    unreachable=0    failed=0  ...
      junos2.lab.example.com     : ok=6    changed=2    unreachable=0    failed=0  ...
    4. Use the tree command to verify the creation of the symbolic links. The date and time stamps displayed in this example differ from your output.

      [student@workstation simplify-files]$ tree backup
      backup
      ├── junos1.lab.example.com_config.2023-08-16@14:12:46
      ├── junos1.lab.example.com_config.2023-08-16@14:16:16
      ├── junos1.lab.example.com_config.latest -> junos1.lab.example.com_config.2023-08-16@14:16:16
      ├── junos2.lab.example.com_config.2023-08-16@14:12:46
      ├── junos2.lab.example.com_config.2023-08-16@14:16:17
      └── junos2.lab.example.com_config.latest -> junos2.lab.example.com_config.2023-08-16@14:16:17
      
      0 directories, 6 files
  4. Update the backup.yml playbook so that it creates backup files for the Cisco IOS managed nodes.

    1. Add a block to the bottom of the play that targets the Cisco IOS managed nodes. Add a task to include the tasks/ios_backup_create.yml task file.

      The finished playbook contains the following content:

      ---
      - name: Back up all managed network nodes
        hosts:
          - junos
          - ios
        gather_facts: false
        tasks:
          - name: Juniper Junos block
            when: ansible_network_os == 'junipernetworks.junos.junos'
            block:
              - name: Enable NETCONF
                ansible.builtin.include_tasks: tasks/junos_netconf_enable.yml
      
              - name: Create Junos backup files
                ansible.builtin.include_tasks: tasks/junos_backup_create.yml
      
          - name: Cicso IOS block
            when: ansible_network_os == 'cisco.ios.ios'
            block: 1
              - name: Create IOS backup files
                ansible.builtin.include_tasks: tasks/ios_backup_create.yml 2

      1

      Although this playbook only has one task for the Cisco IOS managed nodes, you might still use a block in case you decide to add more tasks.

      2

      This task file is almost the same as the tasks/junos_backup_create.yml task file.

      Note

      Because the playbook does not perform loops or pass variables to the task files, you could use the ansible.builtin.import_tasks module instead of the ansible.builtin.include_tasks module.

    2. Use VS Code or a GNOME terminal to copy the tasks/junos_backup_create.yml task file to the tasks/ios_backup_create.yml task file:

      [student@workstation simplify-files]$ cp tasks/junos_backup_create.yml \
      tasks/ios_backup_create.yml
    3. Update the tasks/ios_backup_create.yml task file so that it uses the cisco.ios.ios_config module instead of the junipernetworks.junos.junos_config module. The updated task file contains the following content:

      ---
      - name: Create the backup file
        cisco.ios.ios_config:
          backup: true
        register: create_file
      
      - name: Show create_file
        ansible.builtin.debug:
          var: create_file
          verbosity: 1
      
      - name: Create or update the 'latest' link
        ansible.builtin.file:
          path: "{{ create_file['shortname'] }}.latest"
          src: "{{ create_file['filename'] }}"
          state: link
          force: true
  5. Run the backup.yml playbook and verify that the playbook creates symbolic links for both the Juniper Junos and Cisco IOS managed nodes.

    1. Run the backup.yml playbook:

      [student@workstation simplify-files]$ ansible-navigator run backup.yml
      ...output omitted...
    2. Use the tree command to verify the creation of the symbolic links. The date and time stamps displayed in this example differ from your output. Notice that even though the Junos managed nodes have three backup files each, the symbolic links point to the most recent files.

      [student@workstation simplify-files]$ tree backup
      backup
      ├── iosxe1.lab.example.com_config.2023-08-16@14:21:56
      ├── iosxe1.lab.example.com_config.latest -> iosxe1.lab.example.com_config.2023-08-16@14:21:56
      ├── iosxe2.lab.example.com_config.2023-08-16@14:21:56
      ├── iosxe2.lab.example.com_config.latest -> iosxe2.lab.example.com_config.2023-08-16@14:21:56
      ├── junos1.lab.example.com_config.2023-08-16@14:12:46
      ├── junos1.lab.example.com_config.2023-08-16@14:16:16
      ├── junos1.lab.example.com_config.2023-08-16@14:21:51
      ├── junos1.lab.example.com_config.latest -> junos1.lab.example.com_config.2023-08-16@14:21:51
      ├── junos2.lab.example.com_config.2023-08-16@14:12:46
      ├── junos2.lab.example.com_config.2023-08-16@14:16:17
      ├── junos2.lab.example.com_config.2023-08-16@14:21:51
      └── junos2.lab.example.com_config.latest -> junos2.lab.example.com_config.2023-08-16@14:21:51
      
      0 directories, 12 files
  6. Close the /home/student/simplify-files directory in VS Code, or run the cd command in a GNOME terminal to return to the student home directory.

    [student@workstation simplify-files]$ 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-files

Revision: do457-2.3-7cfa22a