使用Ansible复制文件夹内容 [英] Copy folder content with Ansible
问题描述
出于某些奇怪的原因,我在执行一个简单的任务时遇到了麻烦,该任务将文件夹myfiles
的内容(其中的文件很少)复制到dist/myfiles
位置.任务如下所示:
For some weird reasons I'm having troubles with a simple task which is copying a content of the folder myfiles
(few files in there) to the dist/myfiles
location. Task looks like this:
name: Deploy config files like there is no tomorrow
copy:
src: "{{ item }}"
dest: "/home/{{ ansible_user_id }}/dist/{{ item }}"
with_items:
- 'config'
- 'myfiles/'
在dist
下存在
myfiles
文件夹,并且config
文件被复制到dist
文件夹.
myfiles
folder exist under the dist
and config
file is copied to the dist
folder.
这在Ansible中是可能的,还是我应该分别复制每个文件?我做错了吗?
Is this possible in Ansible or I should copy each file separately? Am I doing it completely wrong?
推荐答案
您的任务会同时复制两个文件:Debian和CentOS目标上的config
文件和myfiles
.
Your task copies both: the config
file and the myfiles
on Debian and CentOS targets properly.
If for some reason you have a problem, you might have a look at Looping over Fileglobs.
您需要将任务分成两部分,第二项看起来像:
You need to split the task into two, with the second one looking like:
- name: Deploy multiple config files
copy:
src: "{{ item }}"
dest: "/home/{{ ansible_user_id }}/dist/myfiles/{{ item | basename }}"
with_fileglob:
- /path/to/myfiles/*
对于递归副本,请在SeverFault上检查此问题
For a recursive copy, check this question on SeverFault
或者,您可以使用 synchronize
模块,但要特别注意使用become
时.请参阅有关SuperUser的问题.
Alternatively, you could use the synchronize
module, but pay special attention when using become
. See this question on SuperUser.
这篇关于使用Ansible复制文件夹内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!