你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?


当前回答

- file:
    path: /etc/some_directory
    state: directory
    mode: 0755
    owner: someone
    group: somegroup

这也是设置权限,所有者和组的方法。最后三个参数不是必须的。

其他回答

您可以使用以下方法创建:

最新版本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

在Ansible中创建目录的最简单方法。

name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory

OR

您希望给该目录赋予sudo特权。

name:如果your_directory不存在,创建该目录。 文件: 路径:/etc/your_directory 模式:“777”

您可以创建目录。使用

# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755

你也可以咨询 http://docs.ansible.com/ansible/file_module.html 有关目录和文件系统的进一步详细信息。

只需要为特定的分配设置执行任务的条件

- name: Creates directory
  file: path=/src/www state=directory
  when: ansible_distribution == 'Debian'