Ansible 版本:2.2.1.0
我在 Ansible 中将目录内容复制到目标主机时遇到问题。我的角色目录结构如下所示:
roles/server/
??? defaults
? ??? main.yml
??? files
? ??? Common-x86_64.repo
? ??? docker-ce-stable.repo
|
??? handlers
? ??? main.yml
??? meta
? ??? main.yml
??? README.md
??? tasks
? ??? main.yml
? ??? packages.yml
??? templates
??? tests
? ??? inventory
? ??? test.yml
??? vars
??? main.yml
Run Code Online (Sandbox Code Playgroud)
文件目录中有很多(大约 12 个)存储库,为了保持简短,我省略了大部分。我正在运行此代码packages.yml
- name: 1. Setup Repos
file:
src: files/
dest: /etc/yum.repos.d/
owner: root
group: root
mode: 0644
Run Code Online (Sandbox Code Playgroud)
即使任务没有错误地完成,也没有任何内容复制到/etc/yum.repos.d/目标机器上:
ls /etc/yum.repos.d/
CentOS-Base.repo CentOS-Media.repo CentOS-fasttrack.repo
CentOS-CR.repo CentOS-Sources.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
Run Code Online (Sandbox Code Playgroud)
我在文档页面上读到,以 a 结尾的源目录/会导致内容被递归复制。我究竟做错了什么?
为已接受的答案提供一些附加信息。
使用目录路径的递归复制有以下缺点:
--check标志--check --diff不会显示任何内容changed=0并且还可能影响远程主机上已存在的文件。似乎有一种更强大的方法来执行递归复制,即with_filetree与when
- name: "create-remote-dirs"
file:
path: /dest/dir/{{item.path}}
state: directory
mode: '0775'
with_filetree: sourceDir/
when: item.state == 'directory'
- name: "copy-files"
copy:
src: "{{item.src}}"
dest: /dest/dir/{{item.path}}
mode: '0744'
with_filetree: sourceDir/
# combinations of 'is' and 'is not' can be used below.
when: item.state == 'file'
and item.path is not search("excludedDir/*")
and item.path is not search("*.bak")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10206 次 |
| 最近记录: |