Create Directory in Ansible

Directory  is very necessary for linux operation.You can create Directory in linux system using shell through command.But when we are going to create directory  large no of systems one after another  at a time we utilize  Ansible because it is extremely  easier to implement large no of systems at a same time. 

We are going to implement node1  machine though ansible.

In this tutorial we learn how to create directory in ansible,how to create multiples directories in Ansible,how to create local directory in ansible,how to change permission of a directory in ansible and how to change permission of a directory recursively. 



    Create  Directory in Ansible

     You can create a directory using ansible file module.Here we have to defined two arguments one is path and other is state.Normally we create a directory using mkdir command. When we are going to create a directory using ansible  we set the state parameters to ‘directory’  .


    ---
    - hosts: node1
      tasks:
      - name: ansible create directory example
        file:
          path: /opt/testdir
          state: directory
     
    create directory in ansible
    create directory in ansible

    Create multiple directories in Ansible

    We can create multiple directories running ansible playbook using  with_items module.
    Let’s try an example
    We are going to create testdir1,testdir2,testdir3  under /opt
    ---
    - hosts: all
      tasks:
      - name: create multiple directories
        file:
          path: "{{ item }}"
          state: directory
        with_items:
          - '/opt/testdir1'
          - '/opt/testdir2'
          - '/opt/testdir3'



    Create local directory in Ansible

    Local_action statement is used in ansible when we are going to create a directory on the local machine as well as remote machine simultaneously .Directoy is created on the local machine before creating the remote maching when we execute the playbook.
    ---
    - hosts: all
      tasks:
      - name:  create local directory in ansible
        local_action:
          module: file
          path: /opt/localdir
          state: directory


    Change  Permissions of a Directory in Ansible

    In the above example we have created  a directory with default permission .Now we want to change the directory defaults permission.So, We have to focused one arguments that is mode .
    Now we are going to change the default permission of testdir directory which is located at /opt .
    We can use the octal form or symbolic form to define the mode agument.
    ---
    - hosts: node1
      tasks:
      - name: change permission of directory in ansible
        file:
          path: /opt/testdir
          state: directory
          mode: 0777


    Change  Permissions of Directory recursively

    The above example we have changed only permission of a directory from its default permission.Now we are going to change the change the permission of this directory recursively that means change the pemission of the directory as well as change the permission of those files and directories which is inside the parent directory.

    ---
    - hosts: node1
      tasks:
      - name: change recursive permission of directory in ansible
        file:
          path: /opt/testdir
          state: directory
          mode: 0777
          recurse: yes

     Delete Directory in Ansible

    We can delete a directory in Ansible to set the state value to ‘absent’.

    ---
    - hosts: node1
      tasks:
      - name: delete directory in ansible
        file:
          path: /opt/testdir
          state: absent

    If this article is helpful to know about create directory in ansible please share and subscribe this article. 

    SHARE

    Admin

    • Image
    • Image
    • Image
    • Image
    • Image
      Blogger Comment
      Facebook Comment

    2 comments: