Bookmark this page

Guided Exercise: Managing Ansible Project Materials Using Git

Clone an existing Git repository that contains an Ansible Playbook, make edits to files in that repository, commit the changes to your local repository, and push them to the original repository.

Outcomes

  • Clone a Git repository to the workstation machine.

  • Create a playbook to configure the Simple Network Management Protocol (SNMP) on Cisco IOS XE managed nodes.

  • Stage the playbook file, commit the changes, and synchronize with the remote repository.

As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available. The command also removes any Simple Network Management Protocol (SNMP) configuration from the Cisco IOS XE managed nodes.

[student@workstation ~]$ lab start run-git

Instructions

  1. Open a terminal and verify that SNMP is not configured on the iosxe1.lab.example.com managed node.

    1. Connect to the iosxe1.lab.example.com managed node by using SSH:

      [student@workstation ~]$ ssh student@iosxe1.lab.example.com
      
      iosxe1.lab.example.com#
    2. Display the SNMP configuration for the iosxe1.lab.example.com managed node. The command output indicates that the SNMP agent is not enabled:

      iosxe1.lab.example.com#show snmp
      %SNMP agent not enabled
      iosxe1.lab.example.com#
    3. Return to the workstation machine:

      iosxe1.lab.example.com#exit
      Connection to iosxe1.lab.example.com closed by remote host.
      Connection to iosxe1.lab.example.com closed.
      [student@workstation ~]$
  2. Clone the git@git.lab.example.com:student/snmp Git repository to the /home/student/git-repos directory. Create a branch named exercise and check out that branch.

    1. Create the /home/student/git-repos directory if it does not exist, and then change to this directory:

      [student@workstation ~]$ mkdir -p ~/git-repos
      [student@workstation ~]$ cd ~/git-repos
      [student@workstation git-repos]$
    2. Clone the git@git.lab.example.com:student/snmp repository, and then change to the repository directory:

      [student@workstation git-repos]$ git clone \
      git@git.lab.example.com:student/snmp.git
      Cloning into 'snmp'...
      ...output omitted...
      [student@workstation git-repos]$ cd snmp
      [student@workstation snmp]$
    3. Create and check out the exercise branch:

      [student@workstation snmp]$ git branch exercise
      [student@workstation snmp]$ git checkout exercise
      Switched to branch 'exercise'

      Note

      You can also use the git checkout -b exercise command to create and check out the exercise branch by using a single command:

      [student@workstation snmp]$ git checkout -b exercise
      Switched to a new branch 'exercise'
  3. Open VS Code and create a playbook named cisco_snmp.yml in the /home/student/git-repos/snmp directory.

    1. Open VS Code and click FileOpen Folder. Navigate to Homegit-repossnmp and then click Open.

    2. In VS Code, click FileNew Text File. Click Select a language and then click Ansible.

    3. Create a playbook with the following content and save it as cisco_snmp.yml:

      ---
      - name: Configure SNMP on Cisco IOS XE managed nodes
        hosts: ios
        gather_facts: false
        tasks:
          - name: Configure SNMP using the snmp_server resource module
            cisco.ios.ios_snmp_server:
              state: merged
              config:
                location: 'Raleigh, NC'
                contact: 'Network Engineering | neteng@company.com'
                communities:
                  - acl_v4: snmpaclro
                    name: rocommunity2n4g!
                    ro: true
                  - acl_v4: snmpaclrw
                    name: rwcommunityd7g$v
                    rw: true
                hosts:
                  - community_string: 'm0n1t0r'
                    host: 172.25.1.10
                    informs: true
                    version: 2c
                traps:
                  bgp:
                    enable: true
                  eigrp: true
                  hsrp: true
                  ospf:
                    cisco_specific:
                      error: true
                      lsa: true
                      retransmit: true
                  snmp:
                    authentication: true
                    coldstart: true
                    linkdown: true
                    linkup: true
                    warmstart: true
  4. In VS Code, stage and commit the SNMP playbook.

    1. Click the Source Control icon in the left navigation menu, or click ViewSource Control to display the Source Control panel.

    2. Hover over your mouse over the file. A plus (+) symbol next to the file appears. Click the plus symbol to add the file to the Staged Changes section.

    3. In the Message field in the Primary Side Bar, enter "Cisco SNMP playbook for IOS XE managed nodes" as the commit message and click Commit.

  5. Return to the terminal and run the cisco_snmp.yml playbook.

    1. Copy all files from the ~/run-git directory to the ~/snmp directory. This copies the ansible-navigator.yml and inventory files to the repository directory so that the playbook runs. Add and commit these files to complete the project:

      [student@workstation snmp]$ cp ~/run-git/* .
      [student@workstation snmp]$ git add ansible-navigator.yml inventory
      [student@workstation snmp]$ git commit -m "Add automation navigator config file and managed node inventory file"
      [exercise 08eb493] Add automation navigator config file and managed node inventory file
       2 files changed, 5 insertions(+), 1 deletion(-)
      [student@workstation snmp]
    2. Run the cisco_snmp.yml playbook:

      [student@workstation snmp]$ ansible-navigator run cisco_snmp.yml
      
      PLAY [Configure SNMP on Cisco IOS XE managed nodes] ***************************
      
      TASK [Configure SNMP using the snmp_server resource module] *******************
      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 ...
  6. In a terminal, verify that the job successfully deployed SNMP.

    1. Connect to the iosxe1.lab.example.com managed node as the student user:

      [student@workstation snmp]$ ssh student@iosxe1.lab.example.com
      
      iosxe1.lab.example.com#
    2. Display the SNMP configuration for the managed node:

      iosxe1.lab.example.com#show snmp
      ...output omitted...
      SNMP informs: enabled
          Informs in flight 0/25 (current/max)
          Logging to 172.25.1.10.162
              0 sent, 0 in-flight, 0 retries, 0 failed, 0 dropped
    3. Return to the workstation machine:

      iosxe1.lab.example.com#exit
      Connection to iosxe1.lab.example.com closed by remote host.
      Connection to iosxe1.lab.example.com closed.
      [student@workstation snmp]$
  7. In the VS Code Source Control panel, synchronize the local commits with the remote repository.

    1. Click the Views and More Actions icon (…​) and then click Push.

    2. You might see a message asking you to publish the exercise branch. Click Ok.

    3. You might see another message asking you to periodically run git fetch. Click No.

  8. Merge the exercise branch into the main branch.

    1. Click exercise on the bottom status bar to display the menu to change branches. Select the main branch.

    2. Click the Views and More Actions icon (…​) and then click BranchMerge Branch.

    3. Select the exercise branch.

  9. Close the /home/student/git-repos/snmp 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/git-repos/snmp directory.

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

      [student@workstation snmp]$ 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 run-git

Revision: do457-2.3-7cfa22a