--- - name: Uninstall and Revert Elasticsearch Configuration hosts: elasticsearch_hosts become: true vars: # Set this to 'false' if you want to keep Java (OpenJDK) installed on the system # because other applications might be using it. remove_java: true tasks: - name: "BLOCK: Stop and Remove Elasticsearch Service" block: - name: Stop and disable Elasticsearch service ansible.builtin.systemd: name: elasticsearch state: stopped enabled: false ignore_errors: true # Ignore errors if the service doesn't exist - name: Purge Elasticsearch package and its config files ansible.builtin.apt: name: elasticsearch state: absent autoremove: true purge: true ignore_errors: true # Ignore errors if the package isn't installed - name: "BLOCK: Clean Up Elasticsearch Files and Directories" block: # --- MODIFIED SECTION START --- - name: Find all contents within the /var/lib/elasticsearch mountpoint ansible.builtin.find: paths: /var/lib/elasticsearch hidden: true register: contents_to_delete - name: Delete all contents found within the mountpoint (leaving the mountpoint itself) ansible.builtin.file: path: "{{ item.path }}" state: absent loop: "{{ contents_to_delete.files | sort(attribute='path', reverse=true) }}" loop_control: label: "{{ item.path }}" # --- MODIFIED SECTION END --- - name: Remove Elasticsearch log directory ansible.builtin.file: path: /var/log/elasticsearch state: absent - name: Remove Elasticsearch config directory (just in case purge missed it) ansible.builtin.file: path: /etc/elasticsearch state: absent - name: "BLOCK: Remove APT Repository and GPG Key" block: - name: Remove Elasticsearch APT repository ansible.builtin.apt_repository: repo: "deb https://artifacts.elastic.co/packages/9.x/apt stable main" state: absent filename: elastic-9.x - name: Remove Elasticsearch GPG key ansible.builtin.file: path: /etc/apt/trusted.gpg.d/elasticsearch.gpg state: absent - name: "BLOCK: Revert System Tuning" block: - name: Revert vm.max_map_count setting ansible.posix.sysctl: name: vm.max_map_count state: absent reload: true - name: Remove Elasticsearch user memory limit community.general.pam_limits: domain: "elasticsearch" limit_type: "-" limit_item: "memlock" state: absent - name: Remove Elasticsearch user file descriptor limit community.general.pam_limits: domain: "elasticsearch" limit_type: "-" limit_item: "nofile" state: absent - name: "BLOCK: (Optional) Uninstall Java" block: - name: Uninstall OpenJDK package ansible.builtin.apt: name: openjdk-21-jdk state: absent autoremove: true when: remove_java - name: Final APT cache update ansible.builtin.apt: update_cache: true