我正在一个项目中工作,我们使用 ansible 来创建部署服务器集群。
我必须实现的任务之一是将本地文件复制到远程主机,前提是该文件本地存在。
现在我正在尝试用这个来解决这个问题
- 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”属性
我尝试使用命令任务创建此 if 。
我已经尝试使用 local_action 创建此任务,但无法使其工作。
我发现的所有示例都没有考虑 local_action 中的 shell,只有命令示例,并且除了命令之外没有其他任何内容。
有没有办法使用ansible来完成这个任务?
更全面的答案:
如果你想检查是否存在local在执行某些任务之前创建文件,以下是完整的片段:
- name: get file stat to be able to perform a check in the following task
local_action: stat path=/path/to/file
register: file
- name: copy file if it exists
copy: src=/path/to/file dest=/destination/path
when: file.stat.exists
如果你想检查是否存在remote在执行某些任务之前文件,这是要走的路:
- name: get file stat to be able to perform check in the following task
stat: path=/path/to/file
register: file
- name: copy file if it exists
copy: src=/path/to/file dest=/destination/path
when: file.stat.exists
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)