First Steps with the RedHat Ansible Automation Platform (AAP) – Configuration as Code

After the installation of the RedHat Ansible Automation Platform (AAP) the platform is still empty. This post shows how to create the first objects (an organization with a team and a user, a credential, an inventory, a project, job templates with a survey, a schedule and a workflow) and how to run the first jobs. Instead of clicking through the GUI, everything is defined as code with the collections ansible.platform and ansible.controller.

This is the first part of a small series. The next posts show how to read secrets from HashiCorp Vault, how to install EDB Postgres Advanced Server (EPAS) with Failover Manager (EFM) and how to patch the database cluster with AAP.

The Lab Environment

  • lin2 (RHEL 9.7): AAP 2.6 containerized (growth topology) and the Gitea Git server with the repository aap-demo
  • lin6, lin7 and lin8 (RHEL 10.1): the managed nodes. In the later posts lin7 and lin8 become the EPAS database servers and lin6 the EFM witness.

The AAP objects build on each other: An organization contains the teams, users and all automation objects. A project points to a Git repository with playbooks. A job template combines a playbook of a project with an inventory (which hosts) and a credential (how to log in). A workflow template chains several job templates, and a schedule starts a template at a given time.

Preparing the Managed Nodes

My Windows 11 host has 32 GB of RAM. The AAP VM lin2 needs 16 GB, so the three managed nodes get less memory than in the earlier EPAS posts (8 GB each): 4 GB for the database servers lin7 and lin8 and 2 GB for the witness lin6. This is enough for a test cluster. We change the memory on the host system (in PowerShell):

# shut down the managed nodes (if they are running) and wait until they are off
foreach ($vm in 'lin6','lin7','lin8') { vboxmanage controlvm $vm shutdown 2>$null }
while ((vboxmanage list runningvms) -match '"lin[678]"') { Start-Sleep -Seconds 5 }
# lin6 (witness: EFM agent, later the pgBackRest repository) gets 2 GB
vboxmanage modifyvm lin6 --memory=2048
# lin7 and lin8 (EPAS) get 4 GB each
vboxmanage modifyvm lin7 --memory=4096
vboxmanage modifyvm lin8 --memory=4096
# check the memory settings and start the VMs without a window
foreach ($vm in 'lin6','lin7','lin8') { vboxmanage showvminfo $vm --machinereadable | Select-String '^memory=' }
foreach ($vm in 'lin6','lin7','lin8') { vboxmanage startvm $vm }

AAP connects to the managed nodes with SSH as the user ansible, which may run commands as root with sudo. As the user aap on lin2 we create an SSH key pair and prepare the three VMs. We log in as root here, so ssh asks for the root password of each VM (in my lab it is simply changeme):

# run as aap on lin2
# create an SSH key pair without a passphrase
ssh-keygen -t ed25519 -f ~/.ssh/aap_lab -N '' -C 'aap lab key'
# create the user ansible on the managed nodes and authorize the key
for h in lin6 lin7 lin8; do
  ssh -o StrictHostKeyChecking=accept-new root@$h.fritz.box "useradd -m ansible
    echo 'ansible ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/ansible
    chmod 440 /etc/sudoers.d/ansible
    mkdir -m 700 /home/ansible/.ssh
    echo '$(cat ~/.ssh/aap_lab.pub)' > /home/ansible/.ssh/authorized_keys
    chmod 600 /home/ansible/.ssh/authorized_keys
    chown -R ansible:ansible /home/ansible/.ssh
    restorecon -R /home/ansible/.ssh"
done
# test the login and sudo
for h in lin6 lin7 lin8; do ssh -i ~/.ssh/aap_lab ansible@$h.fritz.box 'hostname; sudo whoami'; done
Sample Output (click to expand):
[aap@lin2 ~]$ # run as aap on lin2
# create an SSH key pair without a passphrase
ssh-keygen -t ed25519 -f ~/.ssh/aap_lab -N '' -C 'aap lab key'
# create the user ansible on the managed nodes and authorize the key
for h in lin6 lin7 lin8; do
  ssh -o StrictHostKeyChecking=accept-new root@$h.fritz.box "useradd -m ansible
    echo 'ansible ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/ansible
    chmod 440 /etc/sudoers.d/ansible
    mkdir -m 700 /home/ansible/.ssh
    echo '$(cat ~/.ssh/aap_lab.pub)' > /home/ansible/.ssh/authorized_keys
    chmod 600 /home/ansible/.ssh/authorized_keys
    chown -R ansible:ansible /home/ansible/.ssh
    restorecon -R /home/ansible/.ssh"
