你如何创建一个目录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: Validar Directorio
     stat:
       path: /tmp/Sabana
     register: sabana_directorio
   
   - debug:
       msg: "Existe"
     when: sabana_directorio.stat.isdir == sabana_directorio.stat.isdir

   - name: Crear el directorio si no existe.
     file:
       path: /tmp/Sabana
       state: directory
     when: sabana_directorio.stat.exists == false

使用它可以在创建目录之前验证该目录是否存在

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

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

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

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

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

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

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

使用文件模块创建一个目录,并使用命令“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).