---
- 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: Create directory
    file:  
      path: "{{ item }}"
      state: directory
    with_items: "{{ mycomdir }}"

  - name: Disabled firewalld
    service:
      name: firewalld
      state: stopped
      enabled: no

  - name: Disable SELinux
    selinux:
      state: disabled

  - name: Performance ssh speed
    lineinfile:
      path: /etc/ssh/sshd_config
      regexp: 'UseDNS'
      line: 'UseDNS no'

  - name: Backup repo file
    shell: "mv /etc/yum.repos.d/CentOS-Linux* /etc/yum.repos.d/backup/"
    ignore_errors: true
      
  - name: Wget CentOS8 repo
    get_url:
      url: "{{ item }}"
      dest: /etc/yum.repos.d/
    with_items: "{{ centos8repourl }}"

  - name: Import RPM GPG key
    rpm_key:
      state: present
      key: "{{ item }}"
    with_items: "{{ cent8key }}"

  - name: Remove epel repo
    raw: |
      rm -rf /etc/yum.repos.d/epel.repo
      rm -rf /etc/yum.repos.d/epel-*.repo
    ignore_errors: true

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

  - name: Install kernel package
    yum:
      name: "{{ centos8kernelpackage }}"
      state: present
#    with_items: "{{ centos8kernelpackage }}"

  - name: Backup elrepo file
    shell: "mv /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup"
    ignore_errors: true

  - name: Update kernel to latest
    yum:  
      name: kernel-ml
      enablerepo: elrepo-kernel
      disablerepo: "*"
      state: present

  - name: Set defaut kernel
    shell: "grub2-set-default 0"

  - name: Update system
    dnf: 
      name: '*'
      state: latest
      
  - name: Config my root vimrc
    get_url:
      url: "{{ item }}"
      dest: /root/.vimrc
      backup: yes
    with_items: "{{ myvimrc }}"

  - name: Config my other user vimrc
    get_url:
      url: "{{ item }}"
      dest: /etc/skel/.vimrc
      backup: yes
    with_items: "{{ myvimrc }}"

  - name: Wget chrony config
    get_url:
      url: "{{ item }}"
      dest: /etc/chrony.conf
      backup: yes
    with_items: "{{ mychrony }}"
    
  - name: Started and enabled chrony
    service:
      name: chronyd
      state: started
      enabled: yes
 
  - name: Config root bashrc
    get_url:
      url: "{{ item }}"
      dest: /etc/profile.d/myalias.sh
      backup: yes
    with_items: "{{ mybashrc }}"
  
  - name: Config time zone
    command: localectl set-locale LANG={{ system_locale }}

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

#  - name: debug
#    debug:
#      var: grub_config
  
#  handlers:
#      ignore_errors: true
#    - name: restart network
#      systemd:
#        name: NetworkManager
#        state: restarted
    
#    - name: active network
#      command: nmcli connection up "{{ conn_name }}"
