passwordless_ssh.yml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2022 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. command: 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: Create rsa_id file if it doesn't exist
  43. ansible.builtin.file:
  44. path: "{{ rsa_id_file }}"
  45. state: touch
  46. mode: "{{ ssh_file_mode }}"
  47. when:
  48. - not ssh_status
  49. - not verify_rsa_id_file.stat.exists
  50. - name: Generate ssh key pair
  51. shell: ssh-keygen -t rsa -b 4096 -f "{{ rsa_id_file }}" -q -N "{{ passphrase }}" <<<y >/dev/null 2>&1
  52. when:
  53. - not ssh_status
  54. - name: Creating ssh config file with IdentifyFile value
  55. copy:
  56. dest: "{{ config_file }}"
  57. content: |
  58. Host *
  59. IdentityFile "{{ rsa_id_file }}"
  60. mode: "{{ ssh_file_mode }}"
  61. - name: Add the key identity
  62. shell: |
  63. eval `ssh-agent -s`
  64. ssh-add "{{ rsa_id_file }}"
  65. when: not ssh_status
  66. - name: Post public key
  67. block:
  68. - name: Create .ssh directory
  69. command: >-
  70. sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
  71. ssh root@"{{ current_host }}" mkdir -p /root/.ssh
  72. when: not ssh_status
  73. no_log: True
  74. register: register_error
  75. - name: Copy the public key to remote host
  76. shell: >-
  77. set -o pipefail && cat "{{ rsa_id_file }}".pub
  78. | sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}"
  79. ssh root@"{{ current_host }}" 'cat >> "{{ auth_key_path }}"'
  80. when: not ssh_status
  81. no_log: True
  82. register: register_error
  83. - name: Change permissions on the remote host
  84. shell: sshpass -p "{{ hostvars['127.0.0.1']['cobbler_password'] }}" ssh root@"{{ current_host }}" 'chmod 700 .ssh; chmod 640 "{{ auth_key_path }}"'
  85. when: not ssh_status
  86. no_log: True
  87. register: register_error
  88. rescue:
  89. - name: Passwordless ssh failed
  90. fail:
  91. msg: "{{ register_error.stderr | regex_replace(hostvars['127.0.0.1']['cobbler_password']) | regex_replace(auth_key_path) }}"