Ansible命令,用于从远程服务器拉取文件并防止文件已存在

Ansible命令,用于从远程服务器拉取文件并防止文件已存在,ansible,Ansible,使用Ansible,我想从远程服务器(172.19.113.87)复制文件(ansibletest&MariaDB-client-5.1.67-122.el5.x86_64.rpm),但如果文件已经存在,就不应该复制 我尝试了以下方法,但出现了错误: - hosts: webservers vars: ip: 172.19.113.87 tasks: - name: this is to pull local_action: shell 'ls /opt/ansible

使用Ansible,我想从远程服务器(172.19.113.87)复制文件(ansibletest&MariaDB-client-5.1.67-122.el5.x86_64.rpm),但如果文件已经存在,就不应该复制

我尝试了以下方法,但出现了错误:

- hosts: webservers
  vars:
   ip: 172.19.113.87
  tasks:
  - name: this is to pull
    local_action: shell 'ls /opt/ansibletest'
    register: result

  - name: ts2
    synchronize:  src={{ item }} dest=/opt/ mode=pull
    with_items:
    - "/opt/ansibletest"
    - "/opt/MariaDB-client-5.1.67-122.el5.x86_64.rpm"
    when: result.shell.exists == true









[root@rbtstaging ansible]# ansible-playbook fetch.yml

PLAY [webservers] ************************************************************************************************************************************************

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

TASK [this is to pull] *******************************************************************************************************************************************
changed: [172.19.113.87]

TASK [ts2] *******************************************************************************************************************************************************
fatal: [172.19.113.87]: FAILED! => {"msg": "The conditional check 'result.stat.exists == True' failed. The error was: error while evaluating conditional (result.stat.exists == True): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/RND/sudhir/ansible/fetch.yml': line 9, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: ts2\n    ^ here\n"}
    to retry, use: --limit @/RND/sudhir/ansible/fetch.retry

PLAY RECAP *******************************************************************************************************************************************************
172.19.113.87              : ok=2    changed=1    unreachable=0    failed=1   
注意:具有权限的文件存在

您可以通过本地“stat”操作预先执行获取(从远程服务器获取文件-复制将文件发送到删除服务器),并检查本地文件是否存在

local_action:
  module: stat
  path: /path/to/local/file
register: local_file
become: no

fetch:
  src: /path/to/remote/file
  dest: /path/to/local/file
  flat: yes
when: local_file.stat.exists == False

它不工作,默认情况下,获取模块,如果文件已经存在,并且其中的数据与远程服务器的同一文件中的数据相同,则文件不会得到修改,如果远程服务器中的文件发生更改,则我再次运行playbook,在这种情况下,它将替换主机服务器中的文件作为其修改。。。。。。。。。。。。。我的要求是,如果它在复制时发现文件,那么它应该复制,即使远程服务器中的文件已修改。很抱歉,我无法解析:-(您也切换到了shell命令,我的示例使用状态模块-为什么?