configure_sshd.yml 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: Check if AllowUsers entry exixts
  16. shell: cat "{{ sshd_conf_file }}"
  17. register: file_content
  18. - name: Check if user is specified
  19. debug:
  20. msg: "Users not specified"
  21. when: user | length < 1
  22. - name: Configure sshd_config
  23. block:
  24. - name: Configure sshd_config file when AllowUsers entry not exists
  25. lineinfile:
  26. path: "{{ sshd_conf_file }}"
  27. line: 'AllowUsers {{ user }}'
  28. notify:
  29. - Restart sshd
  30. when:
  31. - allow_deny == 'Allow'
  32. - file_content.stdout.find('AllowUsers') == -1
  33. - name: Configure sshd_config file when DenyUsers entry not exists
  34. lineinfile:
  35. path: "{{ sshd_conf_file }}"
  36. line: 'DenyUsers {{ user }}'
  37. notify:
  38. - Restart sshd
  39. when:
  40. - allow_deny == 'Deny'
  41. - file_content.stdout.find('DenyUsers') == -1
  42. - name: Configure sshd_config file when AllowUsers entry exists
  43. replace:
  44. path: "{{ sshd_conf_file }}"
  45. regexp: '^(AllowUsers)(.*)'
  46. replace: '\1\2 {{ user }}'
  47. notify:
  48. - Restart sshd
  49. when:
  50. - allow_deny == 'Allow'
  51. - file_content.stdout.find('AllowUsers') != -1
  52. - name: Configure sshd_config file when DenyUsers entry exists
  53. replace:
  54. path: "{{ sshd_conf_file }}"
  55. regexp: '^(DenyUsers)(.*)'
  56. replace: '\1\2 {{ user }}'
  57. notify:
  58. - Restart sshd
  59. when:
  60. - allow_deny == 'Deny'
  61. - file_content.stdout.find('DenyUsers') != -1
  62. when:
  63. - user | length > 1