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

查看:33
本文介绍了使用 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/'

myfiles 文件夹存在于 dist 下,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.

如果由于某种原因你有问题,你可以看看 循环 Fileglobs.

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天全站免登陆