In this exercise, you install an Ansible Content Collection and use a role or module from that content collection in a playbook.
Outcomes
Use the ansible-galaxy command to install an Ansible Content Collection.
Use a requirements.yml file to install multiple Ansible Content Collections.
Run playbooks that use roles and modules from Ansible Content Collections.
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-collections
Procedure 7.3. Instructions
Install and then inspect the gls.utils collection.
Change into the /home/student/role-collections directory.
[student@workstation ~]$ cd ~/role-collections
[student@workstation role-collections]$Install the gls.utils collection from the tar archive file in the /home/student/role-collections/ directory.
[student@workstation role-collections]$ansible-galaxy collection \>install /home/student/role-collections/gls-utils-0.0.1.tar.gz -p collectionsStarting galaxy collection install process Process install dependency map Starting collection install process Installing 'gls.utils:0.0.1' to '/home/student/role-collections/collections/ansible_collections/gls/utils' gls.utils:0.0.1 was installed successfully
The preceding command installs the gls.utils Ansible Content Collection in the /home/student/role-collections/collections/ansible_collections/gls/utils directory.
Use the ansible-navigator collections command to open the text-based user interface (TUI).
[student@workstation role-collections]$ansible-navigator collectionsName Version Shadowed Type Path 0│amazon.aws 3.2.0 False contained /usr/share/ansible/collections/ansible_collections/amazon/aws 1│ansible.builtin 2.13.3 False contained /usr/lib/python3.9/site-packages/ansible ...output omitted...17│gls.utils0.0.1 False bind_mount /home/student/role-collections/collections/ansible_collections/gls/utils ...output omitted...
Select the gls.utils collection to list the roles that the gls.utils Ansible Content Collection provides.
Gls.utils Type Added Deprecated Description0│backuproleUnknown Unknown Curriculum Developer 1│myping module historical False Try to connect to host, verify a usable python and return C(pong) on success2│restoreroleUnknown Unknown Curriculum Developer
In the preceding output, notice that the collection provides two roles: backup and restore.
Select the backup role to read its documentation.
Image: gls.utils.backup Description: Curriculum Developer ...output omitted... 24│ backup 25│ ====== 26│ 27│ This role backups up the files and directories provided using the `backup_files` variable. 28│ The backup is identified with a given name (`backup_id`) and can be restored using the `gls.utils.restore` role. 29│ 30│ If a backup with the same name already exists, then the role immediately returns. 31│ 32│ 33│ Requirements 34│ ------------ 35│ 36│ None 37│ 38│ 39│ Role Variables 40│ -------------- 41│ 42│ The role accepts the following variables: ...output omitted...
Press Esc to return to the preceding screen.
The modules provided by the role are also indicated by ansible-navigator.
Gls.utils Type Added Deprecated Description 0│backup role Unknown Unknown Curriculum Developer1│mypingmodulehistorical False Try to connect to host, verify a usable python and return C(pong) on success 2│restore role Unknown Unknown Curriculum Developer
The gls.utils collection provides the newping module.
Type :q to exit the ansible-navigator collections TUI, then use the ansible-navigator doc command to display the documentation for the gls.utils.newping module.
[student@workstation role-collections]$ansible-navigator doc \>-m stdout gls.utils.newping> GLS.UTILS.MYPING (/home/student/role-collections/collections/ansible_collections/gls/uti> A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable Python is configured. This is NOT ICMP ping, this is just a trivial test module that requires Python on the remote-node. For Windows targets, use the [ansible.windows.win_ping] module instead. For Network targets, use the [ansible.netcommon.net_ping] module instead. ...output omitted...
Type :q to exit ansible-navigator doc.
Complete and then run the /home/student/role-collections/bck.yml playbook.
That playbook uses the gls.utils.newping module and the gls.utils.backup role.
Edit the bck.yml playbook.
In the first task, run the gls.utils.newping module.
...output omitted...
tasks:
- name: Ensure the machine is up
gls.utils.newping:
data: pong
...output omitted...Do not close the file yet.
In the second task, run the gls.utils.backup role.
When you finish editing the file, save and close it.
...output omitted...
- name: Ensure configuration files are saved
include_role:
name: gls.utils.backup
vars:
backup_id: backup_etc
backup_files:
- /etc/sysconfig
- /etc/yum.repos.dThe resulting file must contain the following contents:
---
- name: Backup the system configuration
hosts: servera.lab.example.com
become: true
gather_facts: false
tasks:
- name: Ensure the machine is up
gls.utils.newping:
data: pong
- name: Ensure configuration files are saved
include_role:
name: gls.utils.backup
vars:
backup_id: backup_etc
backup_files:
- /etc/sysconfig
- /etc/yum.repos.dVerify the syntax of the bck.yml playbook.
If you get any errors, compare your playbook to the preceding example.
[student@workstation role-collections]$ansible-navigator run \>-m stdout bck.yml --syntax-checkplaybook: /home/student/role-collections/bck.yml
Run the playbook.
[student@workstation role-collections]$ansible-navigator run \>-m stdout bck.ymlPLAY [Backup the system configuration] ***************************************** TASK [Ensure the machine is up] ************************************************ ok: [servera.lab.example.com] TASK [Ensure configuration files are saved] ************************************ TASK [gls.utils.backup : Ensure the backup directory exists] ******************* changed: [servera.lab.example.com] TASK [gls.utils.backup : Ensure the backup exists] ***************************** changed: [servera.lab.example.com] PLAY RECAP ********************************************************************* servera.lab.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In the second part of this exercise, install the Ansible Content Collections specified by the Ansible project's requirements.yml file.
To test your work, run the new_system.yml playbook.
That playbook uses the redhat.insights.insights_client and redhat.rhel_system_roles.selinux roles to configure Red Hat Insights and SELinux on the servera machine.
Review the requirements.yml file.
The file lists three Ansible Content Collections to install from tar archive files that are located in your Ansible project directory.
The redhat.insights collection currently requires the unsupported community.general.ini_file module.
The tar archive files are pre-downloaded to your project directory from the Red Hat automation hub at https://console.redhat.com/ansible/automation-hub.
--- collections: - name: /home/student/role-collections/redhat-insights-1.0.7.tar.gz - name: /home/student/role-collections/redhat-rhel_system_roles-1.19.3.tar.gz - name: /home/student/role-collections/community-general-5.5.0.tar.gz
Use the ansible-galaxy command with the requirements.yml file to install the collections.
[student@workstation role-collections]$ansible-galaxy collection install \>-r requirements.yml -p collectionsStarting galaxy collection install process Process install dependency map Starting collection install process Installing 'redhat.insights:1.0.7' to '/home/student/role-collections/collections/ansible_collections/redhat/insights' redhat.insights:1.0.7 was installed successfully Installing 'redhat.rhel_system_roles:1.19.3' to '/home/student/role-collections/collections/ansible_collections/redhat/rhel_system_roles' redhat.rhel_system_roles:1.19.3 was installed successfully Installing 'community.general:5.5.0' to '/home/student/role-collections/collections/ansible_collections/community/general' community.general:5.5.0 was installed successfully
Use the ansible-galaxy collection list command to verify that the collections are installed.
[student@workstation role-collections]$ ansible-galaxy collection list
...output omitted...
# /home/student/role-collections/collections/ansible_collections
Collection Version
------------------------ -------
community.general 5.5.0
gls.utils 0.0.1
redhat.insights 1.0.7
redhat.rhel_system_roles 1.19.3There may be other collections installed under /usr/share/ansible/collections/ansible_collections.
Use the ansible-navigator collections command to view the insights_client role contained in the redhat.insights collection.
When you run the ansible-navigator collections command, it displays the following output in its TUI:
[student@workstation role-collections]$ansible-navigator collectionsName Version Shadowed Type Path 0│amazon.aws 3.2.0 False contained /usr/share/ansible/collections/ansible_collections/amazon/aws 1│ansible.builtin 2.13.3 False contained /usr/lib/python3.9/site-packages/ansible ...output omitted...23│redhat.insights1.0.7 False bind_mount /home/student/role-collections/collections/ansible_collections/redhat/insights ...output omitted...
Enter :23 to select the redhat.insights Ansible Content Collection.
The following output is displayed by ansible-navigator in its TUI:
Redhat.insights Type Added Deprecated Description
0│compliance role Unknown Unknown Install and configure Red Hat Insights Client
1│insights inventory None False insights inventory source
2│insights_client role Unknown Unknown Install and configure Red Hat Insights Client
3│insights_config module None False This module handles initial configuration of the insights client on install
4│insights_register module None False This module registers the insights clientEnter :2 to select the documentation for the insights_client role from the Ansible Content Collection.
The ansible-navigator command displays the following output in its TUI:
Image: redhat.insights.insights_client
Description: Install and configure Red Hat Insights Client
0│---
1│argument_specs: {}
2│argument_specs_path: ''
3│defaults: {}
4│defaults_path: ''
5│full_name: redhat.insights.insights_client
6│info:
7│ galaxy_info:
8│ author: Red Hat, Inc
9│ categories:
10│ - packaging
11│ - system
12│ company: Red Hat, Inc.
13│ dependencies: []
14│ description: Install and configure Red Hat Insights Client
...output omitted...Type :q to exit the ansible-navigator TUI.
Review the new_system.yml playbook.
This playbook uses roles from the redhat.insights and redhat.rhel_system_roles collections.
---
- name: Configure the system
hosts: servera.lab.example.com
become: true
gather_facts: true
tasks:
- name: Ensure the system is registered with Insights
include_role:
name: redhat.insights.insights_client
vars:
auto_config: false
insights_proxy: http://proxy.example.com:8080
- name: Ensure SELinux mode is Enforcing
include_role:
name: redhat.rhel_system_roles.selinux
vars:
selinux_state: enforcingRun the new_system.yml playbook in check mode to confirm that you correctly installed the required collections.
[student@workstation role-collections]$ansible-navigator run \>-m stdout new_system.yml --check...output omitted... RUNNING HANDLER [redhat.insights.insights_client : Run insights-client] ******** skipping: [servera.lab.example.com] PLAY RECAP ********************************************************************* servera.lab.example.com : ok=17 changed=3 unreachable=0 failed=0 skipped=18 rescued=0 ignored=0
Because the classroom systems are not registered with Red Hat Subscription Management and might not have internet access, the new_system.yml playbook cannot complete successfully.
However, to confirm that you correctly installed the required collections, you can still run the playbook in check mode.
This concludes the section.