Ansible Create Directory If Not Exists-DecodingDevOps

Ansible Create Directory If Not Exists:-DecodingDevOps

To create a directory in ansible we use ansible file module with tag name state as directory. To check the directory is existed or not, we use ansible stat module. In the following example i will show you how to create a directory in ansible if not exists. And i am creating jboss directory if not existed.

[root@ip-10-0-0-20 ~]# cat abc.yml
---
- hosts: localhost
  gather_facts: no

  tasks:
  - name: Ansible check directory.
    stat:
     path: /project/devops/jboss
    register: my_folder

  - name: "echo if directory already existed"
    debug:
      msg: "the jboss directory is already existed"
    when: my_folder.stat.exists

  - name: "Ansible Create directory if not exists"
    file:
     path: /project/devops/jboss
     state: directory
     mode: 0755
     group: root
     owner: root
    when: my_folder.stat.exists == false

[root@ip-10-0-0-20 ~]#

 

In the first task we are storing the path(/project/devops/jboss) results in my_folder variable with Ansible register module. Second task is displaying the message with debug module if the jboss directory existed. Ansible debug module is used to print the messages.

In the third task we are creating the jboss directory if not existed.

Ansible Create Directory If Not Exists:

[root@ip-10-0-0-20 ~]# ansible-playbook abc.yml


PLAY [localhost] *******************************************************************************************************************************************************************************

TASK [Ansible check directory exists example.] *************************************************************************************************************************************************
ok: [localhost]

TASK [echo if directory already existed] *******************************************************************************************************************************************************
skipping: [localhost]

TASK [Ansible Create directory if not exists] **************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

[root@ip-10-0-0-20 ~]#

You can see here the second task is skipped since we don't have jboss directory in /project/devops

and third task is changed, so jboss directory is created in the third task.

Ansible Create Directory if Already Existed:

[root@ip-10-0-0-20 ~]# ansible-playbook abc.yml
 

PLAY [localhost] ***************************************************************

TASK [Ansible check directory exists example.] *********************************
ok: [localhost]

TASK [echo if directory already existed] ***************************************
ok: [localhost] => {
    "msg": "the jbosss directory is already existed"
}

TASK [Ansible Create directory if not exists] **********************************
skipping: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

The second task is displaying the message that jboss directory is already existed. Third task is skipped.

 

Leave a Reply

Your email address will not be published. Required fields are marked *