Feedback

Chat Icon

AWX in Action

Ansible Orchestration at Scale

Skip the UI: Driving AWX Entirely From the Command Line
75%

Using the CLI in Real Workflows

To demonstrate how to use the AWX CLI, we'll authenticate with the API using the admin PAT token (Personal Access Token) that we generated earlier.

Here are the commands to re-execute to authenticate with the admin PAT token:

# Export these environment variables
export TOWER_HOST=
export TOWER_USERNAME= # admin
export TOWER_PASSWORD= # Ansible123!

# Generate a token
awx login -k \
    --conf.host $TOWER_HOST \
    --conf.username $TOWER_USERNAME \
    --conf.password $TOWER_PASSWORD

# Export the token
export TOWER_OAUTH_TOKEN=

Now, let's see how to use the AWX CLI to interact with the AWX API and manage resources such as users, organizations, inventories, job templates, and more.

Output Formats: JSON, Human, and jq

Let's first understand the different output formats that the AWX CLI supports. Before starting, let's create an alias for the awx command to include the -k flag and skip SSL certificate verification.

alias awx='awx -k'

Now, let's take an example of a command that lists all users in the AWX instance:

awx user list

The output will be in JSON format by default. If you want to format it in a human-readable way, use the -f human flag:

awx user list -f human

If you want to filter the output to display only specific fields, you can use the --filter flag. The following command will display only the username field for each user and not the id:

awx user list -f human --filter username

Here is how to use multiple fields in the filter:

awx user list -f human --filter username,email

To customize using jq, you should first install it (pip3 install jq), and then use it with the -f jq flag. Here is an example that lists all users and filters only the username and email fields using jq. The result will be in JSON format:

pip install jq

awx user list -f human \
    -f jq \
    --filter '.results[] | {username: .username, email: .email}'

This is what the result could look like:

{"username": "admin", "email": "test@example.com"}
{"username": "nebula_dev1", "email": ""}
{"username": "nebula_dev2", "email": ""}
{"username": "nebula_ops1", "email": ""}
{"username": "nebula_ops2", "email": ""}
{"username": "solara_dev1", "email": ""}
{"username": "solara_dev2", "email": ""}
{"username": "solara_ops1", "email": ""}
{"username": "solara_ops2", "email": ""}

Here is another example with the jq filter:

awx user list -f human \
    -f jq \
    --filter '.results[] | .username + " has the following email: " + .email'

This is what the result could look like:

admin has the following email: test@example.com
nebula_dev1 has the following email: 
nebula_dev2 has the following email: 
nebula_ops1 has the following email: 
nebula_ops2 has the following email: 
solara_dev1 has the following email: 
solara_dev2 has the following email: 
solara_ops1 has the following email: 
solara_ops2 has the following email:

If you want to show only users having an email address, you can use the following command:

awx user list -f human \
    -f jq \
    --filter '.results[] | select(.email != "") | .username + " has the following email: " + .email'

Backup and Restore: Export, Import, Done

To import and export data using the AWX CLI and use it in different AWX instances, you can use the awx command with the import and export subcommands. Here are some examples showing how to export resources such as users, organizations, and everything from an AWX instance:

# Export users to a file
awx export --users > users.json

# Export a specific user to a file
awx export --users admin > admin.json

# Export organizations to a file
awx export --organizations > organizations.json

# Export everything to a file
awx export --all > all.json

# or
awx export > all.json

AWX in Action

Ansible Orchestration at Scale

Enroll now to unlock all content and receive all future updates for free.