done
# test the login and sudo
for h in lin6 lin7 lin8; do ssh -i ~/.ssh/aap_lab ansible@$h.fritz.box 'hostname; sudo whoami'; done
Generating public/private ed25519 key pair.
Your identification has been saved in /home/aap/.ssh/aap_lab
Your public key has been saved in /home/aap/.ssh/aap_lab.pub
The key fingerprint is:
SHA256:OuwcvmzTJcFWspRCZBW9OuJBl3/EhK6LP/l2mWk+Y3I aap lab key
The key's randomart image is:
+--[ED25519 256]--+
|     o+.o+ .     |
|     .. + + .    |
|       + * +     |
|      . B o o    |
|     . oS= .     |
|     .o.= o .    |
|     .== * . +   |
|     ==o= o E    |
|     .*+.+.B.o   |
+----[SHA256]-----+
Warning: Permanently added 'lin6.fritz.box' (ED25519) to the list of known hosts.
root@lin6.fritz.box's password:
Warning: Permanently added 'lin7.fritz.box' (ED25519) to the list of known hosts.
root@lin7.fritz.box's password:
Warning: Permanently added 'lin8.fritz.box' (ED25519) to the list of known hosts.
root@lin8.fritz.box's password:
lin6.fritz.box
root
lin7.fritz.box
root
lin8.fritz.box
root
[aap@lin2 ~]$

Adding the Playbooks to the Git Repository

The repository aap-demo from the Gitea post gets three simple playbooks: one shows some OS information, one checks for pending updates and one installs packages which are entered in a survey. As the user aap on lin2 run:

cd ~/aap-demo
mkdir -p playbooks config
cat > playbooks/os_info.yml <<'EOF'
---
- name: Show some OS information
  hosts: all
  gather_facts: true
  tasks:
    - name: Print OS, kernel, CPUs and memory
      ansible.builtin.debug:
        msg: >-
          {{ ansible_facts.distribution }} {{ ansible_facts.distribution_version }},
          kernel {{ ansible_facts.kernel }}, {{ ansible_facts.processor_vcpus }} vCPUs,
          {{ ansible_facts.memtotal_mb }} MB RAM
EOF
cat > playbooks/os_check_updates.yml <<'EOF'
---
- name: Check for pending OS updates
  hosts: all
  gather_facts: false
  become: true
  tasks:
    - name: Run dnf check-update (rc 100 means updates are available)
      ansible.builtin.command: dnf -q check-update
      register: dnf_check
      changed_when: false
      failed_when: dnf_check.rc not in [0, 100]

    - name: Show the result
      ansible.builtin.debug:
        msg: "{{ 'No updates pending' if dnf_check.rc == 0 else dnf_check.stdout_lines }}"
EOF
cat > playbooks/os_install_packages.yml <<'EOF'
---
- name: Install the packages entered in the survey
  hosts: all
  gather_facts: false
  become: true
  vars:
    packages: vim
  tasks:
    - name: Install packages
      ansible.builtin.dnf:
        name: "{{ packages.split(',') | map('trim') | list }}"
        state: present
EOF
git add playbooks
git commit -m "Add OS playbooks"
git push

Running Playbooks in the Execution Environment

The collections ansible.platform (organizations, teams, users) and ansible.controller (credentials, inventories, projects, templates, …) are certified content from the Automation Hub. We don’t need to install them: The supported execution environment ee-supported, which the installer already loaded into the Podman image store of the user aap, contains both collections. So we simply run ansible-playbook inside this container image. The connection data for AAP is passed in the AAP_* environment variables, which all AAP 2.6 collections understand. To have the same settings in every new shell, we write them into the file ~/aap_env.sh and load it with source. The helper function stops with a clear message if the variables are missing. As the user aap on lin2 run:

