你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?
当前回答
我看到很多Playbooks的例子,我想提到Adhoc命令的例子。
$ansible -i inventory -m file -a "path=/tmp/ directory state=directory(我们可以用touch代替directory来创建文件)
其他回答
您可以使用以下方法创建:
最新版本2<
- name: Create Folder
file:
path: /srv/www/
owner: user
group: user
mode: 0755
state: directory
旧版本
- name: Create Folder
file:
path=/srv/www/
owner=user
group=user
mode=0755
state=directory
参考- http://docs.ansible.com/ansible/file_module.html
另外,在很多情况下,您需要创建多个目录,因此使用循环而不是为每个目录创建单独的任务是一个好主意。
- name: creates multiple directories in one task
file:
path: "{{ item }}"
state: directory
loop:
- /srv/www
- /dir/foo
- /dir/bar
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: someone
group: somegroup
这也是设置权限,所有者和组的方法。最后三个参数不是必须的。
您需要文件模块。要创建目录,您需要指定选项状态:directory:
- name: Creates directory
file:
path: /src/www
state: directory
你可以在https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html上看到其他选项
要检查目录是否存在,然后运行一些任务(例如创建目录),请使用以下命令
- name: Check if output directory exists
stat:
path: /path/to/output
register: output_folder
- name: Create output directory if not exists
file:
path: /path/to/output
state: directory
owner: user
group: user
mode: 0775
when: output_folder.stat.exists == false