One solution could be to set the awx_token
as an environment variable. If you add it as an environment variable in the custom credential, that will be exposed at Ansible’s control_node level, meaning you could set CONTROLLER_OAUTH_TOKEN
and use your token with the awx.awx
collection. However, you’ll need to set the environment variable at the play/task level the same way you would for regular vars to help hide it in shell tasks.
E.g. Setting environment var in task from custom credential’s extra_vars/env_vars.
# Injector configuration for your custom credential in awx
extra_vars:
awx_token: '{{ awx_token }}'
env:
AWX_TOKEN: '{{ awx_token }}'
CONTROLLER_OAUTH_TOKEN: '{{ awx_token }}'
# Shell task in some play
- name: My custom shell with env var
ansible.builtin.shell:
cmd: |
echo "${AWX_TOKEN}" > /dev/null # this is fine
echo "{{ awx_token }}" > /dev/null # this is bad
echo "${AWX_TOKEN}" # this is worse
echo "{{ awx_token }}" # this is the worst
environment:
AWX_TOKEN: "{{ awx_token | default(lookup('env', 'AWX_TOKEN') | default('')) }}"
In the above task, redirecting the output to /dev/null
prevents exposing the token to stdout. ${AWX_TOKEN}
is the env variable, and is not exposed on the cli or in the cmd
key of the module’s output. This makes the first echo safe since you will not see the token in the module’s output (i.e. {'results': {'cmd': 'echo "${AWX_TOKEN}"', 'stdout': ''}}
). On the other hand, you will see the {{ awx_token }}
in plaintext in the cmd
key, and definitely see either var in the stdout if it is not redirected or otherwise used in a parameter.
With the ansible.builtin.uri
, I’m not sure that there’s any way to hide the token, but you might be able to use {{ lookup('env', 'AWX_TOKEN') }}
in the argument to set the header. I’m not sure if the module output would show the literal jinja string or the resulting lookup value.
Like so:
- ansible.builtin.uri:
uri: "https://awx.domain.local/api/v2/inventories"
return_content: true
headers:
Authorization: "Bearer {{ lookup('env', 'AWX_TOKEN') }}"
In this case, the only time I could see the token in testing is if verbosity was increased. It is not in the module output. Haven’t tested it in AWX, so I’m not sure what the module parameters might look like in the job details/output (with or without verbosity increased).