- name: authorized_key
  authorized_key:
    user: "{{ ansible_user_id }}"
    key: "{{ authorized_keys }}"
  when: not (not authorized_keys)
- name: sudoers
  become: true
  copy:
    dest: '/etc/sudoers.d/mycard_sudoers_{{ ansible_user_id }}'
    content: '{{ ansible_user_id }} ALL=(ALL:ALL) NOPASSWD: ALL'
    owner: root
    group: root
    mode: 0700
  when: ansible_user_id != 'root'
- name: hostname
  become: true
  hostname:
    name: "{{ inventory_hostname_short }}"
  when: inventory_hostname_short != '10' and inventory_hostname_short != '172' and inventory_hostname_short != '192'
- name: hostname hosts
  become: true
  lineinfile:
    dest: /etc/hosts
    regexp: "^{{ ansible_default_ipv4.address }}"
    line: "{{ ansible_default_ipv4.address }} {{ inventory_hostname }} {{ inventory_hostname_short }}"
  when: inventory_hostname_short != '10' and inventory_hostname_short != '172' and inventory_hostname_short != '192'
- name: clean aliyun
  become: true
  shell: |
    curl -L http://update.aegis.aliyun.com/download/uninstall.sh | bash
    systemctl stop aliyun.service
    systemctl disable aliyun.service
    rm -rf /usr/local/aegis /etc/init.d/agentwatch /etc/systemd/system/aliyun.service /usr/sbin/aliyun-service /usr/sbin/aliyun-service.backup /usr/sbin/aliyun_installer
    systemctl daemon-reload
  args:
    removes: /usr/local/aegis
- name: clean aliyun 2
  become: true
  shell: |
    /usr/local/cloudmonitor/CmsGoAgent.linux-amd64 stop
    /usr/local/cloudmonitor/CmsGoAgent.linux-amd64 uninstall
    rm -rf /usr/local/cloudmonitor
  args:
    removes: /usr/local/cloudmonitor
- name: clean txyun
  become: true
  shell: |
    /usr/local/qcloud/stargate/admin/uninstall.sh
    /usr/local/qcloud/YunJing/uninst.sh
    /usr/local/qcloud/monitor/barad/admin/uninstall.sh
    rm -rf /usr/local/qcloud
  args:
    removes: /usr/local/qcloud
- name: timezone
  become: true
  timezone:
    name: Asia/Shanghai
- name: set facts for China mirror
  set_fact:
    mirror_debian: http://mirrors.tuna.tsinghua.edu.cn
    mirror_debian_security: http://mirrors.tuna.tsinghua.edu.cn
    mirror_ubuntu: http://mirrors.tuna.tsinghua.edu.cn
    mirror_ubuntu_security: http://mirrors.tuna.tsinghua.edu.cn
  when: china_mirror
- name: keep old distribution
  set_fact:
    target_distribution: '{{ ansible_distribution_release }}'
  when: not upgrade or ansible_distribution_release == 'sid'
- name: move to focal
  set_fact:
    target_distribution: 'focal'
  when: ansible_distribution == 'Ubuntu' and upgrade
- name: move to buster
  set_fact:
    target_distribution: 'buster'
  when: ansible_distribution == 'Debian' and upgrade and ansible_distribution_release != 'sid'
- name: 系统源 (apt)
  become: true
  template:
    src: sources.list.j2
    dest: /etc/apt/sources.list
  when: ansible_os_family == 'Debian'
- name: remove pve enterprise source list
  become: true
  file:
    name: /etc/apt/sources.list.d/pve-enterprise.list
    state: absent
- name: clean some Debian packages
  become: true
  apt:
    name: ufw
    state: absent
    autoremove: yes
    purge: yes
  when: ansible_os_family == 'Debian'
- name: remove not proper installed wireguard
  become: true
  apt:
    name: wireguard,wireguard-tools,wireguard-dkms
    state: absent
    autoremove: yes
    purge: yes
  when: ansible_distribution == 'Ubuntu' and upgrade and ansible_distribution_release != 'focal'
- name: remove not proper installed wireguard
  become: true
  apt:
    name: wireguard,wireguard-tools,wireguard-dkms
    state: absent
    autoremove: yes
    purge: yes
  when: ansible_distribution == 'Ubuntu' and upgrade and ansible_distribution_release != 'focal'
- name: remove not proper installed wireguard repo
  become: true
  file:
    path: '/etc/apt/sources.list.d/wireguard_ubuntu_wireguard_{{ ansible_distribution_release|lower }}.list'
    state: absent
  when: ansible_distribution == 'Ubuntu' and (upgrade or ansible_distribution_release == 'focal')
