1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- ---
- - name: Initialize variables
- set_fact:
- ssh_status: false
- current_host: "{{ item }}"
- - name: Verify whether passwordless ssh is set on the remote host
- command: ssh -o PasswordAuthentication=no root@"{{ current_host }}" 'hostname'
- register: ssh_output
- ignore_errors: yes
- changed_when: False
- - name: Update ssh connection status
- set_fact:
- ssh_status: true
- when: "'Permission denied' not in ssh_output.stderr"
- - name: Verify the public key file existence
- stat:
- path: "{{ rsa_id_file }}"
- register: verify_rsa_id_file
- when: not ssh_status
- - name: Generate ssh key pair
- command: ssh-keygen -t rsa -b 4096 -f "{{ rsa_id_file }}" -q -N "{{ passphrase }}"
- when:
- - not ssh_status
- - not verify_rsa_id_file.stat.exists
- - name: Add the key identity
- shell: |
- eval `ssh-agent -s`
- ssh-add "{{ rsa_id_file }}"
- when: not ssh_status
- - name: Create .ssh directory
- command: >-
- sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
- ssh root@"{{ current_host }}" mkdir -p /root/.ssh
- when: not ssh_status
- - name: Copy the public key to remote host
- shell: >-
- set -o pipefail && cat "{{ rsa_id_file }}".pub
- | sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
- ssh root@"{{ current_host }}" 'cat >> "{{ auth_key_path }}"'
- when: not ssh_status
- - name: Change permissions on the remote host
- shell: sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}" ssh root@"{{ current_host }}" 'chmod 700 .ssh; chmod 640 "{{ auth_key_path }}"'
- when: not ssh_status
|