如何使用ansible删除本地目录

How to delete local directory with ansible

我必须删除一些目录并在复制到远程之前在本地创建它们。有没有办法在本地删除和创建?

目前我正在使用'command'

command: rm -r directory

但是警告显示为

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

我们可以使用任何选项来更改本地文件夹吗?

您可以使用不同的 delegation methods 或使用 local_action

- local_action: file path=directory state=absent

如果你运行在剧本中这样做,你可以使用剧本的一部分,使用本地连接在命令机器上进行更改,然后将文件复制到远程:

---
- 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

更新:
当前的 Ansible (2.10) 不喜欢 - local_action: ,而是使用 delegate_to:

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