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


当前回答

目录只能使用文件模块创建,因为目录只是一个文件。

# create a directory if it doesn't exist
- file:
    path: /etc/some_directory
    state: directory
    mode: 0755
    owner: foo
    group: foo

其他回答

您可以创建目录。使用

# 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'

要检查目录是否存在,然后运行一些任务(例如创建目录),请使用以下命令

- 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

创建目录

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

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