Ansible命令从远程服务器提取文件并防止该文件已经存在

时间:2019-01-12 18:57:37

标签: ansible

使用Ansible来托管服务器(172.19.113.104)我要复制文件( ansibletest MariaDB-client-5.1.67-122.el5.x86_64.rpm >)从远程服务器(172.19.113.87),但是如果文件已经存在,则不应复制。

我尝试如下,但抛出错误:

- 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   

注意:文件存在并获得许可

1 个答案:

答案 0 :(得分: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