使用ansible复制本地文件(如果存在) [英] Copy local file if exists, using ansible

查看:878
本文介绍了使用ansible复制本地文件(如果存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我们使用ansible创建一个部署服务器集群. 我要实现的任务之一是将本地文件复制到远程主机,前提是该文件位于本地. 现在,我正在尝试使用此方法解决此问题

I'm working in a project, and we use ansible to create a deploy a cluster of servers. One of the tasks that I've to implement, is to copy a local file to the remote host, only if that file exists locally. Now I'm trying to solve this problem using this

- hosts: 127.0.0.1 
  connection: local
  tasks:
    - name: copy local filetocopy.zip to remote if exists
    - shell: if [[ -f "../filetocopy.zip" ]]; then /bin/true; else /bin/false; fi;
      register: result    
    - copy: src=../filetocopy.zip dest=/tmp/filetocopy.zip
      when: result|success

此操作失败,并显示以下消息: 错误:任务将本地filetocopy.zip复制到远程文件(如果存在)"中缺少操作"或本地操作"属性

Bu this is failing with the following message: ERROR: 'action' or 'local_action' attribute missing in task "copy local filetocopy.zip to remote if exists"

如果使用命令任务,我尝试创建此文件. 我已经尝试使用local_action创建此任务,但是无法使其工作. 我发现的所有示例都没有将shell视为local_action的外壳,只有命令示例,并且除了命令之外,没有其他任何示例. 有没有办法使用Ansible完成这项任务?

I've tried to create this if with command task. I've already tried to create this task with a local_action, but I couldn't make it work. All samples that I've found, doesn't consider a shell into local_action, there are only samples of command, and neither of them have anything else then a command. Is there a way to do this task using ansible?

推荐答案

将您的第一步更改为以下内容

Change your first step into the following on

- name: copy local filetocopy.zip to remote if exists
  local_action: stat path="../filetocopy.zip"
  register: result    

这篇关于使用ansible复制本地文件(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