6

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?

Neil
  • 2,714
  • 11
  • 29
  • 45

3 Answers3

17

You can use diffrent delegation methods or use the local_action:

- local_action: file path=directory state=absent
flxPeters
  • 1,476
  • 12
  • 21
2

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
Scott Rankin
  • 443
  • 3
  • 7
  • Do I need to make a new local connection just for changing the local file? – Neil Feb 23 '17 at 22:02
  • That's how ansible knows to run it locally vs on the remote machines. You can also use local_action as mentioned below. The local connection is good if you have a bunch of things to do locally. – Scott Rankin Feb 23 '17 at 22:04
1

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
Yunnosch
  • 26,130
  • 9
  • 42
  • 54