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


当前回答

创建目录

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

其他回答

—name:创建目录 ansible.builtin.file: 路径:/etc/some_directory 状态:目录 模式:“0755”

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

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

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

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

最新版本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"模块,其中有许多参数可以传递给新创建的目录,如所有者、组、位置、模式等.....

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

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

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

大家下午好。

我愿在此与大家分享以下几点。

   - 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

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