Install Roles and Content Collections

AnsibleBeginner
Practice Now

Introduction

Ansible content can come from roles and collections. In restricted lab environments, local content archives are safer than internet downloads.

In this lab, you will package a local role, install it through a requirements file, inspect preinstalled collections, and apply the installed role.

Create a Content Installation Project

In this step, you will create a project with local content source, installed role, playbook, and output directories.

cd /home/labex/project
mkdir -p rhce-ansible/{inventory,roles,vendor_src/local_message/{tasks,meta},playbooks,managed,reports}
cd rhce-ansible
cat > ansible.cfg <<'EOF'
[defaults]
inventory = ./inventory/hosts
roles_path = ./roles
host_key_checking = False
retry_files_enabled = False
EOF
cat > inventory/hosts <<'EOF'
[managed]
host ansible_connection=local
EOF

This lab separates role source from installed role content. vendor_src/local_message represents content you received or built, while roles/local_message will be the installed copy Ansible actually loads.

Build a Local Role Source

In this step, you will create a small role source outside the installed roles directory. This simulates receiving role content from a trusted source. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
cat > vendor_src/local_message/tasks/main.yml <<'EOF'
---
- name: Ensure local message directory exists
  ansible.builtin.file:
    path: /home/labex/project/rhce-ansible/managed/local-message
    state: directory
    mode: '0755'

- name: Write message from installed role
  ansible.builtin.copy:
    dest: /home/labex/project/rhce-ansible/managed/local-message/message.txt
    content: |
      installed_role=local_message
    mode: '0644'
EOF
cat > vendor_src/local_message/meta/main.yml <<'EOF'
---
galaxy_info:
  author: LabEx
  description: Local role for RHCE Ansible prep
  license: MIT
  min_ansible_version: '2.16'
dependencies: []
EOF

The role source has the same basic shape as a normal role: task logic under tasks/main.yml and role metadata under meta/main.yml. The task writes a visible message so you can prove the installed role executed.

tar -czf vendor_src/local_message.tar.gz -C vendor_src local_message

The archive simulates an offline role package. Using a local archive keeps the lab deterministic while still teaching the requirements.yml workflow.

ls -l vendor_src/local_message.tar.gz

Install the Role from Requirements

In this step, you will write a role requirements file that points at the local archive and install it into roles. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
cat > requirements.yml <<'EOF'
---
roles:
  - name: local_message
    src: file:///home/labex/project/rhce-ansible/vendor_src/local_message.tar.gz
EOF

The file:// source points ansible-galaxy at the local archive. This is the same idea as using a remote source, but without depending on network access.

ansible-galaxy role install -r requirements.yml -p roles --force

The -r option reads requirements, -p roles chooses the install directory, and --force makes the command repeatable if you rerun the lab.

find roles/local_message -maxdepth 2 -type f -print

You should see the installed role's tasks/main.yml and meta/main.yml. The playbook will use the installed role path, not the original vendor_src directory.

Inspect Installed Collection Content

In this step, you will inspect an installed collection. The RHEL10 image already includes common collections, so this lab avoids an internet download while still showing collection discovery. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
ansible-galaxy collection list ansible.posix | tee reports/collection-list.txt
ansible-doc ansible.posix.acl | sed -n "1,20p"

Collections package modules under a namespace. Here ansible.posix.acl means the acl module from the ansible.posix collection, and ansible-doc proves Ansible can find its documentation locally.

Apply the Installed Role

In this step, you will apply the role installed from requirements. This proves Ansible can find and execute the installed role content. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
cat > playbooks/use-installed-role.yml <<'EOF'
---
- name: Use a locally installed role
  hosts: managed
  gather_facts: false
  roles:
    - role: local_message
EOF

The playbook references only local_message because roles_path points at the install directory. That keeps the playbook independent from the archive location.

ansible-playbook playbooks/use-installed-role.yml | tee reports/installed-role-run.txt

The run should finish with failed=0 and create the message file from the installed role's task.

cat managed/local-message/message.txt

Summary

You packaged and installed a local role through requirements.yml, inspected installed collection content, and applied the installed role from a playbook.