---
- name: Clear all aliases on hcp1 and hcp2
  hosts: hcp1,hcp2
  become: yes
  
  tasks:
    - name: Check current aliases
      shell: alias || echo "No aliases found"
      register: current_aliases
      
    - name: Display current aliases
      debug:
        msg: "Current aliases: {{ current_aliases.stdout_lines }}"
    
    - name: Clear aliases from /root/.bashrc
      shell: |
        sed -i '/^alias /d' /root/.bashrc
        sed -i '/^alias\t/d' /root/.bashrc
      ignore_errors: yes
        
    - name: Clear aliases from /root/.profile
      shell: |
        sed -i '/^alias /d' /root/.profile
        sed -i '/^alias\t/d' /root/.profile
      ignore_errors: yes
        
    - name: Clear aliases from /root/.zshrc
      shell: |
        sed -i '/^alias /d' /root/.zshrc
        sed -i '/^alias\t/d' /root/.zshrc
      ignore_errors: yes
        
    - name: Clear aliases from /etc/bash.bashrc
      shell: |
        sed -i '/^alias /d' /etc/bash.bashrc
        sed -i '/^alias\t/d' /etc/bash.bashrc
      ignore_errors: yes
        
    - name: Clear aliases from /etc/profile
      shell: |
        sed -i '/^alias /d' /etc/profile
        sed -i '/^alias\t/d' /etc/profile
      ignore_errors: yes
        
    - name: Find and clear custom alias files
      find:
        paths: ["/root", "/etc", "/home"]
        patterns: ["*.aliases", ".aliases", "aliases"]
        recurse: yes
      register: alias_files
      
    - name: Remove found alias files
      file:
        path: "{{ item.path }}"
        state: absent
      loop: "{{ alias_files.files }}"
      when: alias_files.files is defined
      
    - name: Clear aliases from /etc/profile.d/aliases.sh
      ansible.builtin.file:
        path: /etc/profile.d/aliases.sh
        state: absent
    
    - name: Clear aliases from /root/.bashrc
      ansible.builtin.lineinfile:
        path: /root/.bashrc
        state: absent
        regexp: "^alias "
    
    - name: Clear aliases from /root/.bash_aliases
      ansible.builtin.file:
        path: /root/.bash_aliases
        state: absent
    
    - name: Clear history
      ansible.builtin.command:
        cmd: > /root/.bash_history
    
    - name: Restart shell to apply changes
      ansible.builtin.command:
        cmd: pkill -f bash || true
        
    - name: Test network connectivity after clearing aliases
      shell: ping -c 2 8.8.8.8 || echo "Ping failed"
      register: ping_test
      
    - name: Display ping test result
      debug:
        msg: "Ping test: {{ ping_test.stdout_lines }}"