How to remove directory in ansible?

How to remove directory in ansible?

In ansible, lets see how to remove or delete any required directory.

file module is used to delete any directory or file in ansible.

Required Parameters:

path parameter is used to set the required directory or file.

state parameter is set as absent then all the files and directories deleted recursively.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
	- name: initialize directory path
  	set_fact:
    	dir: "test/testing"

	- name: "Clean up {{ dir }} folder"
  	become: true
  	become_user: root
  	file:
    	path: "{{ dir }}"
    	state: absent

	- name: Print the msg
  	debug:
    	msg: "Cleaned {{ dir }} direcotry."

Output:

$ ansible-playbook cleanup_directory.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

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

TASK [initialize directory path] ********************************************************************************************************************************************
ok: [localhost]

TASK [Clean up test/testing folder] *****************************************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Cleaned test/testing direcotry."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=3	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0

test/testing directory exists then deletes the directory recursively.

Output:

$ ls test
No testing directory is displayed.



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^