Write a playbook that uses modules from an Ansible Content Collection.
Outcomes
Identify the Ansible Content Collection that provides a specific module.
Create a task that specifies a module by its fully qualified collection name.
Use the collections keyword in a playbook.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that a project directory is created with the required files for this exercise.
[student@workstation ~]$ lab start manage-reusing
Procedure 2.1. Instructions
Change to the /home/student/manage-reusing/ directory and attempt to run the basic-web.yml playbook by using the ansible-playbook command.
Change to the /home/student/manage-reusing/ directory and list the contents of the directory.
[student@workstation ~]$cd ~/manage-reusing/[student@workstation manage-reusing]$lsansible.cfg basic-web.yml inventory
Use the ansible-playbook command to attempt to run the basic-web.yml playbook.
The attempt fails with an error message.
[student@workstation manage-reusing]$ ansible-playbook basic-web.yml
ERROR! couldn't resolve module/action 'firewalld'. This often indicates a misspelling, missing collection, or incorrect module path.
...output omitted...Attempt to find the firewalld module in the default modules installed by the ansible-core package.
The command does not return output indicating that the module is not found.
[student@workstation manage-reusing]$ ansible-doc -l | grep firewalldThe ansible-playbook command provided by Red Hat Ansible Automation Platform 2 is based on the Ansible Core 2.13 package.
It does not include any Ansible Content Collections other than ansible.builtin, which is why the preceding two substeps fail.
Your control node also does not have any additional Ansible Content Collections installed.
The firewalld module was moved into a separate collection after Ansible 2.9.
Use automation content navigator with the default ee-supported-rhel8 execution environment to identify the Ansible Content Collection that provides the firewalld module.
Use the podman login command to log in to the classroom private automation hub at hub.lab.example.com.
Log in as student with redhat123 as the password.
This step simulates a customer logging in to registry.redhat.io and ensures that the specified automation execution environment is pulled down to the local system if it does not already exist.
[student@workstation manage-reusing]$podman login hub.lab.example.comUsername:studentPassword:redhat123Login Succeeded!
List the module documentation available within the ee-supported-rhel8 automation execution environment.
Notice the colon at then end of the output which means that the output is extensive.
[student@workstation manage-reusing]$ansible-navigator doc -l \>--eei ee-supported-rhel8 --pp missing -m stdoutadd_host Add a host (and alternatively a group) to the ... amazon.aws.aws_az_info Gather information about availability zones in ... amazon.aws.aws_caller_info Get information about the user and account ... amazon.aws.aws_s3 manage objects in S3 amazon.aws.cloudformation Create or delete an AWS CloudFormation stack ...output omitted... :
Type /firewalld to search the module documentation for firewalld.
The ansible.posix content collection provides the firewalld module.
add_host Add a host (and alternatively a group) to the ...
amazon.aws.aws_az_info Gather information about availability zones in ...
amazon.aws.aws_caller_info Get information about the user and account ...
amazon.aws.aws_s3 manage objects in S3
amazon.aws.cloudformation Create or delete an AWS CloudFormation stack
...output omitted...
/firewalldThe fully qualified collection name for the firewalld module is ansible.posix.firewalld.
ansible.posix.firewalldManage arbitrary ports/services withfirewalldansible.posix.firewalld_info Gather information about firewalld ...output omitted...
Press q to exit the ansible-navigator command.
Create a copy of the basic-web.yml playbook to a file named basic-web-fqcn.yml and then update the new file to use fully qualified collection names for each module.
Create a copy of the basic-web.yml playbook named basic-web-fqcn.yml.
[student@workstation manage-reusing]$ cp basic-web.yml basic-web-fqcn.ymlUpdate the basic-web-fqcn.yml playbook to use the fully qualified collection names for each module.
The ansible.posix collection provides the firewalld module.
The ansible.builtin collection provides the other modules.
The modified basic-web-fqcn.yml playbook should consist of the following content:
---
- name: Configure a basic web server
hosts: servere.lab.example.com
become: true
tasks:
- name: Install software
ansible.builtin.yum:
name:
- httpd
- firewalld
state: latest
- name: Start and enable services
ansible.builtin.service:
name: "{{ item }}"
state: started
enabled: true
loop:
- httpd
- firewalld
- name: Open access to http
ansible.posix.firewalld:
service: http
immediate: true
permanent: true
state: enabled
- name: Configure simple index.html
ansible.builtin.copy:
content: "Hello world from {{ ansible_facts['fqdn'] }}.\n"
dest: /var/www/html/index.htmlAlthough not currently required, Red Hat recommends that you use the fully qualified collection name.
Use the ee-supported-rhel8 automation execution environment to verify that the basic-web-fqcn.yml playbook completes successfully.
Run the basic-web-fqcn.yml playbook.
[student@workstation manage-reusing]$ansible-navigator run \>basic-web-fqcn.yml --eei ee-supported-rhel8 --pp missingPlay name Ok Changed ... Failed ... Task count Progress 0│Configure a basic web server 5 4 ... 0 ... 5 Complete ^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back ...Successful
When the Progress column shows Successful, press ESC to exit from the ansible-navigator command.
Use the curl command to display web content on servere.lab.example.com.
[student@workstation manage-reusing]$ curl servere.lab.example.com
Hello world from servere.lab.example.com.Create a copy of the basic-web.yml playbook named basic-web-keyword.yml.
Modify the basic-web-keyword.yml playbook so that it defines the included collections.
Create a copy of the basic-web.yml playbook.
[student@workstation manage-reusing]$ cp basic-web.yml basic-web-keyword.ymlDefine the ansible.builtin and ansible.posix collections in the basic-web-keyword.yml playbook using the collections keyword.
Configure the play to target serverf.lab.example.com instead of servere.lab.example.com.
The top of the modified playbook should consist of the following content:
--- - name: Configure a basic web serverhosts: serverf.lab.example.combecome: truecollections:- ansible.builtin- ansible.posixtasks: ...output omitted...
Use the ee-supported-rhel8 automation execution environment to verify that the basic-web-keyword.yml playbook completes successfully.
Run the basic-web-keyword.yml playbook.
[student@workstation manage-reusing]$ansible-navigator run \>basic-web-keyword.yml --eei ee-supported-rhel8 --pp missingPlay name Ok Changed ... Failed ... Task count Progress 0│Configure a basic web server 5 4 ... 0 ... 5 Complete ^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back ...Successful
When the Progress column shows Successful, press ESC to exit from the ansible-navigator command.
Use the curl command to display web content on serverf.lab.example.com.
[student@workstation manage-reusing]$ curl serverf.lab.example.com
Hello world from serverf.lab.example.com.