ansible:为什么文件模块被跳过?


8

我有一个ansible 1.1剧本,我在其中做类似的事情:

- name: copy files
  sudo: True                                                                                                             
  shell: cp /from/* /to/

- name: change owner
  sudo: True
  file: path=$item owner=newuser group=newgroup
  with_fileglob: /to/*

第二项任务“更改所有者”始终处于跳过状态。谁能帮我找出原因?文件模块跳过是因为文件存在?我被卡住了:)


不知道它是否允许$ item ...我以为是{{item}}
Arun Sangal

Answers:


16

文档

请记住,查找插件在“控制”计算机上运行:

with_fileglob 是一个查找插件,因此它将在本地服务器(您正在从中运行ansible-playbook的服务器)上查找文件。

您可以执行以下操作:

- name: list files 
  action: command ls -1 /to/* 
  register: dumpfiles 

- name: change ownership 
  action: file path=$item owner=newuser group=newgroup
  with_items: ${dumpfiles.stdout_lines}

现在,您说出来就很有意义了。顺便说一句,我通过使用[shell:chown -R newuser:newgroup / to]解决了这个问题
2013年

3
在这种情况下,使用shell并不是可取的方法,因为您将失去幂等性。您应该改为使用文件模块和with_items
Tom Aac 2013年

是的,我想使用文件模块,但是with_items不支持globs吗?我真正想要的不是在with_items列表中列出每个文件
Deadsven

见我的答案,你还需要什么
汤姆AAC格式

2
每次您运行chown时,都将更改文件的时间戳。特别是ctime。例如,这对于某些备份软件可能是个问题。
Tom Aac 2014年

5

Ansible 1.1在文件模块中添加了recurse参数,因此您需要执行的更改所有权任务是:

- name: change ownership 
  action: file state=directory recurse=yes path=/to/ owner=newuser group=newgroup

当实际情况发生变化时,这将变得更加明显。使用shell或命令模块将始终返回已更改的状态,即使实际上没有任何更改。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.