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
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.
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/
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.gitCloning into 'develop-review'... ...output omitted... [student@workstation git-repos]$cd develop-review
Create the exercise branch.
[student@workstation develop-review]$ git checkout -b exercise
Switched to a new branch 'exercise'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.
Edit the site.yml playbook to include a name for the play.
---
- name: Sets the GRUB timeout
hosts: all
become: true
roles:
- role: testEdit the roles/test/tasks/main.yml file to add a name to each task.
---- name: Sets persistent GRUB timeoutansible.builtin.lineinfile: path: /etc/default/grub regexp: "^GRUB_TIMEOUT=" line: "GRUB_TIMEOUT={{ timeout | int }}" when: persistent | bool == true- name: Sets temporary GRUB timeoutansible.builtin.lineinfile: path: /boot/grub2/grub.cfg regexp: "^set timeout=" line: "set timeout={{ timeout | int }}"
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.
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: grubRename the role by moving the roles/test subdirectory to roles/grub.
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/grubChange 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.
Edit the roles/grub/defaults/main.yml file to add the grub_ prefix.
---grub_timeout: 0grub_persistent: true
Edit the group_vars/lb_servers.yml to add the grub_ prefix.
---grub_timeout: 30grub_persistent: true
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 }}"
(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 timeouthosts: all become: true roles: - role:grubgrub_timeout: 15grub_persistent: true ...output omitted...
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.
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 stdoutPLAY [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 ...
Push all of your changes to the exercise branch of the remote Git repository.
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.ymlAdd the files you created and edited to the staging area:
[student@workstation develop-review]$ git add group_vars/ roles/ site.ymlCommit the staged files to the local Git repository:
[student@workstation develop-review]$ git commit -m 'Changes for exercise'
...output omitted...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 exercisePassword for 'https://student@git.lab.example.com':Student@123...output omitted...
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