Ansible Create Directory If Not Exists

Ansible Create Directory If Not Exists:

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.

 

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:

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:

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

 

  • Ansible Create Directory If Not Exists
  • Create Directory Ansible
  • Ansible directory create
  • Ansible Create Directory