Bookmark this page

Lab: Developing Playbooks with Ansible Automation Platform

  • Improve an existing Ansible Playbook to follow recommended practices, commit the changes to its Git repository, and test it with automation content navigator.

Outcomes

  • Create roles with appropriate names.

  • Implement style guidelines for task and variable names in roles and playbooks.

  • Commit changes to a Git repository.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command initializes the remote Git repository at https://git.lab.example.com/student/develop-review.git. The Git repository contains a site.yml playbook that uses a role to configure the number of seconds that GRUB waits before proceeding with the boot process. You can push changes to this repository by using Student@123 as the Git password.

[student@workstation ~]$ lab start develop-review

Procedure 1.4. Instructions

  1. Clone the https://git.lab.example.com/student/develop-review.git Git repository into the /home/student/git-repos directory and then create a new branch named exercise.

    1. From a terminal, create the /home/student/git-repos directory if it does not already exist, and then change into it.

      [student@workstation ~]$ mkdir -p ~/git-repos/
      [student@workstation ~]$ cd ~/git-repos/
    2. Clone the https://git.lab.example.com/student/develop-review.git repository and then change into the cloned repository:

      [student@workstation git-repos]$ git clone \
      > https://git.lab.example.com/student/develop-review.git
      Cloning into 'develop-review'...
      ...output omitted...
      [student@workstation git-repos]$ cd develop-review
    3. Create the exercise branch.

      [student@workstation develop-review]$ git checkout -b exercise
      Switched to a new branch 'exercise'
  2. Ensure that plays and tasks have names associated with them. Add a name to the play in the site.yml playbook and add names to the two tasks in the roles/test/tasks/main.yml file.

    1. Edit the site.yml playbook to include a name for the play.

      ---
      - name: Sets the GRUB timeout
        hosts: all
        become: true
        roles:
          - role: test
    2. Edit the roles/test/tasks/main.yml file to add a name to each task.

      ---
      - name: Sets persistent GRUB timeout
        ansible.builtin.lineinfile:
          path: /etc/default/grub
          regexp: "^GRUB_TIMEOUT="
          line: "GRUB_TIMEOUT={{ timeout | int }}"
        when: persistent | bool == true
      
      - name: Sets temporary GRUB timeout
        ansible.builtin.lineinfile:
          path: /boot/grub2/grub.cfg
          regexp: "^set timeout="
          line: "set timeout={{ timeout | int }}"
  3. The site.yml playbook uses the test role to deploy the configuration changes. This role does not have a suitable name. Rename this role to grub and then update the site.yml playbook accordingly.

    1. Edit the related variables to reflect the new role location. Edit the site.yml file to show the new role.

      ---
      - name: Sets the GRUB timeout
        hosts: all
        become: true
        roles:
        - role: grub
    2. Rename the role by moving the roles/test subdirectory to roles/grub.

      Note

      Use the git mv command to rename the test role to the grub role. The git mv command renames the file or directory and automatically stages the change.

      [student@workstation develop-review]$ git mv roles/test roles/grub
  4. Change the two role variables names (timeout and persistent) to use the grub_ prefix. Update the roles/grub/tasks/main.yml and group_vars/lb_servers.yml files to use the new variable names. Although not evaluated, you might update the roles/grub/README.md file to reflect changes made in this exercise.

    1. Edit the roles/grub/defaults/main.yml file to add the grub_ prefix.

      ---
      grub_timeout: 0
      grub_persistent: true
    2. Edit the group_vars/lb_servers.yml to add the grub_ prefix.

      ---
      grub_timeout: 30
      grub_persistent: true
    3. Edit the roles/grub/tasks/main.yml file.

      ---
      - name: Sets persistent GRUB timeout
        ansible.builtin.lineinfile:
          path: /etc/default/grub
          regexp: "^GRUB_TIMEOUT="
          line: "GRUB_TIMEOUT={{ grub_timeout | int }}"
        when: grub_persistent | bool == true
      
      - name: Sets temporary GRUB timeout
        ansible.builtin.lineinfile:
          path: /boot/grub2/grub.cfg
          regexp: "^set timeout="
          line: "set timeout={{ grub_timeout | int }}"
    4. (Optional) Update the roles/grub/README.md file to change the role and variable names. The README.md file provides information on how to use the role, but the file is not used by the role itself. The grading script does not evaluate this file.

      ...output omitted...
      * `grub_timeout`: An integer used to set the GRUB timeout (in seconds). If a user does not interrupt the boot process within this timeout, then the system uses the default kernel with the default boot options. Defaults to a value of `0`.
      
      * `grub_persistent`: A boolean that controls whether a change to the GRUB timeout is persistent or temporary. A persistent change also updates the `/etc/default/grub` file so that the new timeout will be applied, even if students install a new kernel. Defaults to a value of `true`.
      
      Example Playbook
      ----------------
      
          - name: Sets the GRUB timeout
            hosts: all
            become: true
            roles:
              - role: grub
                grub_timeout: 15
                grub_persistent: true
      ...output omitted...
  5. Test the ~/git-repos/develop-review/site.yml playbook. Using the ee-supported-rhel8 execution environment, verify that the site.yml file runs without any errors. Automation execution environments are available from hub.lab.example.com by using student as the username and redhat123 as the password.

    1. Run the site.yml playbook using the ee-supported-rhel8 execution environment. Verify that the playbook has no errors.

      [student@workstation develop-review]$ ansible-navigator run site.yml \
      > --eei ee-supported-rhel8 --pp missing -m stdout
      
      PLAY [Sets the GRUB timeout] ***************************************************
      
      TASK [Gathering Facts] *********************************************************
      ok: [serverb.lab.example.com]
      ok: [servera.lab.example.com]
      ok: [serverc.lab.example.com]
      
      TASK [grub : Sets persistent GRUB timeout] *************************************
      ok: [serverb.lab.example.com]
      changed: [servera.lab.example.com]
      ok: [serverc.lab.example.com]
      
      TASK [grub : Sets temporary GRUB timeout] **************************************
      changed: [servera.lab.example.com]
      ok: [serverb.lab.example.com]
      ok: [serverc.lab.example.com]
      
      PLAY RECAP *********************************************************************
      servera.lab.example.com    : ok=3    changed=2    unreachable=0    failed=0  ...
      serverb.lab.example.com    : ok=3    changed=0    unreachable=0    failed=0  ...
      serverc.lab.example.com    : ok=3    changed=0    unreachable=0    failed=0  ...
  6. Push all of your changes to the exercise branch of the remote Git repository.

    1. Display changes made to the local Git repository:

      [student@workstation develop-review]$ git status
      On branch exercise
      Changes to be committed:
        (use "git restore --staged <file>..." to unstage)
          renamed:    roles/test/README.md -> roles/grub/README.md
          renamed:    roles/test/defaults/main.yml -> roles/grub/defaults/main.yml
          renamed:    roles/test/meta/main.yml -> roles/grub/meta/main.yml
          renamed:    roles/test/tasks/main.yml -> roles/grub/tasks/main.yml
      
      Changes not staged for commit:
        (use "git add <file>..." to update what will be committed)
        (use "git restore <file>..." to discard changes in working directory)
          modified:   group_vars/lb_servers.yml
          modified:   roles/grub/README.md
          modified:   roles/grub/defaults/main.yml
          modified:   roles/grub/tasks/main.yml
          modified:   site.yml
    2. Add the files you created and edited to the staging area:

      [student@workstation develop-review]$ git add group_vars/ roles/ site.yml
    3. Commit the staged files to the local Git repository:

      [student@workstation develop-review]$ git commit -m 'Changes for exercise'
      ...output omitted...
    4. Push the changes from the local Git repository to the exercise branch on the remote Git repository. If prompted, use Student@123 as the Git password.

      [student@workstation develop-review]$ git push -u origin exercise
      Password for 'https://student@git.lab.example.com': Student@123
      ...output omitted...
    5. Verify that your changes have been pushed to the exercise branch:

      [student@workstation develop-review]$ git status
      On branch exercise
      Your branch is up to date with 'origin/exercise'.
      
      nothing to commit, working tree clean

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures. Make sure to commit and push any additional changes to the remote Git repository and then rerun the command until successful.

[student@workstation ~]$ lab grade develop-review

Finish

On the workstation machine, change to the student user home directory and 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 develop-review

This concludes the section.

Revision: do374-2.2-82dc0d7