Want to use private_key_file as varable in template file.

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?

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

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

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

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

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.