Ansible-创建多个文件夹(如果不存在) [英] Ansible - Create multiple folders if don't exist
本文介绍了Ansible-创建多个文件夹(如果不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标:
- 创建多个目录(如果不存在).
- 请勿更改现有文件夹的权限
当前剧本:
- 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
when
语句是错误的.
我看了提供的例子; http://docs.ansible.com/ansible/stat_module.html .但这仅适用于单个文件夹.
I looked at the example provided; http://docs.ansible.com/ansible/stat_module.html. But this only works for a single folder.
推荐答案
Ansible-创建多个文件夹而不更改以前存在的权限.
对我来说很好.希望这也对您有用.Ansible - Creating multiple folders without changing permissions of previously existing.
Working fine for me. Hope this works for you as well just try.---
- 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}}"
...
这篇关于Ansible-创建多个文件夹(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文