# Machine credential injector

**URL:** <https://forum.ansible.com/t/machine-credential-injector/30399>\
**Category:** AWX Project\
**Tags:** awx\
**Created:** [May 22, 2019, 1:17pm UTC](https://forum.ansible.com/t/machine-credential-injector/30399 "2019-05-22T13:17:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://forum.ansible.com/u/ahmed)\
**Post date:** [May 22, 2019, 1:17pm UTC](https://forum.ansible.com/t/machine-credential-injector/30399/1 "2019-05-22T13:17:30Z")

</div>

Hi,

I create a AWX credential with machine type.

How can I access username and password fields of this credential in a playbook ?

Thank you.

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [May 30, 2019, 7:15am UTC](https://forum.ansible.com/t/machine-credential-injector/30399/2 "2019-05-30T07:15:28Z")

</div>

Hello Ahmed,

The following playbook is an example that how to use machine credentials in your playbook:

`

- hosts: all  
vars:  
machine:  
username: ‘{{ machine\_credential\_user }}’  
password: ‘{{ machine\_credential\_password }}’

tasks:

- debug:

var: machine

`

Thanks  
Soniya

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://forum.ansible.com/u/ahmed)\
**Post date:** [June 18, 2019, 8:54am UTC](https://forum.ansible.com/t/machine-credential-injector/30399/3 "2019-06-18T08:54:11Z")

</div>

Hello Soniya,

I have tried your solution but it does not work. I get “VARIABLE IS NOT DEFINED!” error.  
is there any documentation about cred injection in AWX ?

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [July 15, 2019, 11:32am UTC](https://forum.ansible.com/t/machine-credential-injector/30399/4 "2019-07-15T11:32:13Z")

</div>

Hello Ahmed,

You are getting this error because ansible fact caching is disabled. Variables ‘{{ machine\_credential\_user }}’ and ‘{{ machine\_credential\_password }}’ are present in ansible fact cache. To use these credentials you have to enable fact caching by setting gather\_facts: true.

> > is there any documentation about cred injection in AWX ?  
> > Yes we can achieve this task by creating a custom credential type. Credential types consist of two key concepts – “inputs” and “injectors“. Where:  
> > **Inputs:** define the value types that are used for this credential – such as a username, a password, a token, or any other identifier that’s part of the credential.  
> > **Injectors:** describe how these credentials are exposed for Ansible to use – this can be Ansible extra variables, environment variables, or templated file content.

Here are the steps:

Creating the Custom Credential type in Ansible AWX:

1. Login Ansible AWX with administrator privileges.
2. Navigate to custom credential type and click on “+” to create new.
3. Update the input & inject fields with desired values. In this example, I am creating the custom credential type for Machine\_cred authentication.

**Input:**

`  
fields:

- type: string  
id: machine\_credential\_user  
label: Machine username

- type: string  
id: machine\_credential\_password  
label: “Machine password”  
secret: True

required:

- machine\_credential\_user
- machine\_credential\_password

`

**Injector:**

`  
extra\_vars:  
username: ‘{{ machine\_credential\_user }}’  
password: ‘{{ machine\_credential\_password }}’

`

And then you will see the newly created credential type.

1. Then Navigate to Credential. Create a new credential for Machine authentication. Click on the Credential type’s search box.
2. Select the credential type which we have created for Machine\_cred.
3. Enter machine username & password to authenticate to the portal.
4. In your playbook, you need to call the injector’s extra-vars for successful authentication. Refer step : 3 to know the extra\_vars.

**Example:**

`

- name: Create a your job by using Ansible playbook

\<Your\_JobName\>:  
name: MachineJobdemo  
user: “{{ username }}”  
password: “{{ password }}”

`

1. Add Machine credential in playbook template if your job requires machine authentication.

Hope this solution will work for you. Please revert in case of any query

Thanks  
Soniya
