Skip to content

Instantly share code, notes, and snippets.

@harlowja
Created October 19, 2018 16:48
Show Gist options
  • Save harlowja/0fd3d681b4b65ee3d259d016356edde9 to your computer and use it in GitHub Desktop.
Save harlowja/0fd3d681b4b65ee3d259d016356edde9 to your computer and use it in GitHub Desktop.
- name: "Create temporary public keys directory"
become_user: jenkins
become: yes
tempfile:
state: directory
suffix: keys
register: output
- name: "Capture all slaves host keys into known_hosts"
become: yes
become_user: jenkins
shell: |
exists=$(grep {{ item }} ~/.ssh/known_hosts) || true
if [ -z "$exists" ]; then
ssh-keyscan {{ item }} -t rsa >> ~/.ssh/known_hosts
fi
with_items:
- gtx1
- gtx3
- gtx4
- gtx6
- gtx7
- name: "Gather all slave machines public keys"
become: yes
become_user: jenkins
shell: |
scp jenkins@{{ item }}:/home/jenkins/.ssh/id_rsa.pub {{ output.path }}/{{ item }}_id_rsa.pub
with_items:
- gtx1
- gtx3
- gtx4
- gtx6
- gtx7
- name: "Combine all slave machines public keys"
become: yes
become_user: jenkins
shell: |
cd {{ output.path }}
for k in $(ls *id_rsa.pub); do
cat $k >> combined.pub
echo "" >> combined.pub
done
- name: "Slurp all slave machines public keys"
become: yes
slurp:
src: "{{ output.path }}/combined.pub"
register: keys
- name: "Inject all slave machines public keys into authorized keys"
become: yes
authorized_key:
user: jenkins
key: "{{ keys['content'] | b64decode }}"
state: present
- name: "Removing temporary public keys directory"
become: yes
file:
path: "{{ output.path }}"
state: absent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment