10.2 Ansible-Runner
In this lab, we will learn about ansible-runner
, the component of AAP Controller / AWX that actually runs Ansible playbooks.
Task 1
- Install all packages needed to use
ansible-runner
command line tool on the controller host. (In case of python problems, have a look at the ansible-builder lab. - Show the help context of the
ansible-runner
command.
Solution Task 1
Since we have no Red Hat Subscription available, we install ansible-runner with pip. We install podman as well to be able to use containers.
|
|
Note that ansible-runner
is already present when you installed ansible-navigator
in the labs before.
Task 2
- Set up the folder structure needed by ansible-runner to find your inventory and put your playbook in the correct folder as well.
- Use
ansible-runner
to run the play site.yml from Lab 10.0 Task 3.
Solution Task 2
|
|
Task 3
- Add a task to
site.yml
playbook that waits for 120 seconds. The tasks should be at the end of the play running on the web groupweb
. - Use
ansible-runner
cli to run the playbooksite.yml
. - Have a look at the running process in the
artifacts
directory. Find information about the facts cached for node1. - Find information about a random job event.
- Show the standard output of the ansible run`
Solution Task 3
|
|
NOTE: the output of the tree
command varies depending on when it is run.
$ cat 82b7743d-72db-4760-b163-e24257a5ff78/fact_cache/node1
{
"_ansible_facts_gathered": true,
"ansible_all_ipv4_addresses": [
"5.102.147.253"
],
"ansible_all_ipv6_addresses": [
"fe80::5842:5ff:fe66:93fd"
],
"ansible_apparmor": {
"status": "disabled"
...
$ cat artifacts/82b7743d-72db-4760-b163-e24257a5ff78/job_events/1-54889cf9-5ceb-4541-930f-73fe24d4a08a.json
...
$ cat artifacts/82b7743d-72db-4760-b163-e24257a5ff78/stdout
PLAY [Run tasks on webservers] *************************************************
TASK [Gathering Facts] *********************************************************
ok: [node1]
TASK [install httpd] ***********************************************************
ok: [node1]
TASK [start and enable httpd] **************************************************
ok: [node1]
TASK [start and enable firewalld] **********************************************
ok: [node1]
TASK [open firewall for http] **************************************************
ok: [node1]
TASK [Sleep for 120 seconds] ***************************************************
ok: [node1]
PLAY [Run tasks on dbservers] **************************************************
TASK [Gathering Facts] *********************************************************
ok: [node2]
TASK [prepare motd] ************************************************************
ok: [node2]
PLAY RECAP *********************************************************************
node1 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Task 4
Use ansible-runner
cli to run the playbook site.yml
inside an execution environment (ee). If you did the ansible-builder
labs, you can use the ee you created then. Otherwise use quay.io/ansible/ansible-navigator-demo-ee
. We want to use podman
to execute the ee and also use process isolation. See ansible-runner run --help
for which options you have to use.
Since it is run inside a container, we have to specify with which user we want to run Ansible. Default would be the root user which is not a best practice. Also, we add a working ssh_key to the correct location in our directory. This is needed in order to open an ssh-connetion from inside the ee to the managed nodes.
Tip
If the ee you choose is not present yet, podman will first pull it when runningansible-runner
. This can take some time.- create the needed folder, files and its content for using the ee
- create the needed folder, files and its content for using
ansible
asremote_user
and the corresponding ssh_key - run the playbook
site.yml
inside the ee with theansible-runner
cli
Solution Task 4
|
|
Warning
Not defining process_isolation_executable: podman
in env/settings
would lead to ansible-runner
run our playbook NOT inside an ee. The output however looks almost the same.
Task 5
Now we want to have a look at whats happening in the background while running our playbook with ansible-runner
inside an ee.
- Start your playbook in the background. Use
ansible-runner --help
to see which option you can use. - While the playbook runs, have look at the running processes with
watch -n1 'ps -ef | grep ansible- | grep -v grep'
- Also while the playbook runs, have a look at the running containers with
watch -n1 podman ps -a
- While the playbook still runs, stop it with corresponding option and see how the processes are terminated and the running container stopped.
Solution Task 5
|
|
Task 6
Now we want to run our playbook site.yml
by starting an execution environment with podman and mounting our metadata folder into the correct location inside the ee. Have a look at the documentation for help.
Tip
If you have selinux in enforcing mode, remember to relabel the volumes mounted inside the container with:Z
.Warning
Remove yourpodman
-settings in env/settings
. Otherwise, ansible-runner
would try to run Ansible inside the container with podman
again. This would fail since its not installed inside the ansible-runner
image.- Use the ee
default-ee
from before or usequay.io/ansible/ansible-navigator-demo-ee
. Remember, that an ee is always based on the ansible-runner reference image. - How do you specify which playbook to run?
- Into which directory inside the container do you have to mount your metadata directory?
- Remove the podman-settings in the file
env/settings
. These settings would causeansible-runner
inside the container to try run in podman again. This would fail because podman is not installed inside the ee. - Run your Ansible project with podman using the ee stated above.
Solution Task 6
- With the env variable
RUNNER_PLAYBOOK=test.yml
. - Into the
/runner
directory. $ mv env/settings ../
- Run it:
|
|
Task 7 (Advanced)
- Why can’t you run your
site.yml
playbook with thequay.io/ansible/ansible-runner
image?
Solution Task 7
Our playbook contains a tasks that uses the ansible.posix.firewalld
module. The ansible-runner
ee contains nothing but ansible-core
. This means it cannot run the ansible.posix.firewalld
module because it’s not present in ansible-core
.
You can show the content of the ansible-builder
ee with ansible-navigator
:
|
|
See the ansible-navigator lab about how to get there!
Task 8
Now we want to run our playbook directly by using the python module ansible-runner
.
- Install the python module
ansible-runner
. - Create a python script
run_ansible_run.py
that runs your playbooksite.yml
withansible-runner
. Ansible-runner
should not use an ee yet.- The script should use
/home/ansible/techlab/
as the ansible metadata directory. - Make the script executable.
- Run the script.
- After the Ansible run completed successfully, change things to run it inside an ee.
Solution Task 8
|
|
For running ansible-runner
from the python script inside an ee, just ensure that the settings
file is present in the env
folder.
|
|
Now, when you start the run_ansible_run.py
script again, Ansible is run inside the defined ee. You can check this with $ watch -n1 podman ps -a
while running the script.
All done?
- Have a look at the ansible-runner github page
- Have a look at the docs of the latest version of ansible-runner
- Have a look at the documentation of the python module and see what the library is capable of.