英文:
Ansible : How to copy everything from "Files" folders of a specific role
问题
- 我有一个 Ansible 角色,看起来像这样:
my-role
├─── files
│ my-file-one
│ my-file-two
│ my-file-...
│ my-file-n
└─── tasks
main.yml
在我的 main.yml 中,我有这个递归复制任务,并且我想复制所有文件,而无需手动列出它们:
- name: 复制所有文件
copy:
src: "{{ item }}"
dest: /dest/
with_items:
- ????
建议?
英文:
i ve an ansible role which looks like this :
my-role
├─── files
│ my-file-one
│ my-file-two
│ my-file-...
│ my-file-n
└─── tasks
main.yml
in my main.yml , i ve this recursive copy task ,
and i want to copy all files without the need of listing them manually :
- name: copy all files
copy:
src: "{{ item }}"
dest: /dest/
with_items:
- ????
Suggestions ??
答案1
得分: 7
Here is the translated content:
如果您的 files
目录是扁平的(即,您无需担心递归目录),您可以使用 with_fileglob
来获取文件列表:
---
- name: 复制所有文件
copy:
src: "{{ item }}"
dest: /dest/
with_fileglob: "files/*"
如果您需要递归复制,您不能使用 with_fileglob
,因为它只返回文件列表。您可以使用 find
模块来实现,如下所示:
---
- name: 列出文件
find:
paths: "{{ role_path }}/files/"
file_type: any
register: files
- name: 复制文件
copy:
src: "{{ item.path }}"
dest: /dest/
loop: "{{ files.files }}"
请注意,这是您提供的内容的翻译。如果您需要代码部分的答案,请告诉我。
英文:
If your files
directory is flat (i.e., you don't need to worry about recursing directories), you can just use with_fileglob
to get the list of files:
---
- name: copy all files
copy:
src: "{{ item }}"
dest: /dest/
with_fileglob: "files/*"
If you need a recursive copy, you can't use with_fileglob
because it only returns a list of files. You can use the find
module instead like this:
---
- name: list files
find:
paths: "{{ role_path }}/files/"
file_type: any
register: files
- name: copy files
copy:
src: "{{ item.path }}"
dest: /dest/
loop: "{{ files.files }}"
答案2
得分: 4
从copy模块文档中。
本地文件的路径,用于复制到远程服务器。
可以是绝对路径或相对路径。
如果路径是一个目录,则会递归复制。在这种情况下,如果路径以"/"结尾,则仅复制该目录的内部内容到目标位置。否则,如果路径不以"/"结尾,则目录本身和所有内容都会被复制。
如果您将文件放入files
目录的子目录中(例如my_files
),那么您可以将my_files/
作为copy
模块的src
参数。
my-role
├─── files
| └───my_files
│ my-file-one
│ my-file-two
│ my-file-...
│ my-file-n
└─── tasks
main.yml
- name: 复制所有文件
copy:
src: my_files/
dest: /dest/
英文:
From the copy module docs.
>Local path to a file to copy to the remote server.
This can be absolute or relative.
If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all contents is copied.
If you place the files into a subdirectory of the files
directory (e.g. my_files
) then you can use my_files/
as the src
argument to the copy
module.
my-role
├─── files
| └───my_files
│ my-file-one
│ my-file-two
│ my-file-...
│ my-file-n
└─── tasks
main.yml
- name: copy all files
copy:
src: my_files/
dest: /dest/
答案3
得分: 4
Using ./
作为src
参数对我有用。它会递归复制角色files
目录中的所有文件和目录到目标位置。
这个解决方案不需要在复制之前列出文件。
英文:
Using ./
as the src
argument worked for me. It copies recursively all files and directories from the role files
directory to target.
This solution does not require to list the files with another task before copying them.
---
- name: Copy all role files to target
copy:
src: ./
dest: <destination_dir>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论