Bookmark this page

Guided Exercise: Getting Roles and Modules from Content Collections

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

  1. Install and then inspect the gls.utils collection.

    1. Change into the /home/student/role-collections directory.

      [student@workstation ~]$ cd ~/role-collections
      [student@workstation role-collections]$
    2. 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 collections
      Starting 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.

    3. Use the ansible-navigator collections command to open the text-based user interface (TUI).

      [student@workstation role-collections]$ ansible-navigator collections
      
         Name            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.utils         0.0.1 False    bind_mount /home/student/role-collections/collections/ansible_collections/gls/utils
      ...output omitted...
    4. Select the gls.utils collection to list the roles that the gls.utils Ansible Content Collection provides.

        Gls.utils  Type   Added	Deprecated  Description
      0│backup     role   Unknown     Unknown     Curriculum Developer
      1│myping     module historical  False       Try to connect to host, verify a usable python and return C(pong) on success
      2│restore    role   Unknown     Unknown     Curriculum Developer

      In the preceding output, notice that the collection provides two roles: backup and restore.

    5. 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...
    6. 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 Developer
      1│myping     module historical  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.

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

  2. 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.

    1. 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.

    2. 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.d

      The 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.d
    3. Verify 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-check
      playbook: /home/student/role-collections/bck.yml
    4. Run the playbook.

      [student@workstation role-collections]$ ansible-navigator run \
      > -m stdout bck.yml
      
      PLAY [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
  3. 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.

    1. 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
    2. 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 collections
      Starting 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
    3. 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.3

      There may be other collections installed under /usr/share/ansible/collections/ansible_collections.

    4. 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 collections
      
         Name            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.insights   1.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 client

      Enter :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.

    5. 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: enforcing
    6. Run 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

      Important

      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.

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 role-collections

This concludes the section.

Revision: rh294-9.0-c95c7de