Bookmark this page

Guided Exercise: Running a Playbook that Requires a Role

Create a requirements.yml file in a project to download and install an Ansible Role, and then use automation controller to automatically get that role and run a playbook that uses it.

Outcomes

  • Create a requirements file to specify role dependencies for a playbook in a Git repository.

  • Create a project in automation controller that points to the Git repository.

  • Launch a job template that automatically retrieves the playbook role dependencies from the requirements file.

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

Instructions

  1. Verify that the Message of the Day (MOTD) banner is not configured on the iosxe1.lab.example.com managed node.

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

      [student@workstation ~]$ ssh student@iosxe1.lab.example.com
      
      
      iosxe1.lab.example.com#
    2. 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.
  2. From the automation controller web UI, create a project that points to the simplify-external Git project.

    Use the information in the following table to create the project:

    FieldValue
    Name Cisco IOS MOTD
    Organization Default
    Source Control Type Git
    Source Control URL git@git.lab.example.com:student/simplify-external
    Source Control Credential Git Projects
    Options Delete
    Allow Branch Override
    1. Navigate to https://controller.lab.example.com and log in as admin using redhat as the password.

    2. Navigate to ResourcesProjects and then click Add.

    3. Create the project with settings from the preceding table and then click Save.

      Automation controller immediately attempts to synchronize the project. The Last Job Status field updates to indicate that the project synchronization succeeded.

      Important

      If synchronization fails, ensure that the project specifies the correct source control URL and credentials and then attempt the project synchronization again.

  3. Create a job template to run the ios_motd.yml playbook from the Cisco IOS MOTD automation controller project. Launch the job template and review the job output.

    Use the information in the following table to create the job template:

    FieldValue
    Name Cisco IOS MOTD banner
    Description Configure a MOTD banner on Cisco IOS managed nodes
    Inventory Common
    Project Cisco IOS MOTD
    Playbook ios_motd.yml
    Credentials Cisco IOS XE Auth
    1. Navigate to ResourcesTemplates and then click AddAdd job template.

    2. Create the job template with settings from the preceding table and then click Save.

    3. Click Launch to run the job template.

    4. Navigate to ViewsJobs and look for a numbered job with Cisco IOS MOTD banner in the name. The job displays a status of Failed.

    5. Click the name of the Cisco IOS MOTD banner job to review the job output. The ios_motd.yml playbook fails in the Configure the MOTD banner task:

      ...output omitted...
      TASK [Configure the MOTD banner] *************************************************
      ERROR! the role 'banner' was not found in /runner/project/roles:/runner/requirements_roles:/home/runner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/runner/project
      
      The error appears to be in '/runner/project/ios_motd.yml': line 12, column 15, but may
      be elsewhere in the file depending on the exact syntax problem.
      
      The offending line appears to be:
      
            ansible.builtin.include_role:
              name: banner
                    ^ here
      ...output omitted...

      The task fails because the playbook includes the banner role, but the project does not indicate where to find that role.

  4. Clone the git@git.lab.example.com:student/simplify-external Git repository into the /home/student/git-repos directory. Create a branch named exercise and check out that branch.

    1. On the workstation machine, change to the /home/student/git-repos directory and then use the git clone command to clone the git@git.lab.example.com:student/simplify-external Git repository:

      [student@workstation ~]$ cd ~/git-repos/
      [student@workstation git-repos]$ git clone \
      git@git.lab.example.com:student/simplify-external
      Cloning into 'simplify-external'...
      remote: Enumerating objects: 3, done.
      remote: Counting objects: 100% (3/3), done.
      remote: Compressing objects: 100% (2/2), done.
      remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
      Receiving objects: 100% (3/3), done.
    2. Change to the simplify-external directory, and then create a branch named exercise and check it out:

      [student@workstation git-repos]$ cd simplify-external
      [student@workstation simplify-external]$ git checkout -b exercise
      Switched to a new branch 'exercise'
  5. Open the /home/student/git-repos/simplify-external directory in VS Code. Examine the ios_motd.yml playbook. Notice that the playbook uses the banner role. For this task to succeed, the banner role must be installed.

    1. Open VS Code and click FileOpen Folder.

    2. Navigate to git-repossimplify-external 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. Examine the ios_motd.yml playbook. The playbook uses the ansible.builtin.include_role module to include the banner role.

      ---
      - name: Add a MOTD banner on Cisco IOS managed nodes
        hosts: ios
        gather_facts: false
      
        tasks:
          - name: Configure the MOTD banner
            vars:
              banner_text: Managed by Ansible
              banner_type: motd
            ansible.builtin.include_role:
              name: banner

      Notice that the playbook is the only file in the Git repository and the playbook does not specify where to find the banner role.

  6. Create a role requirements file in your project directory that downloads and installs the banner role from the git@git.lab.example.com:student/banner Git repository. The banner role configures banners on Cisco IOS and Juniper Junos managed nodes.

    After you create the role requirements file, use either VS Code or the CLI to add, commit, and push the changes to the remote repository.

    1. Switch to the Terminal tab in VS Code, or return to your GNOME terminal. Use the mkdir command to create the roles directory in your project:

      [student@workstation simplify-external]$ mkdir roles
    2. In VS Code, create the roles/requirements.yml file with the following content:

      ---
      - src: git@git.lab.example.com:student/banner
        scm: git
        version: main

      The src attribute specifies the location of the banner role. The scm attribute specifies Git as the source code management tool. The version attribute specifies the use of the main branch of the specified Git repository.

    3. Add, commit, and push the changes to the remote repository. Use a descriptive commit message, such as Add requirements file.

      If you choose to use the CLI, then use the git add, git commit, and git push commands from your GNOME terminal:

      [student@workstation simplify-external]$ git add roles
      [student@workstation simplify-external]$ git commit -m "Add requirements file"
       1 file changed, 4 insertions(+)
       create mode 100644 roles/requirements.yml
      [student@workstation simplify-external]$ git push -u origin exercise
      Enumerating objects: 5, done.
      Counting objects: 100% (5/5), done.
      Delta compression using up to 6 threads
      Compressing objects: 100% (3/3), done.
      Writing objects: 100% (4/4), 422 bytes | 422.00 KiB/s, done.
      Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
      remote:
      remote: To create a merge request for exercise, visit:
      remote:   https://git.lab.example.com/student/simplify-external/-/merge_requests/new?merge_request%5Bsource_branch%5D=exercise
      remote:
      To git.lab.example.com:student/simplify-external
       * [new branch]      exercise -> exercise
      Branch 'exercise' set up to track remote branch 'exercise' from 'origin'.
  7. From the automation controller web UI, update the Cisco IOS MOTD project resource so that the project uses the exercise branch you created with the requirements file.

    1. Navigate to ResourcesProjects and then click the Edit Project icon for the Cisco IOS MOTD project.

    2. Enter exercise in the Source Control Branch/Tag/Commit field and then click Save. Automation controller immediately attempts to synchronize the project. The Last Job Status field updates to indicate that the project synchronization succeeded.

    3. (Optional) Click Successful in the Last Job Status field to review the job output.

      Figure 6.2: Project synchronization includes the requirements file

      The Cisco IOS MOTD project now includes the requirements.yml file specifying the role dependencies for the ios_motd.yml playbook.

  8. Launch the Cisco IOS MOTD banner job template and verify that the resulting job succeeds. Verify that MOTD banner is now configured on the iosxe1.lab.example.com managed node.

    1. Navigate to ResourcesTemplates and then click the Launch Template icon for the Cisco IOS MOTD banner job template. The job displays the following output:

      PLAY [Add a MOTD banner on Cisco IOS managed nodes] ****************************
      
      TASK [Configure the MOTD banner] ***********************************************
      
      TASK [banner : Configure managed node banner] **********************************
      included: /runner/requirements_roles/banner/tasks/cisco.ios.ios.yml for iosxe1.lab.example.com, iosxe2.lab.example.com
      
      TASK [banner : Create a Cisco IOS banner] **************************************
      changed: [iosxe1.lab.example.com]
      changed: [iosxe2.lab.example.com]
      
      PLAY RECAP *********************************************************************
      iosxe1.lab.example.com    : ok=2   changed=1   unreachable=0   failed=0      ...
      iosxe2.lab.example.com    : ok=2   changed=1   unreachable=0   failed=0      ...
    2. On the workstation machine, connect to the iosxe1.lab.example.com managed node as the student user:

      [student@workstation simplify-external]$ ssh student@iosxe1.lab.example.com
      
      Managed by Ansible
      
      
      
      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.
  9. Click FileClose Folder in VS Code to close the /home/student/git-repos/simplify-external directory. On the workstation machine, run the cd command to return to the /home/student directory:

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

Revision: do457-2.3-7cfa22a