Run Ansible Playbooks to manage Red Hat Satellite infrastructure.
Outcomes
Create a variable file with Satellite access settings for use with Satellite playbooks.
List Satellite organizations by using a Satellite Ansible Collection playbook.
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 api-ansible
Instructions
On the workstation system, verify that the installed Ansible is version 2.9 or later.
[student@workstation ~]$ ansible --version
ansible [core 2.12.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/student/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.9.10 (main, Feb 9, 2022, 00:00:00) [GCC 11.2.1 20220127
(Red Hat 11.2.1-9)]
jinja version = 2.11.3
libyaml = TrueInstall the Satellite Ansible Collection. The collection installs to the default Ansible component location.
[student@workstation ~]$ ansible-galaxy collection install \
redhat-satellite-3.6.0.tgzCreate the ansible-lab working directory to store your Ansible files.
[student@workstation ~]$mkdir ansible-lab[student@workstation ~]$cd ansible-lab[student@workstation ansible-lab]$
Create the sat_vars.yml variable file for Satellite Server access parameters.
Use the following content and values.
The sat_vars.yml file should look like the following.
[student@workstation ansible-lab]$cat sat_vars.yml--- sat_url: https://satellite.lab.example.com sat_un: admin sat_pw: redhat cert_validate: no
Create the sat_org_list.yml playbook to list existing Satellite organizations.
Create the sat_org_list.yml file with the following content.
[student@workstation ansible-lab]$cat sat_org_list.yml--- - name: Example List Organizations hosts: localhost vars_files: sat_vars.yml
List the available plug-ins to locate the module that lists organization information.
The module is in the redhat.satellite collection.
[student@workstation ansible-lab]$ ansible-doc --list
...output omitted...
redhat.satellite.operatingsystem Manage Operating Systems
redhat.satellite.organization Manage Organizations
redhat.satellite.organization_info Get information about organization(s)
redhat.satellite.os_default_template Manage Default Template Associations
...output omitted...Read the module documentation to discover the required module parameters and to view example playbook syntax. Use the module's fully qualified name.
[student@workstation ansible-lab]$ ansible-doc redhat.satellite.organization_info
...output omitted...Add a task to the sat_org_list.yml playbook to list organizations.
A search parameter with an empty attribute filter returns all organizations.
Edit the sat_org_list.yml playbook to include the following content.
[student@workstation ansible-lab]$cat sat_org_list.yml--- - name: Example List Organizations hosts: localhost vars_files: sat_vars.ymltasks: - name: List Satellite Organizations redhat.satellite.organization_info: server_url: "{{ sat_url }}" username: "{{ sat_un }}" password: "{{ sat_pw }}" validate_certs: "{{ cert_validate }}" search: " "
Run the playbook and view the output. Use command options and playbook modifications to improve the output.
Run the playbook without options. Correct any failures and run the playbook again until successful. The play recap states that the playbook succeeded, but the organization list output is not displayed to STDOUT by default.
[student@workstation ansible-lab]$ ansible-playbook sat_org_list.yml
PLAY [Example List Organizations] ************************************************
TASK [Gathering Facts] ***********************************************************
ok: [localhost]
TASK [List Satellite Organizations] **********************************************
ok: [localhost]
PLAY RECAP ***********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Run the playbook with the verbose option. The organization list output is now visible as a JSON-structured stream. The organization list output might differ on your system.
[student@workstation ansible-lab]$ ansible-playbook sat_org_list.yml -v
PLAY [Example List Organizations] ************************************************
TASK [Gathering Facts] ***********************************************************
ok: [localhost]
TASK [List Satellite Organizations] **********************************************
ok: [localhost] => {"changed": false, "organizations": [{"created_at": "2019-11-08 12:15:18 UTC", "description": null, "id": 1, "label": "Default_Organization", "name": "Default Organization", "title": "Default Organization", "updated_at": "2019-11-08 12:15:18 UTC"}, {"created_at": "2022-03-30 17:17:16 UTC", "description": "Operations Department", "id": 2, "label": "Operations", "name": "Operations", "title": "Operations", "updated_at": "2022-03-30 17:17:16 UTC"}, {"created_at": "2022-03-30 17:17:16 UTC", "description": "Finance Department", "id": 3, "label": "Finance", "name": "Finance", "title": "Finance", "updated_at": "2022-03-30 17:17:16 UTC"}]}
PLAY RECAP ***********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Modify the playbook to use a named register to capture and print the organization list in the default JSON output.
[student@workstation ansible-lab]$cat sat_org_list.yml--- - name: Example List Organizations hosts: localhost vars_files: sat_vars.yml tasks: - name: List Satellite Organizations redhat.satellite.organization_info: server_url: "{{ sat_url }}" username: "{{ sat_un }}" password: "{{ sat_pw }}" validate_certs: "{{ cert_validate }}" search: " "register: sat_orgs - name: Print List of Satellite Organizations ansible.builtin.debug: var: sat_orgs
Run the playbook without options. The organization list JSON-formatted output is easier to read.
[student@workstation ansible-lab]$ ansible-playbook sat_org_list.yml
PLAY [Example List Organizations] ************************************************
TASK [Gathering Facts] ***********************************************************
ok: [localhost]
TASK [List Satellite Organizations] **********************************************
ok: [localhost]
TASK [Print List of Satellite Organizations] *************************************
ok: [localhost] => {
"sat_orgs": {
"changed": false,
"failed": false,
"organizations": [
{
"create at": "2022-07-14 16:54:31 UTC",
"description": null,
"id": 1,
"label": "Default Organization",
"name": "Default Organization",
"title": "Default Organization",
"updated at": "2022-07-14 16:59:55 UTC"
},
...output omitted...
]
}
}
PLAY RECAP ***********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Modify the playbook to display parsed data from the JSON output.
Print only the name attribute for each organization.
Replace the var: entry with the msg: entry.
The sat_org_list.yml file should look like the following.
---
- name: Example List Organizations
hosts: localhost
vars_files: sat_vars.yml
tasks:
- name: List Satellite Organizations
redhat.satellite.organization_info:
server_url: "{{ sat_url }}"
username: "{{ sat_un }}"
password: "{{ sat_pw }}"
validate_certs: "{{ cert_validate }}"
search: " "
register: sat_orgs
- name: Print List of Satellite Organizations
ansible.builtin.debug:
msg: "{{ sat_orgs['organizations'] | map(attribute='name') | list }}"Run the playbook without options. The output is limited to only the information that you parsed.
[student@workstation ansible-lab]$ ansible-playbook sat_org_list.yml
PLAY [Example List Organizations] ************************************************
TASK [Gathering Facts] ***********************************************************
ok: [localhost]Demo to
TASK [List Satellite Organizations] **********************************************
ok: [localhost]
TASK [Print List of Satellite Organizations] *************************************
ok: [localhost] => {
"msg": "['Default Organization', 'Finance', 'Operations']"
PLAY RECAP ***********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Return to the student home directory.
[student@workstation ansible-lab]$ cd
[student@workstation ~]$