auth_failure_check.yml 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: Authentication failure alert mail
  16. hosts: localhost
  17. connection: local
  18. vars:
  19. alert_file_path: /tmp/alerting
  20. auth_failure_check_time: 60
  21. auth_failure_search_key: "authentication failure"
  22. auth_failure_info_file: "{{ alert_file_path }}/auth_failure_{{ ansible_date_time.iso8601_basic_short }}.txt"
  23. auth_failure_mail_subject: "Alert - Authentication Failure"
  24. auth_failure_mail_body: "Attached the authentication failure report"
  25. auth_failure_mail_sender: omnia-alert
  26. file_mode: 644
  27. tasks:
  28. - name: Check auth failure in last {{ auth_failure_check_time }} minutes
  29. shell: journalctl -u sshd --since "{{ auth_failure_check_time }} minutes ago" | grep "{{ auth_failure_search_key }}"
  30. changed_when: false
  31. failed_when: false
  32. register: auth_failure_check
  33. - name: Create alerting log directory
  34. file:
  35. path: "{{ alert_file_path }}"
  36. state: directory
  37. mode: "{{ file_mode }}"
  38. - name: Save the authentication failure info
  39. copy:
  40. dest: "{{ auth_failure_info_file }}"
  41. content: |
  42. "{{ auth_failure_check.stdout }}"
  43. mode: "{{ file_mode }}"
  44. when: auth_failure_search_key in auth_failure_check.stdout
  45. - name: Sent mail on auth failure
  46. community.general.mail:
  47. subject: "{{ auth_failure_mail_subject }}"
  48. body: "{{ auth_failure_mail_body }}"
  49. sender: "{{ auth_failure_mail_sender }}"
  50. to: "{{ alert_email_address }}"
  51. attach:
  52. - "{{ auth_failure_info_file }}"
  53. when: auth_failure_search_key in auth_failure_check.stdout
  54. - name: Delete the authentication failure info file
  55. file:
  56. path: "{{ auth_failure_info_file }}"
  57. state: absent