【问题标题】:How to fetch remote file only if file does not already exist in Ansible仅当 Ansible 中不存在文件时如何获取远程文件
【发布时间】:2020-01-21 15:57:01
【问题描述】:

在 Ansible 中,我想使用 Fetch 模块获取远程文件,前提是该文件在目标位置不存在。我怎么能这样做? Fetch 似乎没有现有的参数来检查这个。

我的任务:

- name: Fetch file from remote host
  fetch: 
    dest: /path/filename.{{groups['host'][0]}}.pub
    src: /path/filename.pub
    flat: yes
  delegate_to: "{{groups['host'][0]}}"

提前致谢!

【问题讨论】:

    标签: ansible fetch


    【解决方案1】:

    问:“获取模块,仅当目标文件不存在时。”

    答:fetch 模块是幂等的,只下载更改的文件。例如 playbook 的第一次运行

    - hosts: test_01
      tasks:
        - fetch:
            dest: /tmp/fetched
            src: /etc/passwd
            flat: yes
    

    给予

    TASK [fetch] *********************************************************************************************************************************
    changed: [test_01]
    
    PLAY RECAP ***********************************************************************************************************************************
    test_01                    : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
    

    重复运行相同的剧本给出

    TASK [fetch] *********************************************************************************************************************************
    ok: [test_01]
    
    PLAY RECAP ***********************************************************************************************************************************
    test_01                    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

    source 表明仅当远程和本地校验和不同时才会获取文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多