首页 > 解决方案 > Ansible如何比较ansible两个目录,本地目录

问题描述

[root@pp-private-cloud test]# ls /tmp/test1/
11.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

剧本

---
- hosts: localhost

  tasks:
  - name: test_copy_folder
    copy:
      src: /tmp/test1
      dest: /tmp/data
[root@pp-private-cloud test]# ansible-playbook site-test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

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

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [localhost]

TASK [test_copy_folder] ******************************************************************************************************************************************************************************************
ok: [localhost]

PLAY RECAP *******************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@pp-private-cloud test]# rm -rf /tmp/test1/9.txt

[root@pp-private-cloud test]# ansible-playbook site-test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

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

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [localhost]

TASK [test_copy_folder] ******************************************************************************************************************************************************************************************
ok: [localhost]

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

标签: ansible

解决方案


同步模块更适合您的要求。就像rsynctake--delete选项一样,我们可以在任务中启用它来删除源上不存在的任何目标文件。

例子:

  - name: test_copy_folder
    synchronize:
      src: /tmp/test1
      dest: /tmp/data
      delete: true

注意:您可能需要添加recursive: true,但它在没有设置的情况下对我有用。


推荐阅读