---
- name: Config system other config,like disabled firewalld & selinux,install package,update kernel etc.
  hosts: myNewHosts
  become: true

# This is my defined vars,include hostname and other
  vars_files:
    - 00_myvars_config.yml

  tasks:
  - name: Get installed kernel packages
    shell: rpm -qa kernel\* --qf '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n'
    register: kernel_packages

  - name: Get current kernel version
    shell: uname -r
    register: current_kernel_version

  - name: Filter out current kernel version
    set_fact:
      other_kernel_packages: "{{ kernel_packages.stdout_lines | reject('search', current_kernel_version.stdout) | list }}"

  - name: Print other kernel packages
    debug:
      var: other_kernel_packages

  - name: Uninstall other kernel packages
    yum:
      name: "{{ item }}"
      state: absent
    with_items: "{{ other_kernel_packages }}"

  - name: Reboot the system
    shell: "sleep 10 && /sbin/reboot"
    async: 0
    poll: 0
    ignore_unreachable: yes

  - name: Wait 30 seconds
    pause:
      seconds: 30

  - name: Install common  package
    yum:
      name: "{{ centos8commonpackages }}"
      state: latest
      update_cache: yes

  - name: Clean all cache
    raw: |
      yum clean all
      yum makecache
      rm -rf /var/log/anaconda/*
      rm -rf /var/log/boot.log*
      rm -rf /var/log/btmp*
      rm -rf /var/log/chrony/*
      rm -rf /var/log/cloud-init*
      rm -rf /var/log/cron*
      rm -rf /var/log/dmesg\.*
      rm -rf /var/log/dnf*
      rm -rf /var/log/grubby*
      rm -rf /var/log/hawkey.log*
      rm -rf /var/log/httpd/
      rm -rf /var/log/kdump.log
      rm -rf /var/log/messages*
      rm -rf /var/log/openwebmail.log
      rm -rf /var/log/qemu-ga*
      rm -rf /var/log/rhsm*
      rm -rf /var/log/samba*
      rm -rf /var/log/secure*
      rm -rf /var/log/tallylog
      rm -rf /var/log/tuned*
      rm -rf /var/log/update*.log
      rm -rf /var/log/vmware*
      rm -rf /var/log/yum*
      rm -rf /var/spool/mail/root
      echo "" > /var/log/audit/audit.log
      echo "" > /var/log/boot.log
      echo "" > /var/log/btmp
      echo "" > /var/log/dmesg
      echo "" > /var/log/firewalld
      echo "" > /var/log/syslog
      echo "" > /var/log/lastlog
      echo "" > /var/log/wtmp
      echo "" > /var/log/maillog
      echo "" > /var/log/messages
      echo "" > /var/log/maillog
      echo "" > /var/log/ntp.log
      echo "" > /var/log/README
      echo "" > /var/log/secure
      echo "" > /var/log/spooler
      echo "" > /var/log/sssd/sssd.log
      echo "" > $HOME/.bash_history
      rm -rf /etc/yum.repos.d/epel.repo
      rm -rf /etc/yum.repos.d/epel-*.repo
      rm -rf /var/spool/mail/root
      rm -rf /tmp/*
      history -c
    ignore_errors: true

