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


当前回答

创建目录

ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"

其他回答

您需要文件模块。要创建目录,您需要指定选项状态:directory:

- name: Creates directory
  file:
    path: /src/www
    state: directory

你可以在https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html上看到其他选项

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

这里有一个更简单的方法。

—name:创建dir mkdir -p dir dir/a dir/b

在本例中,您可以使用"file"模块,其中有许多参数可以传递给新创建的目录,如所有者、组、位置、模式等.....

关于文件模块的详细说明请参考本文档…

https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

记住这个模块不仅仅是用来创建目录的!!