# AWX / Ansible Automation Controller tutorial

ipsonic-probe runs on the **managed Linux host**, not inside the AWX Execution Environment (EE). AWX uses SSH and Ansible builtin modules to deploy and execute it. Runtime dependencies remain native Linux packages; **no pip, database, agent or AWX-specific Python library is required on the target**.

## Architecture and prerequisites

- AWX / Automation Controller with a working Execution Environment containing `ansible-core` and SSH support.
- A source-control Project containing this repository (or an internally mirrored checkout), plus an AWX Inventory with a group named `ipsonic_probe`.
- An AWX **Machine credential** with SSH access and optional privilege escalation. Never put SSH passwords or private keys into inventory YAML.
- Targets: Debian, Ubuntu, Raspberry Pi OS, AlmaLinux or Rocky Linux, with OS packages from approved repositories or internal mirrors.
- AWX Project sync needs a reachable Git source; use an internal mirror for air-gapped networks. Source releases can be imported into an internal Git project instead of publishing upstream.

## 1. Create AWX inventory, credentials and project

1. Create an Inventory; add a host to group `ipsonic_probe` (see `ansible/awx-inventory.ini.example`).
2. Create a **Machine** credential for SSH authentication. Enable privilege escalation if the deployment template needs root.
3. Create a Project with the Git repository containing `ansible/prepare-ipsonic-probe.yml`, `ansible/awx-deploy-checks.yml` and `ansible/awx-run-checks.yml`. Select an approved EE.
4. Create a **Job Template** named `ipsonic-probe | prepare`, playbook `ansible/prepare-ipsonic-probe.yml`, inventory and Machine credential. Run it once per host or after package changes.
5. Create `ipsonic-probe | deploy` with playbook `ansible/awx-deploy-checks.yml`, same inventory and Machine credential. Supply `ipsonic_check_source: ansible/awx-checks` as a variable if using the included example. The deployment playbook copies only files in the selected source directory. Review this path before enabling schedules.
6. Create `ipsonic-probe | execute` with playbook `ansible/awx-run-checks.yml` and the same inventory and Machine credential. Add the API credential described below if required.
7. Optionally build a **Workflow Job Template**: prepare → deploy → execute. Use AWX schedules only if periodic orchestration is desired; ipsonic-probe itself remains daemonless.

The `execute` Job Template needs privilege escalation because the deployed checks directory is root-owned and mode `0750`. The Machine credential must permit become.

The `prepare` playbook does not install AWX on the target. The `deploy` playbook deploys YAML checks to `/etc/ipsonic-probe/checks` with root ownership and restrictive permissions. The `execute` playbook validates the directory before running checks and reports a numeric exit code to AWX. The AWX job fails for non-OK results.

## 2. Credential option A: AWX custom credential type (recommended)

In AWX **Credential Types**, create a custom type `ipsonic API Token` using:

**Input configuration** (`ansible/awx-custom-credential-input.json`):

```json
{"fields": [{"id": "api_token", "type": "string", "label": "API token", "secret": true}], "required": ["api_token"]}
```

**Injector configuration** (`ansible/awx-custom-credential-injector.json`):

```json
{"env": {"IPSONIC_API_TOKEN": "{{ api_token }}"}}
```

Create a credential of this type, enter the secret, and attach it to the **execute** Job Template. AWX injects `IPSONIC_API_TOKEN` into the Execution Environment. The playbook reads it with `lookup('ansible.builtin.env', 'IPSONIC_API_TOKEN')` and passes it to the remote probe process using Ansible `environment`. The YAML check uses `auth.token_env: IPSONIC_API_TOKEN`, so no token is committed to Git or written into the YAML check.

**Security:** AWX environment injection protects secrets from normal job output but not necessarily from privileged users on the controller/target or process inspection. Restrict credential use, host access and AWX job permissions. The `no_log: true` execution task intentionally suppresses detailed check output. Never enable verbose Ansible debug for secret-bearing tasks.

## 3. Credential option B: Ansible Vault

Create an encrypted vars file on the controller:

```sh
mkdir -p ansible/group_vars/ipsonic_probe
ansible-vault create ansible/group_vars/ipsonic_probe/vault.yml
```

Enter this content in the editor (use a real token):

```yaml
vault_ipsonic_api_token: REPLACE_WITH_REAL_TOKEN
```

Keep only the **encrypted** Vault file in version control. In AWX create a **Vault credential** containing the Vault password (or a supported vault-ID credential), and attach it to the execute Job Template. Ansible loads `group_vars` when its directory is discoverable relative to the inventory/playbook; confirm your AWX Project layout. Alternatively set `vars_files` in the execute playbook to an explicit encrypted file path inside the Project. Never enter secrets as plaintext AWX survey answers or extra vars.

The playbook prefers the AWX-injected `IPSONIC_API_TOKEN` when present, otherwise uses `vault_ipsonic_api_token`. Both paths use `no_log: true`. If neither is set, authenticated checks fail rather than silently obtaining credentials elsewhere.

## 4. Credential option C: environment variable outside AWX

For a local controller run, provide the token to Ansible in the environment without committing it:

```sh
read -r -s -p 'API token: ' IPSONIC_API_TOKEN; echo
export IPSONIC_API_TOKEN
ansible-playbook -i ansible/awx-inventory.ini.example ansible/awx-run-checks.yml
unset IPSONIC_API_TOKEN
```

Do not pass secrets directly on the command line, where they may appear in process listings or shell history. A plain environment variable is **not a secret store**; use AWX credentials or Vault in production.

## 5. Example YAML and expected results

Example: `ansible/awx-checks/api-health.yaml`. It is deliberately nonfunctional until `api.example.com` is replaced with your actual HTTPS API and a valid token is supplied. The check can be validated offline:

```sh
python3 ipsonic-probe.py --validate --strict ansible/awx-checks/api-health.yaml
```

A successful execute job reports only `exit code 0`; nonzero exit codes mark the job failed. The task output is suppressed so credentials and response details cannot leak into AWX logs. Consult protected target-side diagnostics if more detail is needed. Avoid printing raw HTTP responses or authentication headers.

## 6. Local validation and troubleshooting

```sh
ansible-playbook --syntax-check -i ansible/awx-inventory.ini.example ansible/awx-run-checks.yml
ansible-playbook --syntax-check -i ansible/awx-inventory.ini.example ansible/awx-deploy-checks.yml
python3 ipsonic-probe.py --validate --strict ansible/awx-checks/api-health.yaml
```

- **Missing imports:** run the prepare template; confirm distro-native Requests/PyYAML packages and CA certificates.
- **SSH / become errors:** check Machine credential, privilege escalation and inventory host variables.
- **Vault undefined:** confirm encrypted vars path, Vault credential and inventory group names.
- **API returns 401/403:** confirm token injection, environment variable name and HTTPS URL.
- **AWX job fails with exit 1/2/3:** this reflects monitoring WARNING/CRITICAL/UNKNOWN; no detailed output is intentionally published.
- **Offline hosts:** mirror both OS packages and the AWX Project source internally; no internet access is required for the probe itself.

## Security and operational limits

AWX is an orchestration option, not a monitoring database. This integration does not create persistent history, automatic Nagios service discovery, or real-time alerting. A single secret is injected into the job environment and can be visible to processes with sufficient privileges. For multiple applications use separate AWX templates/credentials or extend the playbook with explicit per-check mappings rather than one shared token. Never commit real credentials or unencrypted Vault files. Review job event retention and RBAC policies.

See [Ansible Installation](ansible-installation.html), [User Guide](user-guide.html), [Feature Examples](feature-examples.html), and [Operator Workflow](operator-workflow.html).
