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


当前回答

使用文件模块创建一个目录,并使用命令“ansible-doc file”获取文件模块的详细信息

这里有一个选项“state”解释:

If directory, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions. If file, the file will NOT be created if it does not exist, see the [copy] or [template] module if you want that behavior. If link, the symbolic link will be created or changed. Use hard for hardlinks. If absent, directories will be recursively deleted, and files or symlinks will be unlinked. Note that file will not fail if the path does not exist as the state did not change. If touch (new in 1.4), an empty file will be created if the path does not exist, while an existing file or directory will receive updated file access and modification times (similar to the way touch works from the command line).

其他回答

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

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

如果你想在windows中创建一个目录:

- name: create folder in Windows
  win_file:
    path: C:\Temp\folder\subfolder
    state: directory

有关更多信息,请参阅win_file模块。

你可以使用陈述句

- 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的更多信息,您可以查看官方文档

您甚至可以扩展文件模块,甚至可以通过它设置所有者、组和权限。(参考: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

这样,如果这两个目录不存在,它将创建它们。