你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?
当前回答
这里有一个更简单的方法。
—name:创建dir mkdir -p dir dir/a dir/b
其他回答
在Ansible中创建目录的最简单方法。
name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory
OR
您希望给该目录赋予sudo特权。
name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory 模式:“777”
您需要文件模块。要创建目录,您需要指定选项状态:directory:
- name: Creates directory
file:
path: /src/www
state: directory
你可以在https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html上看到其他选项
在这种情况下,您需要使用文件模块。下面的剧本,你可以使用你的参考。
---
- 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
您甚至可以扩展文件模块,甚至可以通过它设置所有者、组和权限。(参考:Ansible文件文档)
- name: Creates directory
file:
path: /src/www
state: directory
owner: www-data
group: www-data
mode: 0775
甚至,你可以递归地创建目录:
- name: Creates directory
file:
path: /src/www
state: directory
owner: www-data
group: www-data
mode: 0775
recurse: yes
这样,如果这两个目录不存在,它将创建它们。
enter code here
- name: creating directory in ansible
file:
path: /src/www
state: directory
owner: foo
你可以参考可靠的文档