Ansible to check particular directory exists, if not create it

Viewed 7550

I am trying to find whether particular director exists in ansible, if not, create the one..The problem i am facing here is that the below code should not create a /var/tmp/directory, but it creates while running this.. ( the below code is not for creating a directory i think)

   tasks:
     - name: Checking /var/tmp/ansible directory exists
       stat: path=/var/tmp/ansible
       register: status

can anyone please help me to fix this issue..

1 Answers

You should use the file module:

- name: Create a directory if it does not exist
  file:
    path: /var/tmp/ansible
    state: directory
    mode: '0755'

There is no need to use stat to check whether the directory does not exist first, unless you plan to do something else with the result of that test. The file module ensures that the file is in the desired state after running the task.

Related