In this exercise, you use the ansible-galaxy command to download and install an Ansible role.
Outcomes
Create a requirements file to specify role dependencies for a playbook.
Install roles specified in a requirements file.
List downloaded roles by using the ansible-galaxy command.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start role-galaxy
Procedure 7.2. Instructions
Change into the /home/student/role-galaxy directory.
[student@workstation ~]$ cd ~/role-galaxy
[student@workstation role-galaxy]$Create a role requirements file in your project directory that downloads the student.bash_env role.
This role configures the default initialization files for the Bash shell that are used for newly created users and stored in the /etc/skel directory.
The role adjusts the shell prompt and configures an environment variable that is used by the less command.
Create a file called requirements.yml in the roles subdirectory.
The URL of the role's Git repository is: git@workstation.lab.example.com:student/bash_env.
To see how the role affects the behavior of production hosts, use the main branch of the repository.
Set the local name of the role to student.bash_env.
Ensure that the roles/requirements.yml file contains the following content:
--- # requirements.yml - src: git@workstation.lab.example.com:student/bash_env scm: git version: main name: student.bash_env
Use the ansible-galaxy command to process the new requirements file that installs the student.bash_env role.
List the contents of the roles subdirectory before the role is installed, so that you can compare its current state to command results.
[student@workstation role-galaxy]$ ls roles/
requirements.ymlUse the ansible-galaxy command to download and install the roles listed in the roles/requirements.yml file into the roles subdirectory of your project.
[student@workstation role-galaxy]$ansible-galaxy install \>-r roles/requirements.yml -p rolesStarting galaxy role install process - extracting student.bash_env to /home/student/role-galaxy/roles/student.bash_env - student.bash_env (main) was installed successfully
List the contents of the roles subdirectory after the role is installed.
Confirm that a new subdirectory called student.bash_env, matching the name value specified in the requirements file, is present.
[student@workstation role-galaxy]$ ls roles/
requirements.yml student.bash_envUse the ansible-galaxy list command to list the project roles in the roles subdirectory.
[student@workstation role-galaxy]$ansible-galaxy list -p roles# /home/student/role-galaxy/roles -student.bash_env, main# /usr/share/ansible/roles - linux-system-roles.certificate, (unknown version) - linux-system-roles.cockpit, (unknown version) - linux-system-roles.crypto_policies, (unknown version) - linux-system-roles.firewall, (unknown version) - linux-system-roles.ha_cluster, (unknown version) - linux-system-roles.kdump, (unknown version) - linux-system-roles.kernel_settings, (unknown version) ...output omitted... [WARNING]: - the configured path /home/student/.ansible/roles does not exist.
If you do not specify the option -p roles to your ansible-galaxy list command, the role that you installed is not listed because the roles subdirectory is not in your default roles_path.
[student@workstation role-galaxy]$ ansible-galaxy list
# /usr/share/ansible/roles
- linux-system-roles.certificate, (unknown version)
- linux-system-roles.cockpit, (unknown version)
- linux-system-roles.crypto_policies, (unknown version)
- linux-system-roles.firewall, (unknown version)
- linux-system-roles.ha_cluster, (unknown version)
- linux-system-roles.kdump, (unknown version)
- linux-system-roles.kernel_settings, (unknown version)
...output omitted...
[WARNING]: - the configured path /home/student/.ansible/roles does not exist.Create a playbook named use-bash_env-role.yml that uses the student.bash_env role.
The playbook must have the following contents:
---
- name: Use student.bash_env role playbook
hosts: devservers
vars:
default_prompt: '[\u on \h in \W dir]\$ '
pre_tasks:
- name: Ensure test user does not exist
ansible.builtin.user:
name: student2
state: absent
force: yes
remove: yes
roles:
- student.bash_env
post_tasks:
- name: Create the test user
ansible.builtin.user:
name: student2
state: present
password: "{{ 'redhat' | password_hash('sha512', 'mysecretsalt') }}"You must create a user account to see the effects of the configuration change.
The pre_tasks and post_tasks section of the playbook ensure that the student2 user account is deleted and created each time the playbook is run.
When you run the use-bash_env-role.yml playbook, the student2 account is created with a password of redhat.
The user2 password is generated using a filter.
Filters take data and modify it; here, the string redhat is modified by passing it to the password_hash filter in order to convert the value into a SHA512-protected password hash.
Filters are an advanced topic not covered in this course.
Run the use-bash_env-role.yml playbook.
The student.bash_env role creates standard template configuration files in /etc/skel on the managed host.
The files it creates include .bashrc, .bash_profile, and .vimrc.
[student@workstation role-galaxy]$ansible-navigator run \>-m stdout use-bash_env-role.ymlPLAY [Use student.bash_env role playbook] ************************************** TASK [Gathering Facts] ********************************************************* ok: [servera.lab.example.com] TASK [Ensure test user does not exist] ***************************************** ok: [servera.lab.example.com] TASK [student.bash_env : put away .bashrc] ************************************* changed: [servera.lab.example.com] TASK [student.bash_env : put away .bash_profile] ******************************* changed: [servera.lab.example.com] TASK [student.bash_env : put away .vimrc] ************************************** changed: [servera.lab.example.com] TASK [Create the test user] **************************************************** changed: [servera.lab.example.com] PLAY RECAP ********************************************************************* servera.lab.example.com : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Use SSH to log in to the servera machine as the student2 user.
Observe the custom prompt for the student2 user, and then disconnect from the servera machine.
[student@workstation role-galaxy]$ssh student2@servera...output omitted... [student2 on servera in ~ dir]$exitlogout Connection to servera closed. [student@workstation role-galaxy]$
Run the playbook using the development version of the student.bash_env role.
The development version of the role is located in the dev branch of the Git repository.
The development version of the role uses a new variable, prompt_color.
Before running the playbook, add the prompt_color variable to the vars section of the playbook and set its value to blue.
Update the roles/requirements.yml file, and set the version value to dev.
Ensure that the roles/requirements.yml file contains the following content:
---
# requirements.yml
- src: git@workstation.lab.example.com:student/bash_env
scm: git
version: dev
name: student.bash_envModify the ~/role-galaxy/ansible.cfg and add the roles_path setting to the [defaults] section of the file.
This sets the default roles path and enable you to omit the -p roles option when typing ansible-galaxy commands.
When completed, the file contains then following content:
[defaults]
inventory=inventory
remote_user=devops
become_ask_pass=False
roles_path=roles
[privilege_escalation]
become=True
become_method=sudo
become_user=rootRemove the existing version of the student.bash_env role from the roles subdirectory.
[student@workstation role-galaxy]$ ansible-galaxy remove student.bash_env
- successfully removed student.bash_envUse the ansible-galaxy install command to install the role by using the updated requirements file.
[student@workstation role-galaxy]$ansible-galaxy install \>-r roles/requirements.ymlStarting galaxy role install process - extracting student.bash_env to /home/student/role-galaxy/roles/student.bash_env - student.bash_env (dev) was installed successfully
Modify the use-bash_env-role.yml file.
Add the prompt_color variable with a value of blue to the vars section of the playbook.
Ensure that the file contains the following content:
---
- name: Use student.bash_env role playbook
hosts: devservers
vars:
prompt_color: blue
default_prompt: '[\u on \h in \W dir]\$ '
pre_tasks:
...output omitted...Run the use-bash_env-role.yml playbook.
[student@workstation role-galaxy]$ansible-navigator run \>-m stdout use-bash_env-role.ymlPLAY [Use student.bash_env role playbook] ************************************** TASK [Gathering Facts] ********************************************************* ok: [servera.lab.example.com] TASK [Ensure test user does not exist] ***************************************** changed: [servera.lab.example.com] TASK [student.bash_env : put away .bashrc] ************************************* changed: [servera.lab.example.com] TASK [student.bash_env : put away .bash_profile] ******************************* ok: [servera.lab.example.com] TASK [student.bash_env : put away .vimrc] ************************************** ok: [servera.lab.example.com] TASK [Create the test user] **************************************************** changed: [servera.lab.example.com] PLAY RECAP ********************************************************************* servera.lab.example.com : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Use SSH to log in to the servera machine as the student2 user.
The custom prompt for the student2 user now displays with blue characters.
[student@workstation role-galaxy]$ ssh student2@servera
...output omitted...
[student2 on servera in ~ dir]$Log out from servera.
[student2 on servera in ~ dir]$ exit
logout
Connection to servera closed.
[student@workstation role-galaxy]$This concludes the section.