Create new SSH key and update on remote pi
This script aims to update public key on remote pi by deploying key on them.
[Pre requisites]
- ssh :)
[Execution] execute the first time (because the pi hasn't keys existing) :
- ansible-playbook playbook-ssh.yml -i host-firsttime
execute the other time :
- ansible-playbook playbook-ssh.yml -i hosts
[Content of ]
This playbook aim to memorise the last upload of the ssh key to be able to connect the next time it need to update the certificat.
There is four times:
First Variable update
Second Manage SSH key
Three Upload the current key to remot host
last Save the current key uploaded successfully
- name: Generate key pair
shell: "ssh-keygen -b 2048 -t rsa -f {{ remote_key_remote_path }}/id_rsa -q -N /dev/null"
args:
creates: "{{ remote_key_remote_path }}/id_rsa"
- name: Deploy public key on current host {{ ansible_host }} for user {{ ansible_user }}
authorized_key:
user: "{{ ansible_user }}"
key: "{{ hostvars['master'].master_public_key.stdout }}"
exclusive: "no"
state: "present"
- name: Deploy public key on current host {{ ansible_host }} for user {{ ansible_user }}
authorized_key:
user: "{{ ansible_user }}"
key: "{{ hostvars['master'].master_public_key.stdout }}"
exclusive: "yes"
state: "present"
- authorized_key:
user: "{{ ansible_user }}"
key: "{{ hostvars['master'].master_public_key.stdout }}"
state: absent
- name: Copy ssh file to remote dir
copy: src={{ remote_key_remote_path }}/{{item}} dest={{ remote_key_master_path }}/ remote_src=no directory_mode=yes
with_items:
- "id_rsa"
- "id_rsa.pub"