# Ansible\_ssh\_private\_key from variable, not from file

**URL:** <https://forum.ansible.com/t/ansible-ssh-private-key-from-variable-not-from-file/8403>\
**Category:** Get Help\
**Tags:** playbook, documentation, inventory, ansible-core\
**Created:** [September 10, 2024, 3:33pm UTC](https://forum.ansible.com/t/ansible-ssh-private-key-from-variable-not-from-file/8403 "2024-09-10T15:33:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 10, 2024, 3:33pm UTC](https://forum.ansible.com/t/ansible-ssh-private-key-from-variable-not-from-file/8403/1 "2024-09-10T15:33:33Z")

</div>

Hii

As part of our workflow, we receive VMs that are spun up by another team. The VMs contain a single SSH public key, to which we have the private key, which allows us to further deploy them.  
Part of this process is to replace the public key with other public keys.

The ansible code that we have, contains the SSH private key that can be used to sign in to the VMs (obviously in a vaulted file).  
While setting things up, I found that with both the standard [ssh connection module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html) and the [paramiko module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/paramiko_ssh_connection.html) it is not possible to use a private key that is stored as an ansible variable. They both seem to insist on a key file.

I tried that, and that works. But the ssh connection docs are a bit vague as they only mention the authentication password, and nothing about the private key password.  
I found that the docs on the paramiko module are actually correct:

> Secret used to either login the ssh server or as a passphrase for ssh keys that require it

This also means I have to have `sshpass` installed.

Is there any other way to use a private key from a variable?
