--- - name: Replace Idle JDK hosts: all gather_facts: false vars: jdk_download_url: "" tasks: - name: Check running Java processes command: ps -ef | grep java | grep -v grep register: java_processes changed_when: false - name: Gather JDK paths from running processes set_fact: running_jdk_paths: "{{ java_processes.stdout_lines | map('regex_replace', '^.*?(/.*?/bin/java).*$', '\\1') | list }}" - name: Find all JDK installations find: paths: /path/to/jdk patterns: 'jdk*' register: jdk_installations - name: Filter out idle JDKs set_fact: idle_jdk_paths: "{{ jdk_installations.files | selectattr('path', 'not in', running_jdk_paths) | map(attribute='path') | list }}" - name: Check if all JDKs are in use fail: msg: "All JDKs are in use. No idle JDKs found." when: idle_jdk_paths | length == 0 - name: Display idle JDK paths debug: msg: "Idle JDK paths: {{ idle_jdk_paths }}" - name: Prompt for JDK download URL pause: prompt: "Please enter the JDK download URL" register: download_url_input - name: Set download URL set_fact: jdk_download_url: "{{ download_url_input.user_input }}" - name: Download JDK get_url: url: "{{ jdk_download_url }}" dest: /tmp/jdk_download.tar.gz - name: Unarchive JDK unarchive: src: /tmp/jdk_download.tar.gz dest: /path/to/jdk/ remote_src: yes - name: Replace idle JDK command: mv /path/to/jdk/idle_jdk /path/to/jdk/old_jdk when: idle_jdk_paths | length > 0 - name: Notify user debug: msg: "Idle JDK replaced with the downloaded JDK."
标签:paths,name,JDK,jdk,idle,download From: https://www.cnblogs.com/abc0012383/p/18430104