Better way to reference SSH Identity Key?

47 views
Skip to first unread message

Jason Wood

unread,
Jan 26, 2022, 2:03:18 PM1/26/22
to Ansible Project
Right now I have in ansible.cfg:

[ssh_connection]
ssh_args = -F ssh_config
scp_if_ssh = True

And in ssh_config:

Host sub.domain.com
    Hostname sub.domain.com
    User ubuntu
    IdentityFile ~/mykeyfile.pem


Right now I'm manually editing this file to change the 2 hostnames when I want to work with a different host!
The IdentityFile is the same for all hosts.

Can you suggest a better way? I've had trouble finding docs on this.

Brian Coca

unread,
Jan 26, 2022, 2:07:42 PM1/26/22
to Ansible Project
several ways:

In ssh_config you can:
Host *

^ will match all hosts, you can be more/less restrictive than that.

Within ansible there are many ways to supply the key :

[defaults]
private_key_file = ~/mykeyfile.pem

for a full list see
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameters



--
----------
Brian Coca

Evan Hisey

unread,
Jan 26, 2022, 2:30:24 PM1/26/22
to ansible...@googlegroups.com
Funny this came up, as I just finished a playbook that bounces keys several times. There are 2 variables you can use in the playbook itself and can be changed using set_fact:ansible_private_key_file, and ansible_ssh_private_key_file. Used with the ansible_user or ansible_ssh_user variables it can be quite handy cycling users or updating authorized keys mid play.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7fUtaV_O20o6aBvTqNYxHJA1o%2BBQbO_Wq%2BiJcrjwwT5Tg%40mail.gmail.com.

Jason Wood

unread,
Jan 28, 2022, 6:59:30 PM1/28/22
to Ansible Project
Thank you both.

I'm going with the

[defaults] 
private_key_file = ~/mykeyfile.pem 

method, but it's good to know that this can also be done inside the playbook, as I might want to consider using ansible for key rotation at some point in the future...

Thanks again.

Jason

Reply all
Reply to author
Forward
0 new messages