ansible - 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
- 删除
9.txt
,remote dir no diff,如何保证两个目录的一致性
[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
解决方案
同步模块更适合您的要求。就像rsync
take--delete
选项一样,我们可以在任务中启用它来删除源上不存在的任何目标文件。
例子:
- name: test_copy_folder
synchronize:
src: /tmp/test1
dest: /tmp/data
delete: true
注意:您可能需要添加recursive: true
,但它在没有设置的情况下对我有用。
推荐阅读
- javascript - 箭头位置问题 - 范围输入
- mysql - 如何重新创建问题:Mysql2::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT)
- angular - 无法读取未定义角度 6 的属性“图像”
- azure-devops - 我们可以分别授予 VSTS 工作项和任务的权限吗?
- python - 如何打印随机名称列表中的第一个字母
- java - 如何以 MapReduce 格式在一行中打印一些标记?
- php - Laravel 419 会话在部署到共享主机后过期
- outlook - Outlook 加载项:可固定任务窗格不刷新 Outlook 中的视图
- javascript - 如何使用 jQuery 添加分页?
- abap - 将 gl 帐户数据发布到 bapi_incominginvoice_create1 后引发异常