Bookmark this page

Guided Exercise: Making Flexible Playbooks by Using Variables

Rewrite an Ansible Playbook to customize and configure settings for specific managed nodes by using variables to set parameters that are passed to modules used by tasks in its plays.

Outcomes

  • Create variable files for each inventory group of managed nodes under the groups_var subdirectory in the project directory.

  • Separate variables from your inventory to the new variable files.

  • Create new variables to be used in the tasks of the current playbooks.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise, and to ensure that all required resources are available. This command also creates a project directory with the files needed for the exercise.

[student@workstation ~]$ lab start manage-variables

Instructions

You want to improve the manage-variables project to make it easier to find group-related variables in the project. To do this, you need to relocate inventory variables and create new variables to use in the current playbooks.

  1. Open the /home/student/manage-variables directory in VS Code. Locate the playbooks and the inventory groups in the inventory file for the manage-variables project.

    1. Open VS Code and navigate to FileOpen Folder.

    2. Navigate to Homemanage-variables 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. Notice that the project has two playbooks to configure the Domain Name System (DNS) service and two playbooks to configure the Simple Network Management Protocol (SNMP):

      • The ios_dns.yml playbook configures DNS on the IOS managed nodes.

      • The junos_dns.yml playbook configures DNS on the Juniper Junos managed nodes.

      • The ios_snmp.yml playbook configures SNMP on the IOS managed nodes.

      • The junos_snmp.yml playbooks configures SNMP on the Juniper Junos managed nodes.

    4. Click the inventory file. The inventory file has the ios and junos inventory groups and group variables.

      [ios]
      iosxe[1:2].lab.example.com
      
      [junos]
      junos[1:2].lab.example.com
      
      [ios:vars]
      ansible_user=student
      ansible_ssh_private_key_file=~/.ssh/lab_rsa
      ansible_connection=ansible.netcommon.network_cli
      ansible_network_os=cisco.ios.ios
      
      [junos:vars]
      ansible_user=student
      ansible_ssh_private_key_file=~/.ssh/lab_rsa
      ansible_connection=ansible.netcommon.netconf
      ansible_network_os=junipernetworks.junos.junos
  2. Create the directory structure to hold the variables for the inventory groups.

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

      [student@workstation ~]$ cd manage-variables
      [student@workstation manage-variables]$
    2. Create the group_vars/all, group_vars/ios, and group_vars/junos directories to hold the variable files for the managed nodes:

      [student@workstation manage-variables]$ mkdir -p group_vars/all
      [student@workstation manage-variables]$ mkdir -p group_vars/ios
      [student@workstation manage-variables]$ mkdir -p group_vars/junos
    3. (Optional) Verify the directories in the group_vars directory by using the tree command:

      [student@workstation manage-variables]$ tree group_vars/
      group_vars/
      ├── all
      ├── ios
      └── junos
      
      3 directories, 0 files
  3. Create the following files under the groups_vars directory to store variables from the inventory file. Update the inventory file by removing the variables already defined.

    • The all/common.yml file to store the common variables for the managed nodes.

    • The ios/connection.yml file to store the ios inventory group variables.

    • The junos/connection.yml file to store the junos inventory group variables.

    1. Notice that the inventory file holds the following common variables for the ios and junos inventory groups:

      VariableValue
      ansible_user student
      ansible_ssh_private_key_file ~/.ssh/lab_rsa
    2. Create the group_vars/all/common.yml file with the following content:

      ---
      ansible_user: student
      ansible_ssh_private_key_file: ~/.ssh/lab_rsa
    3. Notice that the inventory file holds the following variables used for connecting to the managed nodes:

      VariableValue for the ios inventory group
      ansible_connection ansible.netcommon.network_cli
      ansible_network_os cisco.ios.ios
      VariableValue for the junos inventory group
      ansible_connection ansible.netcommon.netconf
      ansible_network_os junipernetworks.junos.junos
    4. Create the group_vars/ios/connection.yml file with the following content:

      ---
      ansible_connection: ansible.netcommon.network_cli
      ansible_network_os: cisco.ios.ios
    5. Create the group_vars/junos/connection.yml file with the following content:

      ---
      ansible_connection: ansible.netcommon.netconf
      ansible_network_os: junipernetworks.junos.junos
    6. (Optional) Verify the directories and files in the group_vars directory by using the tree command:

      [student@workstation manage-variables]$ tree group_vars/
      group_vars
      ├── all
      │   └── common.yml
      ├── ios
      │   └── connection.yml
      └── junos
          └── connection.yml
      
      3 directories, 3 files
    7. Update the inventory file by removing the IOS and Juniper Junos variables. The resulting inventory file should consist of the following content:

      [ios]
      iosxe[1:2].lab.example.com
      
      [junos]
      junos[1:2].lab.example.com
  4. Make the ios_dns.yml and junos_dns.yml playbooks more flexible by using variables in their Configure DNS settings task.

    1. In VS Code, click the ios_dns.yml playbook. Pay attention to the parameters used by the cisco.ios.ios_system module in the Configure DNS settings task:

      ...output omitted...
          - name: Configure DNS settings
            cisco.ios.ios_system:
              domain_search:
                - lab.example.com
                - example.com
              name_servers:
                - 172.25.250.220
    2. Click the junos_dns.yml playbook. Pay attention to the parameters used by the junipernetworks.junos.junos_system module in the Configure DNS settings task:

      ...output omitted...
          - name: Configure DNS settings
            junipernetworks.junos.junos_system:
              domain_search:
                - lab.example.com
                - example.com
              name_servers:
                - 172.25.250.220
    3. Create the group_vars/ios/dns.yml and group_vars/junos/dns.yml variables files with the following content:

      ---
      dns_domains:
       - lab.example.com
       - example.com
      dns_name_servers:
       - 172.25.250.220

      Note

      Because the new dns_domains and dns_name_servers variables are the same for both ios and junos inventory groups, another option is to define the variables in a file in the group_vars/all/ directory.

      Both options are valid. The choice depends on the organization that you want to implement in your company.

    4. Edit the ios_dns.yml and junos_dns.yml playbooks to introduce the defined variables. Place the variable names in double braces and use quotes for both variable expressions.

      • The Configure DNS settings task for the ios_dns.yml playbook must have the following content:

        - name: Configure DNS settings
          cisco.ios.ios_system:
            domain_search: "{{ dns_domains }}"
            name_servers: "{{ dns_name_servers }}"
      • The Configure DNS settings task for the junos_dns.yml playbook must have the following content:

        - name: Configure DNS settings
          junipernetworks.junos.junos_system:
            domain_search: "{{ dns_domains }}"
            name_servers: "{{ dns_name_servers }}"
    5. (Optional) Verify the directories and files in the group_vars directory by using the tree command:

      [student@workstation manage-variables]$ tree group_vars/
      group_vars
      ├── all
      │   └── common.yml
      ├── ios
      │   ├── connection.yml
      │   └── dns.yml
      └── junos
          ├── connection.yml
          └── dns.yml
      
      3 directories, 5 files
  5. Make the ios_snmp.yml and junos_snmp.yml playbooks more flexible by using variables in their Configure SNMP settings task.

    1. In VS Code, click the ios_snmp.yml playbook. Pay attention to the parameters used by the cisco.ios.ios_snmp_server module in the Configure SNMP settings task:

      ...output omitted...
          - name: Configure SNMP settings
            cisco.ios.ios_snmp_server:
              state: merged
              config:
                location: 'Raleigh, NC'
                contact: 'Network Engineering | neteng@company.com'
                communities:
                  - name: rocommunity2n4g!
                    ro: true
                  - name: rwcommunityd7g$v
                    rw: true
    2. Click the junos_dns.yml playbook. Pay attention to the parameters used by the junipernetworks.junos.junos_system module in the Configure DNS settings task:

      ...output omitted...
          - name: Configure SNMP settings
            junipernetworks.junos.junos_snmp_server:
              state: merged
              config:
                location: 'Raleigh, NC'
                contact: 'Network Engineering | neteng@company.com'
                communities:
                  - name: rocommunity2n4g!
                    authorization: read-only
                  - name: rwcommunityd7g$v
                    authorization: read-write
    3. Create the group_vars/ios/snmp.yml variables file with the following content:

      ---
      snmp_state: merged
      snmp_location: 'Raleigh, NC'
      snmp_contact: 'Network Engineering | neteng@company.com'
      snmp_communities:
        - name: rocommunity2n4g!
          ro: true
        - name: rwcommunityd7g$v
          rw: true
    4. Create the group_vars/junos/snmp.yml variables file with the following content:

      ---
      snmp_state: merged
      snmp_location: 'Raleigh, NC'
      snmp_contact: 'Network Engineering | neteng@company.com'
      snmp_communities:
        - name: rocommunity2n4g!
          authorization: read-only
        - name: rwcommunityd7g$v
          authorization: read-write
    5. Edit the ios_snmp.yml and junos_snmp.yml playbooks to introduce the defined variables. Place the variable names in double braces and use quotes for both variable expressions.

      • The Configure SNMP settings task for the ios_snmp.yml must have the following content:

            - name: Configure SNMP settings
              cisco.ios.ios_snmp_server:
                state: "{{ snmp_state }}"
                config:
                  location: "{{ snmp_location }}"
                  contact: "{{ snmp_contact }}"
                  communities: "{{ snmp_communities }}"
      • The Configure SNMP settings task for the junos_snmp.yml must have the following content:

            - name: Configure SNMP settings
              junipernetworks.junos.junos_snmp_server:
                state: "{{ snmp_state }}"
                config:
                  location: "{{ snmp_location }}"
                  contact: "{{ snmp_contact }}"
                  communities: "{{ snmp_communities }}"
    6. (Optional) Verify the directories and files in the group_vars directory by using the tree command:

      [student@workstation manage-variables]$ tree group_vars/
      group_vars/
      ├── all
      │   └── common.yml
      ├── ios
      │   ├── connection.yml
      │   ├── dns.yml
      │   └── snmp.yml
      └── junos
          ├── connection.yml
          ├── dns.yml
          └── snmp.yml
      
      3 directories, 7 files
  6. The rewritten playbooks perform the DNS and SNMP configuration tasks on the managed nodes by using variables defined in files under the groups_var directory. The group variable files have meaningful names, making it easier to find particular variables.

    Verify that the Configure DNS settings and Configure SNMP settings tasks run successfully when you run the ios_dns.yml, junos_dns.yml, ios_snmp.yml, and junos_snmp.yml playbooks.

    1. Run the ios_dns.yml playbook. The playbook runs successfully.

      [student@workstation manage-variables]$ ansible-navigator run ios_dns.yml
      
      PLAY [Configure DNS settings on IOS managed nodes] *******************************
      
      TASK [Configure DNS settings] ****************************************************
      changed: [iosxe1.lab.example.com]
      changed: [iosxe2.lab.example.com]
      
      PLAY RECAP ***********************************************************************
      iosxe1.lab.example.com    : ok=1   changed=1   unreachable=0   failed=0      ...
      iosxe2.lab.example.com    : ok=1   changed=1   unreachable=0   failed=0      ...
    2. Run the junos_dns.yml playbook. The playbook runs successfully.

      [student@workstation manage-variables]$ ansible-navigator run junos_dns.yml
      
      PLAY [Configure DNS settings on Juniper managed nodes] ***************************
      
      TASK [Enable netconf service on port 830] ****************************************
      changed: [junos2.lab.example.com]
      changed: [junos1.lab.example.com]
      
      TASK [Configure DNS settings] ****************************************************
      changed: [junos1.lab.example.com]
      changed: [junos2.lab.example.com]
      
      PLAY RECAP ***********************************************************************
      junos1.lab.example.com    : ok=2   changed=2   unreachable=0   failed=0      ...
      junos2.lab.example.com    : ok=2   changed=2   unreachable=0   failed=0      ...
    3. Run the ios_snmp.yml playbook. The playbook runs successfully.

      [student@workstation manage-variables]$ ansible-navigator run ios_snmp.yml
      
      PLAY [Configure SNMP on Cisco IOS XE managed nodes] ******************************
      
      TASK [Configure SNMP settings] ***************************************************
      changed: [iosxe1.lab.example.com]
      changed: [iosxe2.lab.example.com]
      
      PLAY RECAP ***********************************************************************
      iosxe1.lab.example.com    : ok=1   changed=1   unreachable=0   failed=0      ...
      iosxe2.lab.example.com    : ok=1   changed=1   unreachable=0   failed=0      ...
    4. Run the junos_snmp.yml playbook. The playbook runs successfully.

      [student@workstation manage-variables]$ ansible-navigator run junos_snmp.yml
      
      PLAY [Configure SNMP on Juniper managed nodes] ***********************************
      
      TASK [Enable netconf service on port 830] ****************************************
      ok: [junos2.lab.example.com]
      ok: [junos1.lab.example.com]
      
      TASK [Configure SNMP settings] ***************************************************
      changed: [junos1.lab.example.com]
      changed: [junos2.lab.example.com]
      
      PLAY RECAP ***********************************************************************
      junos1.lab.example.com    : ok=2   changed=1   unreachable=0   failed=0      ...
      junos2.lab.example.com    : ok=2   changed=1   unreachable=0   failed=0      ...
    5. (Optional) The playbooks in the aux directory in the current project enable you to verify the DNS and SNMP settings on the IOS and Juniper Junos managed nodes.

      • Run the aux/ios_dns_verify.yml auxiliary playbook to verify the DNS configuration on the IOS managed nodes.

      • Run the aux/junos_dns_verify.yml auxiliary playbook to verify the DNS configuration on the Juniper Junos managed nodes.

      • Run the aux/ios_snmp_verify.yml auxiliary playbook to verify the SNMP configuration on the IOS managed nodes.

      • Run the aux/junos_snmp_verify.yml auxiliary playbook to verify the SNMP configuration on Juniper Junos managed nodes.

  7. Close the /home/student/manage-variables directory in VS Code. If you are using the GNOME terminal, return to the /home/student directory.

    1. Click FileClose Folder to close the /home/student/manage-variables directory.

    2. If you are using the GNOME terminal, run the cd command to return to the student home directory:

      [student@workstation manage-variables]$ 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 manage-variables

Revision: do457-2.3-7cfa22a