# find the execution environment images and check the collections in the AAP 2.6 image
podman images | grep ee-
podman run --rm $(hostname)/ee-supported-rhel9:latest ansible-galaxy collection list | grep -E 'ansible\.(platform|controller)'
# write the settings for the AAP posts into a file (a new shell only needs: source ~/aap_env.sh)
cat > ~/aap_env.sh <<'EOF'
# execution environment: the AAP 2.6 image which the installer pushed into the local automation hub
EE=$(hostname)/ee-supported-rhel9:latest
# connection data for AAP (URL of the platform gateway)
export AAP_HOSTNAME=https://$(hostname)
export AAP_USERNAME=admin
export AAP_PASSWORD=password
export AAP_VALIDATE_CERTS=false
# variables used by the configuration playbooks
export AAP_LAB_KEY="$(cat ~/.ssh/aap_lab)"
export GITEA_HOST=$(hostname)
# helper function which runs a playbook in the execution environment (VAULT_* is used in the Vault post)
aap_play() { podman run --rm --network host -e 'AAP_*' -e 'VAULT_*' -e GITEA_HOST -v ~/aap-demo:/runner/project:Z -w /runner/project "${EE:?EE is not set, run: source ~/aap_env.sh}" ansible-playbook "$@"; }
EOF
chmod 600 ~/aap_env.sh
source ~/aap_env.sh
Sample Output (click to expand):
[aap@lin2 ~]$ # find the execution environment images and check the collections in the AAP 2.6 image
podman images | grep ee-
podman run --rm $(hostname)/ee-supported-rhel9:latest ansible-galaxy collection list | grep -E 'ansible\.(platform|controller)'
# write the settings for the AAP posts into a file (a new shell only needs: source ~/aap_env.sh)
cat > ~/aap_env.sh <<'EOF'
# execution environment: the AAP 2.6 image which the installer pushed into the local automation hub
EE=$(hostname)/ee-supported-rhel9:latest
# connection data for AAP (URL of the platform gateway)
export AAP_HOSTNAME=https://$(hostname)
export AAP_USERNAME=admin
export AAP_PASSWORD=password
export AAP_VALIDATE_CERTS=false
# variables used by the configuration playbooks
export AAP_LAB_KEY="$(cat ~/.ssh/aap_lab)"
export GITEA_HOST=$(hostname)
# helper function which runs a playbook in the execution environment (VAULT_* is used in the Vault post)
aap_play() { podman run --rm --network host -e 'AAP_*' -e 'VAULT_*' -e GITEA_HOST -v ~/aap-demo:/runner/project:Z -w /runner/project "${EE:?EE is not set, run: source ~/aap_env.sh}" ansible-playbook "$@"; }
EOF
chmod 600 ~/aap_env.sh
source ~/aap_env.sh
registry.redhat.io/ansible-automation-platform-24/ee-supported-rhel8       latest      d846e6dddff5  8 months ago  2.16 GB
lin2.fritz.box/ee-supported-rhel9                                          latest      f9dc4651f821  8 months ago  2.59 GB
registry.redhat.io/ansible-automation-platform-24/ee-supported-rhel9       latest      05a1ea63cc93  8 months ago  2.16 GB
ansible.controller              4.7.10
ansible.platform                2.6.20260306
[aap@lin2 ~]$

The Configuration Playbook

The playbook config/aap_config.yml creates all objects. All tasks are idempotent, so we can change the playbook and simply run it again. The SSH private key is not stored in Git but read from the environment variable AAP_LAB_KEY (in the next post it will come from HashiCorp Vault):

