问题描述
我在一个项目中工作,我们使用 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 复制到远程(如果存在)"中缺少action"或local_action"属性
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复制本地文件(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!