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


当前回答

您可以创建目录。使用

# 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:创建dir mkdir -p dir dir/a dir/b

我看到很多Playbooks的例子,我想提到Adhoc命令的例子。

$ansible -i inventory -m file -a "path=/tmp/ directory state=directory(我们可以用touch代替directory来创建文件)

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

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

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

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

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

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

- 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