configure_sshd.yml 2.1 KB

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