From 2e1ed64ffc1a4c3c6280310dc530a3934fbc9ebe Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Mon, 19 Aug 2024 19:13:34 +0200 Subject: [PATCH] chore(awx): and it turned out awx had a client utility all along --- knowledge base/awx.md | 49 +++++++++++++++++++++++++++++ snippets/awx.sh | 73 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 117 insertions(+), 5 deletions(-) diff --git a/knowledge base/awx.md b/knowledge base/awx.md index 2aed883..03f3649 100644 --- a/knowledge base/awx.md +++ b/knowledge base/awx.md @@ -594,6 +594,53 @@ deployment.apps "awx-operator-controller-manager" deleted Refer [AWX API Reference] and [How to use AWX REST API to execute jobs]. +AWX offers the `awx` client CLI tool: + +```sh +# Install the 'awx' client +pipx install 'awxkit' +pip3 install --user 'awxkit' + +# Normally `awx` would require setting the configuration every command like so: +# awx --conf.host https://awx.example.org --conf.username 'admin' --conf.password 'password' config +# awx --conf.host https://awx.example.org --conf.username 'admin' --conf.password 'password' export --schedules + +# Export settings to environment variables to avoid having to set them on the command line all the time +export TOWER_HOST='https://awx.example.org' TOWER_USERNAME='admin' TOWER_PASSWORD='password' + +# Show the client configuration +awx … config + +# List all available endpoints +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/' | jq '.' - + +# List jobs +awx jobs list +awx jobs list -f 'yaml' +awx jobs list -f 'human' --filter 'name,created,status' +awx jobs list -f 'jq' --filter '.results[] | .name + " is " + .status' + +# Show job templates +awx job_templates list +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/job_templates/' | jq '.' - + +# Show notification templates +awx … notification_templates list +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/notification_templates/' | jq '.' - + +# Show schedules +awx … schedules list +awx … schedules --schedules 'schedule-1' 'schedule-n' +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/schedules/' | jq '.' - + +# Export data +awx … export +awx … export --job_templates 'job-template-1' 'job-template-n' --schedules +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/export/' | jq '.' - +``` + +Refer [AWX Command Line Interface] for more information. + ## Further readings - [Website] @@ -601,6 +648,7 @@ Refer [AWX API Reference] and [How to use AWX REST API to execute jobs]. - [Minikube] - [Kustomize] - [Helm] +- [AWX Command Line Interface] ### Sources @@ -629,6 +677,7 @@ Refer [AWX API Reference] and [How to use AWX REST API to execute jobs]. [awx api reference]: https://ansible.readthedocs.io/projects/awx/en/latest/rest_api/ +[awx command line interface]: https://docs.ansible.com/ansible-tower/latest/html/towercli/ [awx's documentation]: https://ansible.readthedocs.io/projects/awx/en/latest/ [awx's repository]: https://github.com/ansible/awx/ [basic install]: https://ansible.readthedocs.io/projects/awx-operator/en/latest/installation/basic-install.html diff --git a/snippets/awx.sh b/snippets/awx.sh index 10c3440..c2a30dc 100644 --- a/snippets/awx.sh +++ b/snippets/awx.sh @@ -1,8 +1,71 @@ #!/usr/bin/env sh -# List all available endpoints -curl -fs 'https://awx.company.com/api/v2/' | jq '.' - +# Install the 'awx' client +pipx install 'awxkit' +pip3 install --user 'awxkit' +pip install 'git+https://github.com/ansible/awx.git@24.6.1#egg=awxkit&subdirectory=awxkit' -# list all jobs -curl -fs --user 'admin:password' 'https://awx.company.com/api/v2/job_templates/' | jq '.' - -curl -fs 'https://awx.company.com/api/v2/job_templates/' | jq '.' - +# Normally `awx` would require setting the configuration every command like so: +# awx --conf.host https://awx.example.org --conf.username 'admin' --conf.password 'password' config +# awx --conf.host https://awx.example.org --conf.username 'admin' --conf.password 'password' export --schedules + +# Export settings to environment variables to avoid having to set them on the command line all the time +export TOWER_HOST='https://awx.example.org' TOWER_USERNAME='admin' TOWER_PASSWORD='password' + +# Show the client configuration +awx config +awx --conf.host https://awx.example.org --conf.username 'admin' --conf.password 'password' config +TOWER_HOST='https://awx.example.org' TOWER_USERNAME='admin' TOWER_PASSWORD='password' awx config + +# List all available endpoints +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/' | jq '.' - + +# List jobs +awx jobs list +awx jobs list -f 'yaml' +awx jobs list -f 'human' --filter 'name,created,status' +awx jobs list -f 'jq' --filter '.results[] | .name + " is " + .status' + +# Show job templates +awx job_templates list +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/job_templates/' | jq '.' - + +# Modify job templates +awx job_templates modify '1' --extra_vars "@vars.yml" +awx job_templates modify '5' --extra_vars "@vars.json" + +# Show notification templates +awx notification_templates list +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/notification_templates/' | jq '.' - + +# Show schedules +awx schedules list +awx … schedules --schedules 'schedule-1' 'schedule-n' +curl -fs --user 'admin:password' 'https://awx.example.org/api/v2/schedules/' | jq '.' - + +# Import SSH keys +awx credentials create --credential_type 'Machine' \ + --name 'My SSH Key' --user 'alice' \ + --inputs '{"username": "alice", "ssh_key_data": "@~/.ssh/id_rsa"}' + +# Execute ad-hoc commands +awx ad_hoc_commands create --monitor --wait --job_type 'check' --inventory 'Localhost' 'ping' --module_name 'ping' + +# Export resources +awx export +awx … export --job_templates 'job-template-1' 'job-template-n' --schedules +awx … export --users 'admin' '42' + +# Import resources +awx import < 'resources.json' + +# Create and launch job templates +awx projects create --wait \ + --organization '1' --name='Example Project' \ + --scm_type 'git' --scm_url 'https://github.com/ansible/ansible-tower-samples' \ + -f 'human' \ +&& awx job_templates create \ + --name='Example Job Template' --project 'Example Project' \ + --playbook 'hello_world.yml' --inventory 'Demo Inventory' \ + -f 'human' \ +&& awx job_templates launch 'Example Job Template' --monitor -f 'human'