使用Ansible复制文件夹内容 [英] Copy folder content with Ansible

查看:670
本文介绍了使用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屋!

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