你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?
当前回答
在这种情况下,您需要使用文件模块。下面的剧本,你可以使用你的参考。
---
- 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
其他回答
另外,在很多情况下,您需要创建多个目录,因此使用循环而不是为每个目录创建单独的任务是一个好主意。
- name: creates multiple directories in one task
file:
path: "{{ item }}"
state: directory
loop:
- /srv/www
- /dir/foo
- /dir/bar
您可以创建目录。使用
# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755
你也可以咨询 http://docs.ansible.com/ansible/file_module.html 有关目录和文件系统的进一步详细信息。
您甚至可以扩展文件模块,甚至可以通过它设置所有者、组和权限。(参考: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
这样,如果这两个目录不存在,它将创建它们。
您可以使用以下方法创建:
最新版本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
---
- hosts: all
connection: local
tasks:
- name: Creates directory
file: path=/src/www state=directory
上面的playbook将在/src路径下创建www目录。
在运行上面的剧本之前。请确保您的ansible主机连接应该设置,
“本地主机ansible_connection=本地”
应该出现在/etc/ansible/hosts中
如需更多信息,请与我联系。