8

I have an ansible 1.1 playbook where I do something like this:

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

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

The second task, "change owner" is always skipping. can anyone help me finding out why? is the file module skipping because the files exist? I'm stuck :)

  • Never knew if it allows $item ...I thought it was {{ item }} – Arun Sangal Dec 9 '15 at 1:07
16

From documentation:

Remember lookup plugins are run on the "controlling" machine:

with_fileglob is a lookup plugin, so it looks for files on the local server, the one you are running ansible-playbook from.

Here is what you can do:

- 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}
  • that makes so much sense now that you say it. BTW, I solved this by using [shell: chown -R newuser:newgroup /to] – deadsven May 29 '13 at 14:12
  • 3
    Using shell in this case is not the preferable way since you are losing idempotency. You should instead use file module and with_items – Tom Aac May 30 '13 at 6:59
  • yes, i'd like to use the file module, but with_items does not support globs does it? listing every file in a with_items list is not really what I want – deadsven May 30 '13 at 11:12
  • See my answer, there is what you need – Tom Aac May 31 '13 at 14:17
  • 2
    Every time you run chown, you change file's timestamp. In particular ctime. That might be issue for some backup software for example. – Tom Aac Jun 13 '14 at 21:38
5

Ansible 1.1 added the recurse parameter to the file module, so all you need to do for your change ownership task is this:

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

This will make it more apparent when actually things change; using the shell or command modules will always return a changed status, even if nothing was actually changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.