Skip to content

Jenkins

This lab utilizes the automation tool, Jenkins, as well as Git, for version control. If you aren't familiar with Git, visit the Automation Fundamentals Workshop here to learn more.

Jenkins is an open-source automation tools with plugins for building, deploying, and automating almost anything.

Git Commands

This lab makes use of a handful of git commands, which are described below.

git init: Initializes an empty git repository

git add: Adds files to the local git repository

git commit: Commits the git repository

git remote add origin: Adds a remote repository to commit to

git push: Pushes code to the repository

git reflog: The git reflog command lists every commit, their checksum, their distance from the current commit, and the commit message.

git revert: Reverts the local repository to a previous commit

Lab Tasks

These lab tasks revolve around automating the task of adding a VLAN to a fabric.

  1. Create the hosts file.

    1. Let’s start out with a different-looking hosts file, this time with every one of your leaf switches in it. Take special note of leafs, we’ll be using this later.

      [leafs]
      192.168.0.12
      192.168.0.13
      192.168.0.14
      192.168.0.15
      
    2. Save the file with the name hosts into the /home/coder/project/labfiles/lab6/lab directory.

      Note

      You will notice that there are existing files and folders. Please don’t overwrite anything for this lab.

  2. For this playbook we are going to call a pre-created role that is provided by Arista. In this case, we’ll be using the arista.eos.eos_vlans module from the arista.eos Ansible Collection to add VLANs.

    1. In the VSCode IDE, create the file below.

      - hosts: leafs
        connection: local
        tasks:
        - name: Create vlans
          arista.eos.eos_vlans:
            config:
            - vlan_id: "{{ item.vlanid }}"
              name: "{{ item.name }}"
            state: overridden
          loop: "{{ vlans }}"
      
    2. Save the file with the name vlan.yml into the /home/coder/project/labfiles/lab6/lab directory.

  3. For this lab we will be using what is called group variables or group_vars. Group variables are used for groups of hosts and not individual hosts.

    Tip

    For more information about Ansible, check out the Ansible section of the Automation Fundamentals Workshop here

    1. When we create a group variable file named leafs.yml, Ansible will use it for the hosts listed below leafs.

      1. Notice that we’re using the Ansible Collections methodology and approach for this lab.

      2. vlans, vlan_id, and name are what the arista.eos.eos_vlans collections module take as an input. If you want to see every module and variable that the collection can use, see the readme for the role.

      3. In the IDE, and create the file below.

        ansible_connection: ansible.netcommon.httpapi
        ansible_httpapi_use_ssl: True
        ansible_httpapi_validate_certs: False
        ansible_network_os: arista.eos.eos
        ansible_user: arista
        ansible_password: <your ATD password>
        vlans:
        - vlanid: 1001
        name: default
        
      4. Save the file with the name leafs.yml into the /home/coder/project/labfiles/lab6/lab/group_vars directory.

  4. Now we will perform tasks within Jenkins.

    1. Back on the ATD landing page, click on the Jenkins link on the lefthand side.

    2. Once Jenkins has loaded, click on the Login link to log in with your credentials from the landing page.

      Jenkins Link

    3. Jenkins will open in a new tab. Click on New Item in the top left of the window.

    4. You will be greeted with a screen like the one below. Enter vlan as the name and select Freestyle project.

      Jenkins Project

    5. Click Ok.

    6. Under Source Code Management, check Git and enter /opt/labfiles/lab6/repo in the Repository URL field.

      Note

      You will see a warning, ignore it for now.

    7. Scroll down to Build Triggers, and check Poll SCM. Poll SCM will poll for changes in Git and trigger a build from it.

      Note

      This is a crucial aspect of continuous delivery - once a change is made, this is the part that deploys it.

    8. In the Schedule field, enter:

      * * * * *
      

      If you are familiar with Linux cron, this is the same format - it’s telling Jenkins to check every 1 minute for a change.

    9. Scroll down to Build and click on Add build step. Select Invoke Ansible Playbook.

      1. For Playbook Path, enter vlan.yml.

      2. Select File or host list and enter in hosts.

    10. Click Save.

  5. We have to commit our changes into a Git repository for Jenkins to detect a change and run our playbook. Let’s go back to our IDE and run a few commands for our initial commit.

    1. Open a terminal window and type.

      cd ~/project/labfiles/lab6/lab
      
    2. First we will need to prep our remote git repository. Type the following command.

      git init --bare /home/coder/project/labfiles/lab6/repo
      
    3. Now enter the following.

      git init
      git add .
      git commit -m "Initial commit"
      git remote add origin /home/coder/project/labfiles/lab6/repo
      git push origin master
      
  6. Now we will run our workflow, which at a high level looks like below.

    Workflow

    1. Add VLANs to the variables file by opening the leafs.yml file in the IDE and adding lines below the existing text.

      vlans:
       - vlanid: 1001
         name: default
       - vlanid: 2000
         name: production
       - vlanid: 3000
         name: development
      
    2. Save the file.

    3. Add the file to Git via a commit and push the changes.

      1. In the terminal window, switch to the lab directory:

        cd ~/project/labfiles/lab6/lab
        
      2. Enter the following:

        git add .
        git commit -m "Added VLAN 2000 and 3000"
        git push origin master
        
      3. Go back to the Jenkins window.

Verification

Now that we are back in Jenkins we will see a few different things.

If we got back in time, we can see our vlan project executing under the Build Executor Status.

Build Status

If you don't see the vlan build running, Jenkins keeps a history we can check to see if the execution was successful.

From the main screen, click on vlan.

Vlan History

On the left hand side, click on the latest build which should be #3, but could be a higher or lower number.

Build History

In the left hand menu, click Console Output. Scroll all the way to the bottom to hopefully see:

PLAY RECAP *********************************************************************
192.168.0.12               : ok=7    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
192.168.0.13               : ok=7    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
192.168.0.14               : ok=7    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
192.168.0.15               : ok=7    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

Finally, we can log into one of the above leaf switches and verify the vlans are there.

Success

Lab Complete!