How to create a directory using Ansible

后端 未结 22 1483
暗喜
暗喜 2020-12-22 16:44

How do you create a directory www at /srv on a Debian-based system using an Ansible playbook?

相关标签:
22条回答
  • 2020-12-22 17:09

    To check if directory exists and then run some task (e.g. create directory) use the following

    - 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
    
    0 讨论(0)
  • 2020-12-22 17:10

    You can even extend the file module and even set the owner,group & permission through it. (Ref: Ansible file documentation)

    - name: Creates directory
      file:
        path: /src/www
        state: directory
        owner: www-data
        group: www-data
        mode: 0775
    

    Even, you can create the directories recursively:

    - name: Creates directory
      file:
        path: /src/www
        state: directory
        owner: www-data
        group: www-data
        mode: 0775
        recurse: yes
    

    This way, it will create both directories, if they didn't exist.

    0 讨论(0)
  • 2020-12-22 17:10

    Hello good afternoon team.

    I share the following with you.

       - 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
    

    With which you can validate if the directory exists before creating it

    0 讨论(0)
  • 2020-12-22 17:11

    to create directory

    ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"
    
    0 讨论(0)
  • 2020-12-22 17:11

    you can use the "file" module in this case, there are so many arguments that you can pass for a newly created directory like the owner, group, location, mode and so on.....

    please refer to this document for the detailed explanation on the file module...

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

    remember this module is not just for creating the directory !!!

    0 讨论(0)
  • 2020-12-22 17:15

    I see lots of Playbooks examples and I would like to mention the Adhoc commands example.

    $ansible -i inventory -m file -a "path=/tmp/direcory state=directory ( instead of directory we can mention touch to create files)

    0 讨论(0)
提交回复
热议问题