# run as aap on lin2
cd ~/aap-demo
cat > config/aap_config.yml <<'EOF'
---
- name: Configure the AAP objects for the lab
  hosts: localhost
  connection: local
  gather_facts: false
  vars:
    org: JBITC
    gitea_url: "http://{{ lookup('ansible.builtin.env', 'GITEA_HOST') }}:3000/gitadmin/aap-demo.git"
  tasks:
    # ----- platform gateway: organization, team and user -----
    - name: Create the organization
      ansible.platform.organization:
        name: "{{ org }}"
        description: JB IT Consulting Lab
      register: org_result

    - name: Create the team DBA
      ansible.platform.team:
        name: DBA
        organization: "{{ org }}"
      register: team_dba

    - name: Create the user jochen
      ansible.platform.user:
        username: jochen
        password: password
        email: jochen@example.com
        first_name: Jochen
        last_name: Bandhauer
        update_secrets: false

    - name: Make the user jochen a member of the organization
      ansible.platform.role_user_assignment:
        role_definition: Organization Member
        user: jochen
        object_ids: ["{{ org_result.id }}"]

    - name: Add the user jochen to the team DBA
      ansible.platform.role_user_assignment:
        role_definition: Team Member
        user: jochen
        object_ids: ["{{ team_dba.id }}"]

    # ----- automation controller -----
    - name: Create the machine credential for the lab VMs
      ansible.controller.credential:
        name: Lab SSH
        organization: "{{ org }}"
        credential_type: Machine
        inputs:
          username: ansible
          ssh_key_data: "{{ lookup('ansible.builtin.env', 'AAP_LAB_KEY') ~ '\n' }}"
          become_method: sudo

    - name: Create the inventory
      ansible.controller.inventory:
        name: Lab
        organization: "{{ org }}"

    - name: Add the hosts
      ansible.controller.host:
        name: "{{ item }}"
        inventory: Lab
      loop: [lin6.fritz.box, lin7.fritz.box, lin8.fritz.box]

    - name: Create the group dbservers
      ansible.controller.group:
        name: dbservers
        inventory: Lab
        hosts: [lin7.fritz.box, lin8.fritz.box]

    - name: Create the group witness
      ansible.controller.group:
        name: witness
        inventory: Lab
        hosts: [lin6.fritz.box]

    - name: Create the project (Git repository in Gitea)
      ansible.controller.project:
        name: aap-demo
        organization: "{{ org }}"
        scm_type: git
        scm_url: "{{ gitea_url }}"
        scm_branch: main
        scm_update_on_launch: true
        wait: true

    - name: Create the job templates
      ansible.controller.job_template:
        name: "{{ item.name }}"
        organization: "{{ org }}"
        project: aap-demo
        playbook: "{{ item.playbook }}"
        inventory: Lab
        credentials: [Lab SSH]
        ask_limit_on_launch: true
      loop:
        - {name: OS - Show Info, playbook: playbooks/os_info.yml}
        - {name: OS - Check Updates, playbook: playbooks/os_check_updates.yml}
      register: jt_os

    - name: Create the job template with a survey
      ansible.controller.job_template:
        name: OS - Install Packages
        organization: "{{ org }}"
        project: aap-demo
        playbook: playbooks/os_install_packages.yml
        inventory: Lab
        credentials: [Lab SSH]
        ask_limit_on_launch: true
        survey_enabled: true
        survey_spec:
          name: Install Packages
          description: Packages to install
          spec:
            - question_name: Packages (comma separated)
              variable: packages
              type: text
              default: vim,jq,tmux
              required: true
      register: jt_packages

    - name: Allow the team DBA to run the job templates
      ansible.controller.role_team_assignment:
        role_definition: JobTemplate Execute
        team: DBA
        object_id: "{{ item }}"
      loop: "{{ (jt_os.results | map(attribute='id') | list) + [jt_packages.id] }}"

    - name: Check for updates every morning at 6:00
      ansible.controller.schedule:
        name: Daily update check
        unified_job_template: OS - Check Updates
        rrule: "DTSTART;TZID=Europe/Berlin:20261205T060000 RRULE:FREQ=DAILY;INTERVAL=1"

    - name: Create a workflow which runs both OS job templates
      ansible.controller.workflow_job_template:
        name: OS - Info and Update Check
        organization: "{{ org }}"
        inventory: Lab
        destroy_current_nodes: true
        workflow_nodes:
          - identifier: info
            unified_job_template:
              name: OS - Show Info
              type: job_template
              organization:
                name: "{{ org }}"
            related:
              success_nodes:
                - identifier: updates
          - identifier: updates
            unified_job_template:
              name: OS - Check Updates
              type: job_template
              organization:
                name: "{{ org }}"
EOF
aap_play config/aap_config.yml
Sample Output (click to expand):
[aap@lin2 aap-demo]$ aap_play config/aap_config.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [Configure the AAP objects for the lab] ***********************************

TASK [Create the organization] *************************************************
changed: [localhost]

TASK [Create the team DBA] *****************************************************
changed: [localhost]

TASK [Create the user jochen] **************************************************
changed: [localhost]

TASK [Make the user jochen a member of the organization] ***********************
changed: [localhost]

TASK [Add the user jochen to the team DBA] *************************************
changed: [localhost]

TASK [Create the machine credential for the lab VMs] ***************************
changed: [localhost]

TASK [Create the inventory] ****************************************************
changed: [localhost]

TASK [Add the hosts] ***********************************************************
changed: [localhost] => (item=lin6.fritz.box)
changed: [localhost] => (item=lin7.fritz.box)
changed: [localhost] => (item=lin8.fritz.box)

TASK [Create the group dbservers] **********************************************
changed: [localhost]

TASK [Create the group witness] ************************************************
changed: [localhost]

