你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?
当前回答
你可以使用陈述句
- name: webfolder - Creates web folder
file: path=/srv/www state=directory owner=www-data group=www-data mode=0775`
其他回答
你可以用以下方法之一来做这件事:
例1:如果父目录已经存在:
- name: Create a new directory www at given path
ansible.builtin.file:
path: /srv/www/
state: directory
mode: '0755'
例2:父目录不存在:
- name: Create a new directory www at given path recursively
ansible.builtin.file:
path: /srv/www/
state: directory
mode: '0755'
recurse: yes
在示例2中,如果两个目录都不存在,它将递归地创建它们。
有关file_module的更多信息,您可以查看官方文档
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: someone
group: somegroup
这也是设置权限,所有者和组的方法。最后三个参数不是必须的。
在这种情况下,您需要使用文件模块。下面的剧本,你可以使用你的参考。
---
- hosts: <Your target host group>
name: play1
tasks:
- name: Create Directory
files:
path=/srv/www/
owner=<Intended User>
mode=<Intended permission, e.g.: 0750>
state=directory
您需要文件模块。要创建目录,您需要指定选项状态:directory:
- name: Creates directory
file:
path: /src/www
state: directory
你可以在https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html上看到其他选项
在Ansible中创建目录的最简单方法。
name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory
OR
您希望给该目录赋予sudo特权。
name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory 模式:“777”