configure_sshd.yml 2.3 KB

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