TASK [Create the project (Git repository in Gitea)] ****************************
changed: [localhost]

TASK [Create the job templates] ************************************************
changed: [localhost] => (item={'name': 'OS - Show Info', 'playbook': 'playbooks/os_info.yml'})
changed: [localhost] => (item={'name': 'OS - Check Updates', 'playbook': 'playbooks/os_check_updates.yml'})

TASK [Create the job template with a survey] ***********************************
changed: [localhost]

TASK [Allow the team DBA to run the job templates] *****************************
changed: [localhost] => (item=18)
changed: [localhost] => (item=19)
changed: [localhost] => (item=20)

TASK [Check for updates every morning at 6:00] *********************************
changed: [localhost]

TASK [Create a workflow which runs both OS job templates] **********************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=16   changed=16   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Launching Jobs

Jobs can of course be started in the GUI, but they can also be launched as code. The playbook config/aap_launch.yml runs an ad hoc command on all hosts, starts the workflow and launches the job template with the survey for lin6 only. The survey answer is passed in extra_vars:

# run as aap on lin2
cd ~/aap-demo
cat > config/aap_launch.yml <<'EOF'
---
- name: Launch jobs in AAP
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Run an ad hoc command on all hosts
      ansible.controller.ad_hoc_command:
        inventory: Lab
        credential: Lab SSH
        module_name: command
        module_args: uptime
        wait: true
      register: adhoc

    - name: Launch the workflow and wait until it has finished
      ansible.controller.workflow_launch:
        workflow_template: OS - Info and Update Check
        wait: true
      register: workflow

    - name: Launch the job template with the survey for lin6 only
      ansible.controller.job_launch:
        job_template: OS - Install Packages
        limit: lin6.fritz.box
        extra_vars:
          packages: vim,jq
        wait: true
      register: job

    - name: Show the job IDs
      ansible.builtin.debug:
        msg: "ad hoc command: {{ adhoc.id }}, workflow: {{ workflow.id }}, job: {{ job.id }}"
EOF
aap_play config/aap_launch.yml
# put the configuration under version control as well
git add config
git commit -m "Add AAP configuration as code"
git push
Sample Output (click to expand):
[aap@lin2 aap-demo]$ # run as aap on lin2
cd ~/aap-demo
cat > config/aap_launch.yml <<'EOF'
---
- name: Launch jobs in AAP
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Run an ad hoc command on all hosts
      ansible.controller.ad_hoc_command:
        inventory: Lab
        credential: Lab SSH
        module_name: command
        module_args: uptime
        wait: true
      register: adhoc

    - name: Launch the workflow and wait until it has finished
      ansible.controller.workflow_launch:
        workflow_template: OS - Info and Update Check
        wait: true
      register: workflow

    - name: Launch the job template with the survey for lin6 only
      ansible.controller.job_launch:
        job_template: OS - Install Packages
        limit: lin6.fritz.box
        extra_vars:
          packages: vim,jq
        wait: true
      register: job

    - name: Show the job IDs
      ansible.builtin.debug:
        msg: "ad hoc command: {{ adhoc.id }}, workflow: {{ workflow.id }}, job: {{ job.id }}"
EOF
aap_play config/aap_launch.yml
# put the configuration under version control as well
git add config
git commit -m "Add AAP configuration as code"
git push
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [Launch jobs in AAP] ******************************************************

TASK [Run an ad hoc command on all hosts] **************************************
changed: [localhost]

TASK [Launch the workflow and wait until it has finished] **********************
changed: [localhost]

TASK [Launch the job template with the survey for lin6 only] *******************
changed: [localhost]

TASK [Show the job IDs] ********************************************************
ok: [localhost] => {
    "msg": "ad hoc command: 52, workflow: 53, job: 58"
}

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[main a59ef97] Add AAP configuration as code
 2 files changed, 190 insertions(+)
 create mode 100644 config/aap_config.yml
 create mode 100644 config/aap_launch.yml
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 2.23 KiB | 2.23 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To http://lin2.fritz.box:3000/gitadmin/aap-demo.git
   8b17b8b..a59ef97  main -> main
[aap@lin2 aap-demo]$

Checking the Result in the GUI

In the GUI we find the new objects under Automation Execution (Templates, Projects, Infrastructure, Schedules) and the job output under Automation Execution, Jobs. If we log in as the user jochen, we only see the job templates which the team DBA may execute.

AAP job list with the successful jobs of this post (workflow, job templates and project syncs)

Have fun

0