Neil
Neil

Reputation: 3036

How to delete local directory with ansible

I have to make delete some directory and create them on local before copy to the remote. Is there anyway to delete and create locally?

Currently I'm using 'command'

command: rm -r directory

But warning shows as

Consider using file module with state=absent rather than running rm

Is there any options we can use for local folder changes?

Upvotes: 6

Views: 23514

Answers (3)

flxPeters
flxPeters

Reputation: 1542

You can use diffrent delegation methods or use the local_action:

- local_action: file path=directory state=absent

Upvotes: 17

Aven_Zerrock
Aven_Zerrock

Reputation: 25

Update:
Current Ansible (2.10) does not like - local_action: , instead use delegate_to:

- name: Remove directory 'dir1'
  file: 
    path: "path/to/dir1" 
    state: absent
  delegate_to: localhost

Upvotes: 1

Scott Rankin
Scott Rankin

Reputation: 443

If you're running this in a playbook, you can use a section of the playbook that uses a local connection to make changes on the command machine, then copies files to the remote:

---
- hosts: 127.0.0.1
  connection: local
  tasks:
    - name: Delete local directory
      file: path=/directory state=absent

- hosts: myhosts
  tasks:
      copy: src=/directory dest=/foo/directory

Upvotes: 2

Related Questions