passwordless_ssh.yml 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. failed_when: false
  23. when: "'manager' in group_names"
  24. - name: Verify whether passwordless ssh is set on the remote host
  25. shell: sshpass ssh -o "PasswordAuthentication=no" root@{{ current_host }} 'hostname'
  26. register: ssh_output
  27. async: 30
  28. poll: 5
  29. failed_when: false
  30. changed_when: false
  31. - name: Update ssh connection status
  32. set_fact:
  33. ssh_status: true
  34. when:
  35. - "'Permission denied' not in ssh_output.stderr"
  36. - ssh_output.stdout | length > 2
  37. - name: Verify the public key file existence
  38. stat:
  39. path: "{{ rsa_id_file }}"
  40. register: verify_rsa_id_file
  41. when: not ssh_status
  42. - name: Generate ssh key pair
  43. command: ssh-keygen -t rsa -b 4096 -f "{{ rsa_id_file }}" -q -N "{{ passphrase }}"
  44. when:
  45. - not ssh_status
  46. - not verify_rsa_id_file.stat.exists
  47. - name: Add the key identity
  48. shell: |
  49. eval `ssh-agent -s`
  50. ssh-add "{{ rsa_id_file }}"
  51. when: not ssh_status
  52. - name: Post public key
  53. block:
  54. - name: Create .ssh directory
  55. command: >-
  56. sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
  57. ssh root@"{{ current_host }}" mkdir -p /root/.ssh
  58. when: not ssh_status
  59. no_log: True
  60. register: register_error
  61. - name: Copy the public key to remote host
  62. shell: >-
  63. set -o pipefail && cat "{{ rsa_id_file }}".pub
  64. | sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
  65. ssh root@"{{ current_host }}" 'cat >> "{{ auth_key_path }}"'
  66. when: not ssh_status
  67. no_log: True
  68. register: register_error
  69. - name: Change permissions on the remote host
  70. shell: sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}" ssh root@"{{ current_host }}" 'chmod 700 .ssh; chmod 640 "{{ auth_key_path }}"'
  71. when: not ssh_status
  72. no_log: True
  73. register: register_error
  74. rescue:
  75. - name: Passwordless ssh failed
  76. fail:
  77. msg: "{{ register_error.stderr | regex_replace(hostvars['127.0.0.1']['cobbler_password']) | regex_replace(auth_key_path) }}"