你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?
当前回答
---
- hosts: all
connection: local
tasks:
- name: Creates directory
file: path=/src/www state=directory
上面的playbook将在/src路径下创建www目录。
在运行上面的剧本之前。请确保您的ansible主机连接应该设置,
“本地主机ansible_connection=本地”
应该出现在/etc/ansible/hosts中
如需更多信息,请与我联系。
其他回答
您可以使用以下方法创建:
最新版本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: <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
您可以创建目录。使用
# 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:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory
OR
您希望给该目录赋予sudo特权。
name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory 模式:“777”
要检查目录是否存在,然后运行一些任务(例如创建目录),请使用以下命令
- 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