Question:
Goal:
- Create multiple directories if they don’t exist.
- Don’t change permissions of existing folder
Current playbook:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
- 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 loop: "{{ stat.results }}" # with_items: "{{ stat.results }}" # for older versions of Ansible # 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.
Answer:
Ansible – Creating multiple folders without changing permissions of previously existing.
Working fine for me. Hope this works for you as well just try.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
--- - name: "Creating multiple by checking folders" hosts: your_host_name tasks: - block: - name: "Checking folders" stat: path: "{{item}}" register: folder_stats with_items: - ["/var/www/f1","/var/www/f2","/var/www/f3","/var/www/f4"] - name: "Creating multiple folders without disturbing previous permissions" file: path: "{{item.item}}" state: directory mode: 0755 group: root owner: root when: item.stat.exists == false loop: - "{{folder_stats.results}}" ... |