0

I have an inventory which contains several hosts. I want to distribute the public part of the SSH keys via Ansible. Each host gets an own key. So far I found the module authorized_keys which can do the general job. However I was not able to figure out how can distribute the different keys.

My .ssh directory is like:

ls .ssh
hostA hostA.pub
hostB hostB.pub
hostC hostC.pub

For one host I could write:

- name: Set authorized key taken from file
  authorized_key:
    user: joeuser
    state: present
    key: "{{ lookup('file', '/home/joeuser/.ssh/hostA.pub') }}"

But how can I do this for different hosts?

qbi
  • 2,104
  • 1
  • 23
  • 35

1 Answers1

1

If each key is named after the hostname, as is suggested by your question, you could just do:

- name: Set authorized key taken from file
  authorized_key:
    user: joeuser
    state: present
    key: "{{ lookup('file', '/home/joeuser/.ssh/{}.pub'.format(inventory_hostname)) }}"
larsks
  • 277,717
  • 41
  • 399
  • 399