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
文件目录中有很多(大约 12 个)存储库,为了简短起见,我省略了大部分。我在 packages.yml
- name: 1. Setup Repos
file:
src: files/
dest: /etc/yum.repos.d/
owner: root
group: root
mode: 0644
即使任务完成且没有错误,但没有任何内容复制到目标计算机上的 /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
我在文档页面上读到,以 /
结尾的源目录会导致内容被递归复制。我做错了什么?
最佳答案
为已接受的答案提供一些附加信息..
使用目录路径的递归复制有以下缺点:
- 您无法获取每个复制文件的更改状态信息
- 因此
--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")
关于Ansible 递归目录复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449852/