2015-04-14 22:02:36 +00:00
# tags defined: [check], services, updates, restart, fileverify, iptables, selinux
# for the fix part, I guess its better to include the role(s) for particular host that brings the system
2025-02-14 12:38:19 -05:00
# to the desired state in terms of: services, updates, file verification, iptables, nftables, and selinux
2015-04-14 22:02:36 +00:00
---
- hosts : "{{ target }}"
user : root
vars :
- datadir_prfx_path : "/var/tmp/ansible-chk-host/"
tasks :
2025-01-14 20:18:57 +10:00
- name : Create temp dir for collecting info
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : mktemp -d
2015-04-14 22:02:36 +00:00
register : temp_dir
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
- name : Get list of active loaded services with systemctl
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : '/bin/systemctl -t service --no-legend | egrep "loaded active" | tr -s " " | cut -d " " -f1'
2024-11-25 18:24:56 +10:00
changed_when : false
2019-09-19 19:27:25 +00:00
when : ansible_distribution_major_version|int >= 29 and ansible_distribution == 'Fedora'
2015-04-14 22:02:36 +00:00
register : loaded_active_services_systemctl
tags :
- check
- services
2019-09-02 12:14:18 +02:00
- name : Get list of active loaded services with systemctl
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : '/bin/systemctl -t service --no-legend | egrep "loaded active" | tr -s " " | cut -d " " -f1'
2024-11-25 18:24:56 +10:00
changed_when : false
2019-09-19 19:27:25 +00:00
when : ansible_distribution_major_version|int > 6 and ansible_distribution == 'RedHat'
2019-09-02 12:14:18 +02:00
register : loaded_active_services_systemctl
tags :
- check
- services
- name : Get list of inactive loaded services with systemctl
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : '/bin/systemctl -t service --no-legend | egrep -v "loaded active" | tr -s " " | cut -d " " -f1'
2024-11-25 18:24:56 +10:00
changed_when : false
2019-09-19 19:27:25 +00:00
when : ansible_distribution_major_version|int >= 29 and ansible_distribution == 'Fedora'
2019-09-02 12:14:18 +02:00
register : loaded_inactive_services_systemctl
tags :
- check
- services
2015-04-14 22:02:36 +00:00
- name : Get list of inactive loaded services with systemctl
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : '/bin/systemctl -t service --no-legend | egrep -v "loaded active" | tr -s " " | cut -d " " -f1'
2024-11-25 18:24:56 +10:00
changed_when : false
2019-09-19 19:27:25 +00:00
when : ansible_distribution_major_version|int > 6 and ansible_distribution == 'RedHat'
2015-04-14 22:02:36 +00:00
register : loaded_inactive_services_systemctl
tags :
- check
- services
- name : Get list of enabled services with chkconfig at current runlevel
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : "chkconfig | grep \"`runlevel | cut -d ' ' -f 2`:on\" | awk '{print $1}'"
2024-11-25 18:24:56 +10:00
changed_when : false
2019-09-19 19:27:25 +00:00
when : ansible_distribution_major_version|int <= 6 and ansible_distribution == 'RedHat'
2015-04-14 22:02:36 +00:00
register : enabled_services_chkconfig
tags :
- check
- services
- name : Get list of disabled services with chkconfig at current runlevel
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : "chkconfig | grep \"`runlevel | cut -d ' ' -f 2`:off\" | awk '{print $1}'"
2024-11-25 18:24:56 +10:00
changed_when : false
2019-09-19 19:27:25 +00:00
when : ansible_distribution_major_version|int <= 6 and ansible_distribution == 'RedHat'
2015-04-14 22:02:36 +00:00
register : disabled_services_chkconfig
tags :
- check
- services
2025-01-14 20:18:57 +10:00
- name : Output enabled service list chkconfig
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{enabled_services_chkconfig.stdout_lines}} >> {{temp_dir.stdout}}/eservices
2015-04-14 22:02:36 +00:00
when : enabled_services_chkconfig is defined and enabled_services_chkconfig.rc == 0
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- services
2025-01-14 20:18:57 +10:00
- name : Output disabled loaded service list chkconfig
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{disabled_services_chkconfig.stdout_lines}} >> {{temp_dir.stdout}}/dservices
2015-04-14 22:02:36 +00:00
when : disabled_services_chkconfig is defined and disabled_services_chkconfig.rc == 0
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- services
2025-01-14 20:18:57 +10:00
- name : Output loaded active service list systemctl
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{loaded_active_services_systemctl.stdout_lines}} >> {{temp_dir.stdout}}/laservices
2015-04-14 22:02:36 +00:00
when : loaded_active_services_systemctl is defined and loaded_active_services_systemctl.rc == 0
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- services
2025-01-14 20:18:57 +10:00
- name : Output loaded inactive service list systemctl
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{loaded_inactive_services_systemctl.stdout_lines}} >> {{temp_dir.stdout}}/liservices
2015-04-14 22:02:36 +00:00
when : loaded_inactive_services_systemctl is defined and loaded_inactive_services_systemctl.rc == 0
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- services
- name : Check for pending updates
2016-05-11 15:08:50 +00:00
# script: {{ scripts }}/needs-updates --host {{ inventory_hostname }}
script : needs-updates --host {{ inventory_hostname }}
2015-04-14 22:02:36 +00:00
register : list_update
delegate_to : 127.0 .0 .1
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- updates
- name : Show pending updates
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{list_update.stdout_lines}} >> {{temp_dir.stdout}}/pending_updates
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- updates
- name : Get processes that need restarting
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : needs-restarting
2015-04-14 22:02:36 +00:00
register : needs_restarting
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- restart
- name : Show processes that need restarting
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{needs_restarting.stdout_lines}} >> {{temp_dir.stdout}}/needing_restart
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- restart
- name : Get locally changed files from the rpm package
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : rpm_tmp_var=`mktemp` && ! rpm -Va 2>/dev/null > $rpm_tmp_var && [[ -s $rpm_tmp_var ]] && echo $rpm_tmp_var warn=no
2015-04-14 22:02:36 +00:00
register : localchanges
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- fileverify
- name : Get locally changed files (excluding config files)
2024-12-19 11:22:24 +10:00
ansible.builtin.command : "egrep -v ' c /' {{ localchanges.stdout }}"
2015-04-14 22:02:36 +00:00
register : rpm_va_nc
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
when : localchanges is defined and localchanges.stdout != ""
tags :
- check
- fileverify
- name : Show locally changed files (excluding config files)
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{rpm_va_nc.stdout_lines}} >> {{temp_dir.stdout}}/local_changed
2015-04-14 22:02:36 +00:00
when : rpm_va_nc.stdout != ""
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- fileverify
- name : 'Whitelist - Get locally changed files (config files)'
2024-12-19 11:22:24 +10:00
ansible.builtin.command : "egrep ' c /' {{ localchanges.stdout }}"
2015-04-14 22:02:36 +00:00
register : rpm_va_c
when : localchanges is defined and localchanges.stdout != ""
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- fileverify
- name : 'Whitelist - Show locally changed files (config files)'
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo {{rpm_va_c.stdout_lines}} >> {{temp_dir.stdout}}/local_config_changed
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
when : rpm_va_c.stdout != ""
tags :
- check
- fileverify
- name : Check if using iptables
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : /sbin/iptables -S
2015-04-14 22:02:36 +00:00
register : iptablesn
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- iptables
2025-02-14 12:38:19 -05:00
- name : Check if using nftables
ansible.builtin.shell : /sbin/nft list ruleset
register : nftablesn
changed_when : false
tags :
- check
- iptables
2015-04-14 22:02:36 +00:00
- name : Show iptables rules
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo "{{iptablesn.stdout_lines}}" >> {{ temp_dir.stdout }}/iptables
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- iptables
2025-02-14 12:38:19 -05:00
- name : Show nftables rules
ansible.builtin.shell : echo "{{nftablesn.stdout_lines}}" >> {{ temp_dir.stdout }}/nftables
changed_when : false
tags :
- check
- iptables
2015-04-14 22:02:36 +00:00
- name : Show current SELinux status
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo "SELinux is {{ ansible_selinux.status }} for this System" >> {{temp_dir.stdout}}/selinux
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- selinux
2016-08-08 19:36:31 +00:00
- name : Show Boot SELinux mode
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo "SELinux boots to {{ ansible_selinux.config_mode }} mode " >> {{temp_dir.stdout}}/selinux
2015-04-14 22:02:36 +00:00
when : ansible_selinux.status != "disabled"
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- selinux
- name : Show Current SELinux mode
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo "SELinux currently is in {{ ansible_selinux.mode }} mode" >> {{temp_dir.stdout}}/selinux
2015-04-14 22:02:36 +00:00
when : ansible_selinux.status != "disabled"
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- selinux
- name : Match current SELinux status with boot status
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo "SElinux Current and Boot modes are in sync" >> {{temp_dir.stdout}}/selinux
2015-04-14 22:02:36 +00:00
when : ansible_selinux.status != "disabled" and ansible_selinux.config_mode == ansible_selinux.mode
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- selinux
2025-01-14 20:18:57 +10:00
- name : MisMatch current SELinux status with boot status
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : echo "SElinux Current and Boot modes are NOT in sync" >> {{temp_dir.stdout}}/selinux
2015-04-14 22:02:36 +00:00
when : ansible_selinux.status != "disabled" and ansible_selinux.config_mode != ansible_selinux.mode
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
tags :
- check
- selinux
2025-01-14 20:18:57 +10:00
- name : Resolve last persisted dir - if one is present
2016-05-11 15:08:50 +00:00
local_action : shell ls -d -1 {{datadir_prfx_path}}/{{inventory_hostname}}-* 2>/dev/null | sort -r | head -1
2015-04-14 22:02:36 +00:00
register : last_dir
2024-11-25 18:24:56 +10:00
changed_when : false
ignore_errors : true
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Get file list
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : ls -1 {{temp_dir.stdout}}/*
2015-04-14 22:02:36 +00:00
register : file_list
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Get timestamp
2024-12-19 16:42:30 +10:00
ansible.builtin.shell : "date +%Y-%m-%d-%H-%M-%S"
2015-04-14 22:02:36 +00:00
register : timestamp
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Create persisting-state directory
2016-05-11 15:08:50 +00:00
local_action : file path=/{{datadir_prfx_path}}/{{inventory_hostname}}-{{timestamp.stdout}} state=directory
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Fetch file list
2016-05-11 15:08:50 +00:00
fetch : src={{item}} dest=/{{datadir_prfx_path}}/{{inventory_hostname}}-{{timestamp.stdout}}/ flat=true
2017-01-31 17:56:14 +00:00
with_items : "{{file_list.stdout_lines}}"
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Diff the new files with last ones presisted
2016-05-11 15:08:50 +00:00
local_action : shell for file in {{datadir_prfx_path}}/{{inventory_hostname}}-{{timestamp.stdout}}/*; do filename=$(basename $file); diff {{datadir_prfx_path}}/{{inventory_hostname}}-{{timestamp.stdout}}/$filename {{last_dir.stdout.strip(':')}}/$filename; done
2024-11-25 18:24:56 +10:00
ignore_errors : true
changed_when : false
2015-04-14 22:02:36 +00:00
register : file_diff
when : last_dir is defined and last_dir.stdout != ""
2016-08-08 19:36:31 +00:00
2025-01-14 20:18:57 +10:00
- name : Display diff
2015-04-14 22:02:36 +00:00
debug : var=file_diff.stdout_lines
2024-11-25 18:24:56 +10:00
ignore_errors : true
changed_when : false
2015-04-14 22:02:36 +00:00
when : file_diff is defined
2024-11-25 18:24:56 +10:00
# clean up: can also be put as handlers
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Clean remote temp dir
2024-12-17 15:31:55 +10:00
ansible.builtin.file : path={{temp_dir.stdout}} state=absent
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
2025-01-14 20:18:57 +10:00
- name : Clean rpm temp file
2024-12-17 15:31:55 +10:00
ansible.builtin.file : path={{localchanges.stdout}} state=absent
2024-11-25 18:24:56 +10:00
changed_when : false
2015-04-14 22:02:36 +00:00
2016-08-08 19:36:31 +00:00
2015-04-14 22:02:36 +00:00
# handlers:
2017-10-15 20:33:11 +00:00
# - import_tasks: "{{ handlers_path }}/restart_services.yml"
2017-10-17 17:37:03 +00:00
# - import_tasks: "restart_services.yml"