# How to use Machine Credentials to connect to network devices ()

**URL:** <https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210>\
**Category:** AWX Project\
**Tags:** awx\
**Created:** [July 31, 2018, 12:46pm UTC](https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210 "2018-07-31T12:46:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_M](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_M](https://forum.ansible.com/u/Daniel_M)\
**Post date:** [July 31, 2018, 12:46pm UTC](https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210/1 "2018-07-31T12:46:43Z")

</div>

Hi guys,

I’m trying to connect to some Juniper network devices from AWX.

I’m struggling to understand how to use the Credentials (See image below for the username and ssh key) I’ve set up in AWX to connect to the network devices.

How could I pass the ssh key and username to my playbook? Is it something like this possible after saving the credentials in AWX?

…  
…  
tasks:

- name: Retrieving full config from devices  
juniper\_junos\_config:  
host: ‘{{ inventory\_hostname }}’  
**user: {{**  **AWX.Credentials.test\_key }}**  
**ssh\_private\_key\_file: {{ AWX.Credentials.test\_key }}**  
**…**  
**…**

At this moment I’m pointing the playbook to look for the key on **'~/.ssh/test\_key** which explains why I get authentication errors (The key doesn’t exists on the container). What I’d like to do is to use the test\_key and the username that I’ve defined in the AWX-Credentials area (See image below).

My SSH key is protected with a passphrase so I’m trying as well to pass that passphrase when I select the option “Private key passphrase - Prompt on launch” (I assume this should work and the -k option when running ansible-playbook main.yaml -k) (See image below).

**Current Playbook**

`

> **(attachments)**
>
> ![Screen Shot 2018-07-31 at 13.39.11.png](https://us1.discourse-cdn.com/ansible/original/2X/c/cc49524c5097c291482f4cc1e1fe88e2eea2a4dd.png)

---

<div class="post-metadata">

**Author:** ![Christopher\_Meyers](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Christopher\_Meyers](https://forum.ansible.com/u/Christopher_Meyers)\
**Post date:** [August 1, 2018, 2:41pm UTC](https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210/2 "2018-08-01T14:41:59Z")

</div>

Daniel,

Credentials are injected in the running Ansible environment in 3 ways:

1. env variables
2. ansible extra variables
3. files (usually with an env variable pointing at the file path)

For your specific case, looks like you want:

---

<div class="post-metadata">

**Author:** ![Daniel\_M](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_M](https://forum.ansible.com/u/Daniel_M)\
**Post date:** [August 3, 2018, 6:37am UTC](https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210/3 "2018-08-03T06:37:49Z")

</div>

Fantastic!!! This is what I was looking for Chris as I wasn’t aware of which env variables to reference in my playbook.

Appreciated. I’ll give that a try later today.

---

<div class="post-metadata">

**Author:** ![Dave\_Kruger](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Dave\_Kruger](https://forum.ansible.com/u/Dave_Kruger)\
**Post date:** [November 22, 2019, 11:39am UTC](https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210/4 "2019-11-22T11:39:38Z")

</div>

Hi Daniel

Did you ever manage to figure out how to use the ssh key credentials in AWX?

I’m trying with the junos\_config module, and according to the [docs](https://docs.ansible.com/ansible/latest/modules/junos_config_module.html) it also makes use of the ANSIBLE\_NET\_SSH\_KEYFILE env var .

Like you, I have my private key loaded in the inventory as shown in your screenshot.

But that variable seems to be empty, for this playbook:

`

---

<div class="post-metadata">

**Author:** ![Dave\_Kruger](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Dave\_Kruger](https://forum.ansible.com/u/Dave_Kruger)\
**Post date:** [November 22, 2019, 12:39pm UTC](https://forum.ansible.com/t/how-to-use-machine-credentials-to-connect-to-network-devices/28210/5 "2019-11-22T12:39:36Z")

</div>

Wow, I think my issue was an extra empty line at the end of my rsa private key.

Even though the ANSIBLE\_NET\_SSH\_KEYFILE env var still prints nothing in the debug, I can now successfully authenticate with private key
