I'm setting up some machines with Ansible and need to enable password less connections between them. I've got a database master and several slaves. For initial replication the slaves need to ssh into the master and get a copy of the database.
I'm not sure what is the best way to dynamically add all the slaves public keys to the masters authorized_keys
file.
I already thought about providing the slaves public keys as variables and then add them via the authorized_key
module. But then I must maintain the list of keys. I'm looking for an approach where I just add another host the the slaves group and the rest will work automatically.
Any ideas?
Update:
So far I got the following pseudo code:
# collect public keys from slave machines
- name: collect slave keys
{% for host in groups['databases_slave'] %}
shell: /bin/cat /var/lib/postgresql/.ssh/id_rsa.pub
register: slave_keys #how to add to an array here?
{% endfor %}
# Tasks for PostgreSQL master
- name: add slave public key
sudo: yes
authorized_key: user=postgres state=present key={{ item }}
with_items: slave_keys
The loop with the {% %}
only works in template files and not in playbooks directly. Any way to do this in my playbook?