# Want to use private\_key\_file as varable in template file.

**URL:** <https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [May 8, 2015, 2:51am UTC](https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188 "2015-05-08T02:51:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![t\_goto](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t\_goto](https://forum.ansible.com/u/t_goto)\
**Post date:** [May 8, 2015, 2:51am UTC](https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188/1 "2015-05-08T02:51:37Z")

</div>

Hello.  
I want to use `private_key_file` in ansible.cfg as a variable in template file.

Say, I have ansible.cfg like this..  
`[defaults] hostfile = hosts remote_user = ubuntu private_key_file = /home/test/.ssh/id_rsa`

And want to use it in Jinja2 format like this..  
`  
options[:user] ||= Etc.getlogin  
options[:keys] = “{{ private\_key\_file }}”

# options[:keys] = “/home/test/.ssh/id\_rsa”

`

with playbook calls template modules..  
`

- name: template copy  
template: src=spec.j2 dest=/serverspec/.spec  
`

If you use `ansible_ssh_private_key_file` in inventory host file, you can successfuly template it.  
But I want to use good old ansible.cfg.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 8, 2015, 2:46pm UTC](https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188/2 "2015-05-08T14:46:25Z")

</div>

something like:  
lookup('pipe', 'grep private\_key\_file /etc/ansible/ansible.cfg|cut -f2 -d"="')

---

<div class="post-metadata">

**Author:** ![t\_goto](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t\_goto](https://forum.ansible.com/u/t_goto)\
**Post date:** [May 9, 2015, 5:36am UTC](https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188/3 "2015-05-09T05:36:13Z")

</div>

Sounds like a plan! 🙂  
I willl try this method, thanks!

2015年5月8日金曜日 23時46分28秒 UTC+9 Brian Coca:

---

<div class="post-metadata">

**Author:** ![Marcus\_Franke](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@Marcus\_Franke](https://forum.ansible.com/u/Marcus_Franke)\
**Post date:** [May 9, 2015, 5:43am UTC](https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188/4 "2015-05-09T05:43:44Z")

</div>

Hi,

one thing I don’t understand, why do you need to roll out the private key at all? Using the ssh-agent and agent forwarding you can use your locally stored private key even for those hosts in the second row behind some kind of jump host.

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![t\_goto](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t\_goto](https://forum.ansible.com/u/t_goto)\
**Post date:** [May 11, 2015, 12:39am UTC](https://forum.ansible.com/t/want-to-use-private-key-file-as-varable-in-template-file/18188/5 "2015-05-11T00:39:40Z")

</div>

Hello, Marcus.

What I’m trying to do is to use `serverspec` to test the ansible-deployed hosts.  
As `serverspec` connects to test machines via ssh, I have to let it know the location of ssh private key.  
I didn’t want to edit the root .ssh/config for such small part, I was looking for a way to specify private key.

Thank you.
