Ansible Cheat Sheet

A collection of some useful Ansible commands

# show Ansible version, module search path, collection location, config file and python version
ansible --version
# list all available Ansible modules
ansible-doc -l
# show documentation about a module
ansible-doc ansible.builtin.setup
# manually run a module on a host
ansible -m ansible.builtin.setup localhost
ansible lin2.fritz.box -i lin2.fritz.box, -m ansible.builtin.setup # create a temporary inventory and run the module on the host
# run an ad hoc command on all hosts
ansible -i myhosts all -a "dnf list installed|grep ^httpd.x86_64|wc -l" -b -m shell
# add public key of the Ansible Control Node user to authorized_keys file
# of the Managed Node
ansible all -k --ssh-common-args='-o StrictHostKeyChecking=no' -m authorized_key -a "user=vagrant key='{{ lookup('file', '/home/user1/.ssh/id_rsa.pub') }}'"
# or
ansible all -u vagrant --private-key=~/.vagrant.d/insecure_private_key --ssh-common-args='-o StrictHostKeyChecking=no' \
  -m authorized_key -a "user=vagrant key='{{ lookup('file', '/home/user1/.ssh/id_rsa.pub') }}'"

Creating a custom RHEL 9 AAP Execution Environment that contains python3-dnf

This needs to be done, if you receive errors like the following:

Could not import the dnf python module using /usr/bin/python3.12 (3.12.12(main, Feb 27 2026, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-11)]). Please install python3-dnf` or `python2-dnf` package or ensure you have specified the correct ansible_python_interpreter.

Run the following code as the aap user to create and upload the created custom image:

cat >execution-environment.yml <<EOF
---
version: 3

images:
  base_image:
    name: 'registry.redhat.io/ansible-automation-platform-24/ee-supported-rhel9:latest'

dependencies:
  system:
    - python3-dnf
	- libxml2-devel
EOF
rm -rf context
ansible-builder create
sed -i '0,/^FROM / s/^FROM .*/&\nRUN microdnf install -y dnf/' context/Containerfile
podman build -t custom_rhel9-ee:latest context/
podman tag custom_rhel9-ee:latest lin2.fritz.box/custom_rhel9-ee:latest
podman login lin2.fritz.box
podman push lin2.fritz.box/custom_rhel9-ee:latest

After the upload to AAP is complete a new execution environment can be created:

  • Automation Execution => Infrastructure => Execution Environments => Create execution environment
  • Under Image enter: lin2.fritz.box/custom_rhel9-ee
0