Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 :)

share|improve this question
Never knew if it allows $item ...I thought it was {{ item }} – Arun Sangal Dec 9 '15 at 1:07
up vote 12 down vote accepted

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}
share|improve this answer
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
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
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

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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