How to copy public ssh-keys to a host using ansible

Viewed 1133

I have a Cassandra cluster that contains 6 nodes which I already have in an inventory:

[cassandra]
cassandra-01 ansible_host=192.168.2.10
cassandra-02 ansible_host=192.168.2.11
cassandra-03 ansible_host=192.168.2.12 
cassandra-04 ansible_host=192.168.2.13
cassandra-05 ansible_host=192.168.2.14
cassandra-06 ansible_host=192.168.2.15

I want to copy the public ssh-key from cassandra-01 to all nodes in the cluster as ssh-copy-id would do (even to cassandra-01) and I want to do that using Ansible.

What is the best and idempotent way to achieve this?

P.S. I found this module ssh-copy-id on Github, may be it will work?

1 Answers

You want to use the authorized_key module.

For example:

- name: ensure ssh-key is present
  ansible.posix.authorized_key:
    user: "your-user"
    state: present
    key: "your-public-key-goes-here"
Related