Skip to content

Instantly share code, notes, and snippets.

@berttejeda
Last active August 21, 2024 19:20
Show Gist options
  • Save berttejeda/8b0cfcd115662d89e180c39a59f87a87 to your computer and use it in GitHub Desktop.
Save berttejeda/8b0cfcd115662d89e180c39a59f87a87 to your computer and use it in GitHub Desktop.
Ansible ARA Quick Start

Ansible ARA Quick Start

Start an instance of ARA via Docker

Below are several options for firing up an instance of Ansible ARA

  • Run the docker container with the sqlite db backend
docker run \
-d --rm \
--name ara \
-it \
-v $PWD:/opt/ara:z \
-p 8000:8000 \
-e ARA_ALLOWED_HOSTS="['*']" \
docker.io/recordsansible/ara-api:latest
  • Run the docker container with the postgres db backend
  docker run \
  --rm \
  --name ara \
  -it \
  -v $PWD:/opt/ara:z \
  -p 8000:8000 \
  -e ARA_DATABASE_ENGINE="django.db.backends.postgresql" \
  -e ARA_DATABASE_CONN_MAX_AGE=300 \
  -e ARA_DATABASE_USER=svcpostgres \
  -e ARA_DATABASE_NAME=ara \
  -e ARA_DATABASE_PASSWORD=D5e82071 \
  -e ARA_DATABASE_HOST=psdb.example.net \
  -e ARA_DATABASE_PORT=16009 \
  -e ARA_ALLOWED_HOSTS="['*']" \
  docker.io/recordsansible/ara-api:latest
  • Run the docker-compose project
  ---
  version: "3.1"
  services:
    ansible-ara:
      image: "recordsansible/ara-api:latest"
      container_name: 'ansible-ara'
      restart: always
      ports:
        - "8000:8000"
      environment:
        ARA_ALLOWED_HOSTS: "['*']"

Install prerequisites

To be able to utilize ARA, you'll need to install some prerequisites:

pip install ara 'ara[server]'
ansible-galaxy collection install recordsansible.ara

Create your ansible ARA-enabled playbook

playbook.yaml

- name: ARA-enabled Playbook
  hosts: localhost
  connection: local
  gather_facts: true
  become: true
  max_fail_percentage: 0
  tasks:

    - name: Prep for Ansible ARA playbook report
      block:

        - name: Get the currently running playbook
          ara_playbook:
          register: query
          run_once: true

        - name: Retrieve playbook id
          set_fact:
            playbook_id: "{{ query.playbook.id | string }}"
      
        # With the playbook id we can create a link to the playbook report
        - name: Retrieve base url from ara
          set_fact:
            api_base_url: "{{ lookup('ara_api', '/api/') }}"
          run_once: true

      rescue:

        - name: Display warning
          debug:
            msg: |-
              Warning: Could not record playbook statistics via ARA!

    - name: Say Hello
      debug:
        msg: |-
          Hello from Ansible!

    - name: ARA Post Execution
      block:
   
      - name: Record Execution Context
        ara_record:
          key: execution_context
          value: 
            playbook_id: '{{ playbook_id }}'
            user: "{{ lookup('env', 'USER') }}"
          type: dict
        run_once: true
        tags:
          - app
          - machine          

      - name: Display link to playbook ARA report
        debug:
          msg: "{{ ui_base_url }}/playbooks/{{ playbook_id }}.html"
        vars:
          ui_base_url: "{{ api_base_url.api[0] | regex_replace('/api/v1/', '') }}"
        run_once: true
        tags:
          - app
          - machine          
   
      when: playbook_id is defined 

Run your ansible playbook with ARA enabled

Given: Your ARA server is accessible at https://localhost:8000

echo -n "Setting up Ansible ARA ... "
eval $(python -m ara.setup.env)
export ARA_API_CLIENT="http"
export ARA_API_SERVER="http://localhost:8000"
export ARA_API_TIMEOUT=15
export ARA_CALLBACK_THREADS=4
export ARA_IGNORED_FACTS=ansible_env,ansible_all_ipv4_addresses
export ARA_IGNORED_FILES=.ansible/tmp,vault.yaml,vault.yml
export ARA_IGNORED_ARGUMENTS=extra_vars,vault_password_files
export ARA_LOCALHOST_AS_HOSTNAME=true
export ARA_LOCALHOST_AS_HOSTNAME_FORMAT=fqdn
export ANSIBLE_CALLBACK_PLUGINS=$(python -m ara.setup.callback_plugins)
echo 'Done!'

ansible-playbook -i localhost, playbook.yaml

References

https://ara.recordsansible.org/ https://ara.readthedocs.io/en/latest/ansible-plugins-and-use-cases.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment