Skip to content

Commit fa126fa

Browse files
authored
feat: run the playbooks on an RTE container (#27)
AnsibleForms 7 runs no playbook in the app, so every playbook form failed with "No runner to run this playbook". An rte service now runs ghcr.io/ansibleforms/ansibleforms-rte:7 on the same data folder, with the ansible settings, roles, collections and PYTHONPATH moved over from the app. data/seed.yaml registers it as the default runner with RTE_TOKEN from .env. It starts once the app is healthy: an RTE that finds no schema stops working without exiting. CI runs the demo playbook on it.
1 parent a705b03 commit fa126fa

6 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎.env‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ ADMIN_PASSWORD=AnsibleForms!123
164164
# REST_ALLOWED_HOSTS=
165165
# REST_DENIED_HOSTS=
166166

167+
# =============================================================================
168+
# RTE (RUNTIME ENVIRONMENT)
169+
# =============================================================================
170+
171+
# Token the app sends with every call to the RTE, at least 16 characters. Both containers
172+
# read it from here, and data/seed.yaml hands it to the app's runner on every start.
173+
RTE_TOKEN=AnsibleForms!RTE!Token!123
174+
167175
# =============================================================================
168176
# CONFIG AND FORMS
169177
# =============================================================================

‎.github/workflows/ci.yml‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: CI
22

33
# Starts the stack exactly as a user does (docker compose up with the committed .env and
4-
# data/) and logs in through the API. A green run means the compose file is valid, the
5-
# pinned images pull and start together, the app reaches its database without waiting for
6-
# it and creates its schema, and the sample data does not stop it from booting. That is
4+
# data/), logs in through the API and runs the demo playbook on the RTE. A green run means
5+
# the compose file is valid, the pinned images pull and start together, the app reaches
6+
# its database without waiting for it and creates its schema, the sample data does not stop
7+
# it from booting, and the seed hands playbooks to the RTE. That is
78
# what a Dependabot bump of the MySQL image has to prove before it is merged.
89

910
on:
@@ -41,6 +42,39 @@ jobs:
4142
echo "::error::The app did not accept the admin login within 5 minutes."
4243
exit 1
4344
45+
# AnsibleForms 7 runs no playbook itself: the rte container does, registered as the
46+
# default runner by data/seed.yaml. Launch the demo playbook form and wait for its output.
47+
- name: A playbook runs on the RTE
48+
run: |
49+
set -euo pipefail
50+
URL=https://localhost
51+
token="$(curl -fsSk -X POST -u 'admin:AnsibleForms!123' "$URL/api/v2/auth/login" | jq -r .token)"
52+
auth=(-H "Authorization: Bearer $token" -H "Content-Type: application/json")
53+
runner="$(curl -fsSk "${auth[@]}" "$URL/api/v2/runner" | jq -c '.records[] | select(.type == "rte" and .is_default == 1)')"
54+
[ -n "$runner" ] || { echo "::error::data/seed.yaml registered no default rte runner."; exit 1; }
55+
echo "Runner: $runner"
56+
# the RTE starts once the app is healthy, so give it a moment to listen
57+
check=""
58+
for i in $(seq 1 30); do
59+
check="$(curl -fsSk "${auth[@]}" -X POST "$URL/api/v2/runner/$(jq -r .id <<<"$runner")/check" || true)"
60+
[ -n "$check" ] && break
61+
sleep 5
62+
done
63+
[ -n "$check" ] || { echo "::error::The app cannot reach the RTE."; exit 1; }
64+
echo "Test connection: $check"
65+
id="$(curl -fsSk "${auth[@]}" -X POST "$URL/api/v2/job" -d '{"formName":"Demo Form Ansible No input","extravars":{}}' | jq -r .id)"
66+
for i in $(seq 1 60); do
67+
job="$(curl -fsSk "${auth[@]}" "$URL/api/v2/job/$id")"
68+
status="$(jq -r .status <<<"$job")"
69+
case "$status" in running|approve) sleep 3 ;; *) break ;; esac
70+
done
71+
output="$(jq -r .output <<<"$job")"
72+
echo "Job $id ended $status"
73+
[ "$status" = success ] || { echo "::error::The playbook job ended $status."; echo "$output"; exit 1; }
74+
grep -q "Running on RTE" <<<"$output" || { echo "::error::The job did not run on the RTE."; exit 1; }
75+
grep -q "Hi there and welcome to ansible" <<<"$output" || { echo "::error::The playbook output is missing."; exit 1; }
76+
echo "The playbook ran on the RTE."
77+
4478
- name: The app never had to wait for its database
4579
run: |
4680
# the app only starts once MySQL is healthy, so it connects on the first try

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ The setup is a compose file, its settings and the data the containers start with
1919

2020
| Path | Holds |
2121
|---|---|
22-
| `docker-compose.yml` | the AnsibleForms and MySQL containers and their volumes |
22+
| `docker-compose.yml` | the AnsibleForms, RTE and MySQL containers and their volumes |
2323
| `.env` | the settings, with sample values that work out of the box |
24-
| `data/` | what AnsibleForms and MySQL start with: config, sample forms and playbooks, custom functions, ansible and git settings |
24+
| `data/` | what AnsibleForms and MySQL start with: config, sample forms and playbooks, custom functions, ansible and git settings, and the config seed that registers the RTE |
2525

2626
What the containers write at runtime (the database, logs, backups, keys) also lands in
2727
`data/`, and `.gitignore` keeps it out of git.

‎README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ Everything under `data/` is mounted into the containers and survives a restart o
3737
- A demo `config.yaml` with categories, roles and constants, and sample forms
3838
- Sample maintenance playbooks and a `dummy.yaml` playbook
3939
- Sample custom JavaScript functions and jq definitions to extend AnsibleForms
40-
- Ansible, Python 3 and a set of Galaxy collections inside the image
40+
- An RTE container that runs the playbooks, with ansible, Python 3 and a set of Galaxy collections
41+
- A config seed, `data/seed.yaml`, that registers the RTE as the default runner
42+
43+
## Running playbooks
44+
45+
AnsibleForms 7 runs no playbook in the app: the `rte` container runs them, from
46+
`ghcr.io/ansibleforms/ansibleforms-rte:7`. The app reaches it on `http://rte:8000` with `RTE_TOKEN` from `.env`.
47+
48+
`data/seed.yaml` registers it as the default runner on every start, so it is read-only under Connections > Runners.
49+
Ansible settings, roles and collections under `data/ansible/` are mounted into the RTE, not the app. When your
50+
playbooks need more, build an image from the app's `Dockerfile.rte` and point the `rte` service at it.
4151

4252
## Kubernetes
4353

‎data/seed.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Config seed, applied by the app at every start (CONFIG_SEED_PATH in docker-compose.yml).
2+
# It registers the rte container as the default runner, so playbook forms run there. Objects
3+
# declared here are read-only in the interface; see https://ansibleforms.com/seed.
4+
version: 1
5+
runners:
6+
items:
7+
- name: rte
8+
type: rte
9+
description: The rte container of docker-compose.yml
10+
uri: http://rte:8000
11+
token: ${RTE_TOKEN}
12+
is_default: true

‎docker-compose.yml‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ services:
4747
- DB_PASSWORD=$MYSQLDB_PASSWORD
4848
- DB_PORT=$MYSQLDB_DOCKER_PORT
4949
- PORT=$WEBAPP_DOCKER_PORT
50-
# adding python path to add our custom persistent lib location
51-
- PYTHONPATH=/usr/lib/python311.zip:/usr/lib/python3.11:/usr/lib/python3.11/lib-dynload:/usr/lib/python3.11/site-packages:/app/dist/persistent/python/lib
50+
# The config seed registers the RTE below as the default runner (data/seed.yaml)
51+
- CONFIG_SEED_PATH=/app/dist/persistent/seed.yaml
5252
# allow interactive shell
5353
stdin_open: true
5454
# allow terminal
@@ -58,12 +58,44 @@ services:
5858
- ./data:/app/dist/persistent # persistent app data
5959
- ./data/ssh:$HOME_DIR/.ssh # persistent ssh keys
6060
- ./data/git/.gitconfig:$HOME_DIR/.gitconfig # custom git config
61-
- ./data/ansible/ansible.cfg:/etc/ansible/ansible.cfg # custom ansible.cfg
62-
- ./data/ansible/roles:/etc/ansible/roles # custom ansible roles
63-
- ./data/ansible/collections:/etc/ansible/collections # custom ansible collections
6461
# Custom functions: the app reads these from its source folder, not from the persistent
6562
# folder above, so they are mounted over the copies in the image.
6663
- ./data/functions/custom.js:/app/dist/src/functions/custom.js # custom JavaScript functions (fnc.*)
6764
- ./data/functions/jq.custom.definitions:/app/dist/src/functions/jq.custom.definitions # custom jq definitions
6865
# - ./data/assets/images:/app/dist/views/img # 6 custom images (login_background_%theme%.jpg and logo_%theme%.svg | 2 for each theme = light, dark and color)
69-
66+
# AnsibleForms RTE (runtime environment): runs the playbooks. AnsibleForms 7 runs none in
67+
# the app itself, so this is where ansible, the Python libraries and the collections live.
68+
# The app reaches it on http://rte:8000 with RTE_TOKEN; it is not published on the host.
69+
rte:
70+
# Only start once the app is healthy: it creates the schema on the first start, and an
71+
# RTE that finds no schema stops working without exiting, so a restart would not help
72+
depends_on:
73+
mysqldb:
74+
condition: service_healthy
75+
app:
76+
condition: service_healthy
77+
image: ghcr.io/ansibleforms/ansibleforms-rte:7
78+
container_name: af_rte
79+
restart: unless-stopped
80+
# Load extra environment variables from file (the database, ENCRYPTION_SECRET, RTE_TOKEN)
81+
env_file:
82+
- ./.env
83+
# Set environment variables
84+
environment:
85+
- DB_HOST=mysqldb
86+
- DB_USER=$MYSQLDB_USER
87+
- DB_PASSWORD=$MYSQLDB_PASSWORD
88+
- DB_PORT=$MYSQLDB_DOCKER_PORT
89+
- PORT=8000
90+
# Only the app calls it, inside the compose network
91+
- HTTPS=0
92+
# The libraries the ansibleforms_pip_install playbook installs into the persistent folder
93+
- PYTHONPATH=/app/dist/persistent/python/lib
94+
volumes:
95+
# The same folders as the app: playbooks, repositories, uploads and the SSH key
96+
- ./data:/app/dist/persistent # persistent app data
97+
- ./data/ssh:$HOME_DIR/.ssh # persistent ssh keys
98+
- ./data/git/.gitconfig:$HOME_DIR/.gitconfig # custom git config
99+
- ./data/ansible/ansible.cfg:/etc/ansible/ansible.cfg # custom ansible.cfg
100+
- ./data/ansible/roles:/etc/ansible/roles # custom ansible roles
101+
- ./data/ansible/collections:/etc/ansible/collections # custom ansible collections

0 commit comments

Comments
 (0)