passwordless_ssh.yml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. ---
  15. - name: Initialize variables
  16. set_fact:
  17. ssh_status: false
  18. current_host: "{{ item }}"
  19. - name: Refresh ssh-key if changed
  20. command: ssh-keygen -R {{ current_host }}
  21. changed_when: False
  22. ignore_errors: yes
  23. when: "'manager' in group_names"
  24. - name: Verify whether passwordless ssh is set on the remote host
  25. command: ssh -o PasswordAuthentication=no root@"{{ current_host }}" 'hostname'
  26. register: ssh_output
  27. ignore_errors: yes
  28. changed_when: False
  29. - name: Update ssh connection status
  30. set_fact:
  31. ssh_status: true
  32. when: "'Permission denied' not in ssh_output.stderr"
  33. - name: Verify the public key file existence
  34. stat:
  35. path: "{{ rsa_id_file }}"
  36. register: verify_rsa_id_file
  37. when: not ssh_status
  38. - name: Generate ssh key pair
  39. command: ssh-keygen -t rsa -b 4096 -f "{{ rsa_id_file }}" -q -N "{{ passphrase }}"
  40. when:
  41. - not ssh_status
  42. - not verify_rsa_id_file.stat.exists
  43. - name: Add the key identity
  44. shell: |
  45. eval `ssh-agent -s`
  46. ssh-add "{{ rsa_id_file }}"
  47. when: not ssh_status
  48. - name: Post public key
  49. block:
  50. - name: Create .ssh directory
  51. command: >-
  52. sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
  53. ssh root@"{{ current_host }}" mkdir -p /root/.ssh
  54. when: not ssh_status
  55. no_log: True
  56. register: register_error
  57. - name: Copy the public key to remote host
  58. shell: >-
  59. set -o pipefail && cat "{{ rsa_id_file }}".pub
  60. | sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
  61. ssh root@"{{ current_host }}" 'cat >> "{{ auth_key_path }}"'
  62. when: not ssh_status
  63. no_log: True
  64. register: register_error
  65. - name: Change permissions on the remote host
  66. shell: sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}" ssh root@"{{ current_host }}" 'chmod 700 .ssh; chmod 640 "{{ auth_key_path }}"'
  67. when: not ssh_status
  68. no_log: True
  69. register: register_error
  70. rescue:
  71. - name: Passwordless ssh failed
  72. fail:
  73. msg: "{{ register_error.stderr | regex_replace(hostvars['127.0.0.1']['cobbler_password']) | regex_replace(auth_key_path) }}"