Ansible:根据文件是否存在创建符号链接

问题描述 投票:0回答:1

我想根据文件是否存在创建符号链接(软链接)。我正在使用 when/register 来尝试实现这一目标,但我想我做错了什么。这是我的剧本;

- 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}
ansible directory filesystems
1个回答
0
投票

我会做类似的事情:

- name: Install java11 with brew
  community.general.homebrew:
    pkg: 'openjdk@11'

- name: Get brew prefix
  ansible.builtin.command:
    cmd: 'brew --prefix'
  register: brew_prefix

- name: Create jdk symlink
  ansible.builtin.file:
    src: '{{ brew_prefix.stdout}}/opt/openjdk@11/libexec/openjdk.jdk'
    dest: '/Library/Java/JavaVirtualMachines/openjdk-11.jdk'
    state: link
© www.soinside.com 2019 - 2024. All rights reserved.