Goal:
- Create multiple directories if they don't exist.
- Don't change permissions of existing folder
Current playbook:
- name: stat directories if they exist
stat:
path: "{{ item }}"
with_items:
- /data/directory
- /data/another
register: myvar
- debug: var=myvar.results
- name: create directory if they don't exist
file:
path: "{{ item.invocation.module_args.path }}"
state: directory
owner: root
group: root
mode: 0775
with_items: "{{ stat.results }}"
# when: myvar.results.stat.exists == false
The when
statement is wrong.
I looked at the example provided; http://docs.ansible.com/ansible/stat_module.html. But this only works for a single folder.
My answer may not work in every case, but if you don't supply an option, ansible won't change the current values. I've used this for NFS mounts where permissions vary on the NFS servers.
Using Ansible modules, you don't need to check if something exist or not, you just describe the desired state, so:
Ansible - Creating multiple folders without changing permissions of previously existing.
Working fine for me. Hope this works for you as well just try.