【问题标题】:With ansible.posix.synchronize, is there a way to a source folder that may not exist?使用 ansible.posix.synchronize,有没有办法找到可能不存在的源文件夹?
【发布时间】:2023-01-31 01:04:57
【问题描述】:
tasks:
    - name: sync folders
      loop: "{{ folder_list | list }}"
      ansible.posix.synchronize:
        src: "/path/folder/{{ item }}"
        dest: "/other_node/folders/"
        archive: false
        recursive: true
        perms: true
        checksum: true
        delete: true
  • 文件夹列表folder_list 在别处定义。
  • 我无法控制它,也无法更改它。我也不知道前面的文件夹列表,所以不能静态设置。
  • 它可能包含“此”机器上不存在的项目。

有没有办法让同步任务跳过这样的items?我找到了stat。看起来它可以用来检查文件或文件夹是否存在,但我不知道如何在任务中一起使用它,set_factsynchronize来完成这个任务。

我想做的是这样的:

遍历文件夹列表 > 如果源文件夹存在 > 将文件夹同步到目标。

PS:如果这属于ServerFault,请告诉我。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    控制器上的testing paths很简单。例如,给定树

    shell> tree /tmp/export/
    /tmp/export/
    ├── dir1
    │   ├── a
    │   └── b
    └── dir2
        └── c
    
    2 directories, 3 files
    

    下面的剧本跳过丢失的文件夹

    shell> cat pb.yml
    - hosts: test_11
    
      vars:
    
        folder_list:
          - /tmp/export/dir1
          - /tmp/export/dir2
          - /tmp/export/dir3
    
      tasks:
    
        - debug:
            msg: "synchronize {{ item }}"
          loop: "{{ folder_list }}"
          when: item is directory
    

    shell> ansible-playbook pb.yml 
    
    PLAY [test_11] ***********************************************************************************************
    
    TASK [debug] *************************************************************************************************
    ok: [test_11] => (item=/tmp/export/dir1) => 
      msg: synchronize /tmp/export/dir1
    ok: [test_11] => (item=/tmp/export/dir2) => 
      msg: synchronize /tmp/export/dir2
    skipping: [test_11] => (item=/tmp/export/dir3) 
    
    PLAY RECAP ***************************************************************************************************
    test_11: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

    你可以结合条件。例如,

          when: item is directory or item is link
    

    ,或者只是简单地测试存在性

          when: item is exists
    

    注意:模块stat 检查当前主机上的文件。您必须将此任务委派给本地主机如果你想在这里使用它。注册结果并在下面声明变量

      folder_exists: "{{ dict(folder_list_stat.results|
                              json_query('[].[item, stat.exists]')) }}"
    
        - block:
    
            - stat:
                path: "{{ item }}"
              loop: "{{ folder_list }}"
              register: folder_list_stat
        
            - debug:
                var: folder_list_stat
              when: debug|d(false)|bool
    
            - debug:
                var: folder_exists
              when: debug|d(false)|bool
    
          delegate_to: localhost
          run_once: true
    

    给出字典

      folder_exists:
        /tmp/export/dir1: true
        /tmp/export/dir2: true
        /tmp/export/dir3: false
    

    然后,条件是微不足道的。查看有关其他可用属性的调试文件夹列表统计并根据需要创建其他词典进行测试。

    用于测试的完整剧本示例

    - hosts: test_11
    
      vars:
    
        folder_list:
          - /tmp/export/dir1
          - /tmp/export/dir2
          - /tmp/export/dir3
    
        folder_exists: "{{ dict(folder_list_stat.results|
                                json_query('[].[item, stat.exists]')) }}"
        
      tasks:
    
        - block:
            - stat:
                path: "{{ item }}"
              loop: "{{ folder_list }}"
              register: folder_list_stat
        
            - debug:
                var: folder_list_stat
              when: debug|d(false)|bool
            - debug:
                var: folder_exists
              when: debug|d(false)|bool
          delegate_to: localhost
          run_once: true
    
        - debug:
            msg: "synchronize {{ item }}"
          loop: "{{ folder_list }}"
          when: folder_exists[item]
    

    【讨论】:

    • 太感谢了!我最终结合使用了您原来的“项目是目录”检查和完整的剧本(而不是 stat/json_query)。
    猜你喜欢
    • 1970-01-01
    • 2012-09-22
    • 2011-06-23
    • 2021-03-05
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多