设置符号链接错误 - Ansible

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

我正在尝试设置从源到目标的符号链接,但继续点击

致命:[默认]:失败! => {“changed”:false,“failed”:true,“msg”:“链接时出错:[Errno 2]没有这样的文件或目录”,“path”:“/Applications/Xcode.app/Contents/Developer /Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby/config.h", "状态“:“缺席”}

观察到它显示状态不存在,即使状态以链接形式给出。

这是执行的任务:

 - name: "Create ruby config.h symlink"
  file:
    src: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h"
    dest: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby/config.h"
    state: link
    force: yes 
  when: xcode_version != "8.0"

文件存在:

ls -l /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h
-rw-r--r--  2 root  wheel  7805 Jan 31  2016 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h

注意:包括基于此处讨论的强制是 - Ansbible github 问题

如有任何帮助,我们将不胜感激!

ansible symlink
3个回答
8
投票

我的回答可能无法解决OP问题,但它确实会导致这个问题,因此可能会解释另一个人遇到这个问题。我经常发现此类错误可能是由链接名称中尾随的

/
引起的。

例如,创建如下目录的任务将会成功。

- name: ensure foobar directory exists
  file: 
    state: directory
    path: '/tmp/foobar/'
    owner: 'foo'    
    group: 'bar'
    mode: 0777

但是,如果按如下方式编写后续链接创建任务,将导致您遇到错误。

- name: Establish link to foobar
  file:
    src: '/tmp/foobar/'
    dest: '/local/baz/'
    state: link

导致以下错误。

致命:[默认]:失败! => {“已更改”:假,“失败”:真,“消息”: “链接时出错:[Errno 2]没有这样的文件或目录”,“路径”: “...”, “状态”:“缺席”}

按照如下所示删除

/
,可以解决此问题。

- name: Establish link to foobar
  file:
    src: '/tmp/foobar/'
    dest: '/local/baz'
    state: link

2
投票

我不确定丢失的目录是否是导致问题的原因,但在创建指向该目录的链接之前确保目标目录存在是一个很好的做法:

- name: Ensure that the directory exist
  file:
    path: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby"
    state: directory
    mode: 0755
    owner: root
    group: wheel

- name: "Create ruby config.h symlink"
  file:
  ...

0
投票

使用

force: true
创建符号链接时,您还应该添加选项
follow:false
以使其正常工作。 因此,您的代码应该按以下方式更改:

- name: "Create ruby config.h symlink"
  file:
    src: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h"
    dest: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby/config.h"
    state: link
    force: yes 
    follow: false  #   <-- Note this addition
  when: xcode_version != "8.0"

致谢Davide Del Vento 的回复

© www.soinside.com 2019 - 2024. All rights reserved.