Ansible问题:基于Brew路径创建Java软链接提示源文件不存在
问题描述
希望根据文件是否存在创建软链接,使用when/register实现逻辑但运行报错。Playbook内容如下:
- name: Check if /usr/local/bin/brew exists stat: path: /usr/local/bin/brew register: file_status_intel - name: Check if /opt/homebrew/bin/brew exists stat: path: /opt/homebrew/bin/brew register: file_status_arm - name: Create library symlink to Java for intel file: src: "/usr/local/bin/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link when: file_status_intel.stat.exists tags: selenium - name: Create library symlink to Java for Mac M1, M2, M3 file: src: "/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link when: file_status_arm.stat.exists tags: selenium
执行后报错:
fatal: [tyr-mac-1-dev]: FAILED! => {"changed": false, "gid": 0, "group": "wheel", "mode": "0755", "msg": "src file does not exist, use \"force=yes\" if you really want to create the link: /usr/local/bin/openjdk@11/libexec/openjdk.jdk", "owner": "root", "path": "/Library/Java/JavaVirtualMachines/openjdk-11.jdk", "size": 45, "src": "/usr/local/bin/openjdk@11/libexec/openjdk.jdk", "state": "link", "uid": 0}
问题分析
当前逻辑的核心错误是:判断的是brew的存在性,但实际创建软链接依赖的是openjdk@11的安装路径。brew存在不代表openjdk@11已经通过brew安装,所以即使file_status_intel.stat.exists为true,目标源文件/usr/local/bin/openjdk@11/libexec/openjdk.jdk可能根本不存在,导致Ansible报错。
解决方案
方案1:直接检查软链接源文件的存在性
替换原来的brew检查,直接检查openjdk的目标路径,确保源文件存在再创建链接:
- name: Check if Intel OpenJDK 11 path exists stat: path: "/usr/local/bin/openjdk@11/libexec/openjdk.jdk" register: intel_jdk_exists - name: Check if ARM OpenJDK 11 path exists stat: path: "/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk" register: arm_jdk_exists - name: Create library symlink to Java for intel file: src: "/usr/local/bin/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link when: intel_jdk_exists.stat.exists tags: selenium - name: Create library symlink to Java for Mac M1, M2, M3 file: src: "/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link when: arm_jdk_exists.stat.exists tags: selenium
方案2:保留brew判断,同时添加源文件存在条件
如果需要保留基于brew的架构判断,可在when条件中同时校验brew存在和源文件存在:
- name: Check if /usr/local/bin/brew exists stat: path: /usr/local/bin/brew register: file_status_intel - name: Check if /opt/homebrew/bin/brew exists stat: path: /opt/homebrew/bin/brew register: file_status_arm - name: Check Intel OpenJDK 11 path stat: path: "/usr/local/bin/openjdk@11/libexec/openjdk.jdk" register: intel_jdk_path - name: Check ARM OpenJDK 11 path stat: path: "/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk" register: arm_jdk_path - name: Create library symlink to Java for intel file: src: "/usr/local/bin/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link when: file_status_intel.stat.exists and intel_jdk_path.stat.exists tags: selenium - name: Create library symlink to Java for Mac M1, M2, M3 file: src: "/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link when: file_status_arm.stat.exists and arm_jdk_path.stat.exists tags: selenium
方案3:使用force=yes(谨慎使用)
如果确认即使源文件不存在也要创建链接(比如后续会手动部署源文件),可以添加force=yes参数,但这会跳过源文件存在性检查:
- name: Create library symlink to Java for intel file: src: "/usr/local/bin/openjdk@11/libexec/openjdk.jdk" dest: "/Library/Java/JavaVirtualMachines/openjdk-11.jdk" state: link force: yes when: file_status_intel.stat.exists tags: selenium
内容的提问来源于stack exchange,提问作者Cyanic Wolf
相关产品推荐
相关产品推荐

