In this exercise, you configure a new Yum repository and install packages from it on your managed hosts.
Outcomes
Configure a Yum repository using the ansible.builtin.yum_repository module.
Manage RPM GPG keys using the ansible.builtin.rpm_key module.
Obtain information about the installed packages on a host using the ansible.builtin.package_facts module.
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 system-software
Procedure 9.1. Instructions
Your organization requires that the simple-agent RPM package be installed on all hosts.
This package is provided by an internal Yum repository maintained by your organization to host its internally developed software packages.
You need to write a playbook to ensure that the simple-agent package is installed on all managed hosts.
The playbook must also ensure that all managed hosts are configured to use the internal Yum repository.
The repository is located at http://materials.example.com/yum/repository.
All RPM packages in the repository are signed with a GPG key pair.
The GPG public key for the repository packages is available at http://materials.example.com/yum/repository/RPM-GPG-KEY-example.
Change to the /home/student/system-software directory.
[student@workstation ~]$ cd ~/system-software
[student@workstation system-software]$Begin writing the repo_playbook.yml playbook.
Define a single play in the playbook that targets all hosts.
Add a vars clause to the play that defines a single variable, custom_pkg, with the value simple-agent (the name of the RPM package that needs to be installed everywhere.)
Add an empty tasks clause to the play.
The playbook should consist of the following content:
---
- name: Repository Configuration
hosts: all
vars:
custom_pkg: simple-agent
tasks:Add two tasks to the tasks clause of the play in the repo_playbook.yml file.
Use the ansible.builtin.package_facts module in the first task to gather information about installed packages on the managed hosts.
This task populates the ansible_facts.packages fact.
Use the ansible.builtin.debug module in the second task to print the installed version of the package referenced by the custom_pkg variable.
Only run this task if the custom package is found in the ansible_facts.packages fact.
Run the repo_playbook.yml playbook.
Add the first task to the play.
Set the value of the manager keyword to auto for the ansible.builtin.package_facts module.
The first task should consist of the following content:
- name: Gather Package Facts
ansible.builtin.package_facts:
manager: autoAdd a second task to the play that uses the ansible.builtin.debug module to display the value of the ansible_facts.packages[custom_pkg] variable.
Add a when clause to the task to verify that the value of the custom_pkg variable is contained in the ansible_facts['packages'] variable.
The second task should consist of the following content:
- name: Show Package Facts for the custom package
ansible.builtin.debug:
var: ansible_facts['packages'][custom_pkg]
when: custom_pkg in ansible_facts['packages']Run the playbook:
[student@workstation system-software]$ansible-navigator run \>-m stdout repo_playbook.ymlPLAY [Repository Configuration] ********************************************** TASK [Gathering Facts] ******************************************************* ok: [servera.lab.example.com] TASK [Gather Package Facts] ************************************************** ok: [servera.lab.example.com] TASK [Show Package Facts for the custom package] *****************************skipping: [servera.lab.example.com]PLAY RECAP ******************************************************************* servera.lab.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The Show Package Facts for the custom package task is skipped because the simple-agent package is not installed on the managed hosts.
Add a third task to the play that uses the ansible.builtin.yum_repository module to ensure the internal Yum repository is configured on the managed hosts.
This task has the following requirements:
The repository configuration is stored in the file /etc/yum.repos.d/example.repo
The repository ID is example-internal
The repository base URL is http://materials.example.com/yum/repository
The repository is configured to check RPM GPG signatures
The repository description is Example Inc. Internal YUM repo
The third task should consist of the following content:
- name: Ensure Example Repo exists
ansible.builtin.yum_repository:
name: example-internal
description: Example Inc. Internal YUM repo
file: example
baseurl: http://materials.example.com/yum/repository/
gpgcheck: yesAdd a fourth task to the play that uses the ansible.builtin.rpm_key module to ensure that the repository's public key is present on the managed hosts.
The repository's public key is available at http://materials.example.com/yum/repository/RPM-GPG-KEY-example.
The fourth task should consist of the following content:
- name: Ensure Repo RPM Key is Installed
ansible.builtin.rpm_key:
key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
state: presentAdd a fifth task to the play that ensures that the package referenced by the custom_pkg variable is installed on the managed hosts.
The fifth task should consist of the following content:
- name: Install Example package
ansible.builtin.dnf:
name: "{{ custom_pkg }}"
state: presentThe ansible_facts['packages'] fact is not automatically updated when a new package is installed on a managed host.
This step demonstrates that this is true.
Copy the second task and add it as the sixth task in the play.
Run the playbook and verify that the ansible_facts['packages'] fact does not contain information about the simple-agent package installed on the managed hosts.
The sixth task contains a copy of the second task:
- name: Show Package Facts for the custom package
ansible.builtin.debug:
var: ansible_facts['packages'][custom_pkg]
when: custom_pkg in ansible_facts['packages']The entire repo_playbook.yml playbook should now consist of the following content:
---
- name: Repository Configuration
hosts: all
vars:
custom_pkg: simple-agent
tasks:
- name: Gather Package Facts
ansible.builtin.package_facts:
manager: auto
- name: Show Package Facts for the custom package
ansible.builtin.debug:
var: ansible_facts['packages'][custom_pkg]
when: custom_pkg in ansible_facts['packages']
- name: Ensure Example Repo exists
ansible.builtin.yum_repository:
name: example-internal
description: Example Inc. Internal YUM repo
file: example
baseurl: http://materials.example.com/yum/repository/
gpgcheck: yes
- name: Ensure Repo RPM Key is Installed
ansible.builtin.rpm_key:
key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
state: present
- name: Install Example package
ansible.builtin.dnf:
name: "{{ custom_pkg }}"
state: present
- name: Show Package Facts for the custom package
ansible.builtin.debug:
var: ansible_facts['packages'][custom_pkg]
when: custom_pkg in ansible_facts['packages']Run the playbook.
[student@workstation system-software]$ansible-navigator run \>-m stdout repo_playbook.ymlPLAY [Repository Configuration] ********************************************** TASK [Gathering Facts] ******************************************************* ok: [servera.lab.example.com] TASK [Gather Package Facts] ************************************************** ok: [servera.lab.example.com]TASK [Show Package Facts for the custom package] ***************************** skipping: [servera.lab.example.com] TASK [Ensure Example Repo exists] ******************************************** changed: [servera.lab.example.com] TASK [Ensure Repo RPM Key is Installed] ************************************** changed: [servera.lab.example.com] TASK [Install Example package] ****************************************** changed: [servera.lab.example.com] TASK [Show Package Facts for the custom package] ***************************** skipping: [servera.lab.example.com]
PLAY RECAP ******************************************************************* servera.lab.example.com : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Update the package facts in your play by inserting a task immediately after the Install Example package task.
Write the new task so that it runs the ansible.builtin.package_facts module.
Set the module's manager attribute to auto.
The complete playbook should consist of the following content:
---
- name: Repository Configuration
hosts: all
vars:
custom_pkg: simple-agent
tasks:
- name: Gather Package Facts
ansible.builtin.package_facts:
manager: auto
- name: Show Package Facts for the custom package
ansible.builtin.debug:
var: ansible_facts['packages'][custom_pkg]
when: custom_pkg in ansible_facts['packages']
- name: Ensure Example Repo exists
ansible.builtin.yum_repository:
name: example-internal
description: Example Inc. Internal YUM repo
file: example
baseurl: http://materials.example.com/yum/repository/
gpgcheck: yes
- name: Ensure Repo RPM Key is Installed
ansible.builtin.rpm_key:
key: http://materials.example.com/yum/repository/RPM-GPG-KEY-example
state: present
- name: Install Example package
ansible.builtin.dnf:
name: "{{ custom_pkg }}"
state: present
- name: Gather Package Facts
ansible.builtin.package_facts:
manager: auto
- name: Show Package Facts for the custom package
ansible.builtin.debug:
var: ansible_facts['packages'][custom_pkg]
when: custom_pkg in ansible_facts['packages']Use an Ansible ad hoc command to remove the simple-agent package installed during the previous execution of the playbook.
Run the playbook with the inserted ansible.builtin.package_facts task and use the output to verify the installation of the simple-agent package.
To remove the simple-agent package from all hosts, use the ansible all command with the -m ansible.builtin.dnf and -a 'name=simple-agent state=absent' options.
[student@workstation system-software]$ansible all -m ansible.builtin.dnf \>-a 'name=simple-agent state=absent'servera.lab.example.com | CHANGED => { ...output omitted... "changed": true, "msg": "", "rc": 0, "results": [ "Removed: simple-agent-1.0-1.el9.x86_64" ] ...output omitted...
Run the repo_playbook.yml playbook.
[student@workstation system-software]$ansible-navigator run \>-m stdout repo_playbook.ymlPLAY [Repository Configuration] ********************************************** TASK [Gathering Facts] ******************************************************* ok: [servera.lab.example.com] TASK [Gather Package Facts] ************************************************** ok: [servera.lab.example.com] TASK [Show Package Facts for the custom package] ***************************** skipping: [servera.lab.example.com]...output omitted... TASK [Install Example package] *********************************************** changed: [servera.lab.example.com]
TASK [Gather Package Facts] ************************************************** ok: [servera.lab.example.com]
TASK [Show Package Facts for the custom package] ***************************** ok: [servera.lab.example.com] => { "ansible_facts['packages'][custom_pkg]": [
{ "arch": "x86_64", "epoch": null, "name": "simple-agent", "release": "1.el9", "source": "rpm", "version": "1.0" } ] } PLAY RECAP ******************************************************************* servera.lab.example.com : ok=7 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
No package fact exists for the | |
The | |
This task updates the package facts with information about the | |
The |
This concludes the section.