Ansible synchronise fails if parent directories are not already created
我在Ansible中有一个同步任务:
1 2 3 4 5 | --- - name: Copy over code - lib synchronize: src: ../lib/some/parent/directories/ dest: ~/project/lib/some/parent/directories/ |
这失败了,因为目的地缺少
1 2 3 4 5 6 7 8 9 10 11 | --- - set_fact: directory_lib_dest: ~/project/lib/some/parent/directories/ - name: Create directories file: path={{ item }} state=directory with_items: -"{{ directory_lib_dest }}" - name: Copy over code - lib synchronize: src: ../lib/some/parent/directories/ dest:"{{ directory_lib_dest }}" |
有没有更好的解决方案可以使用soley的ansible synchronize模块来完成,或者避免我使用
这就是
如果你要采用点斜线技巧的
如果您需要重新创建
在实际使用前进行测试!
1 2 3 4 5 | - synchronize: src: '{{ playbook_dir }}/./lib/some/parent/directories/' dest: ~/project/ rsync_opts: - '--relative' |
正如我所说,
摘自
To limit the amount of path information that is sent, you have a couple options: (1) With
a modern rsync on the sending side (beginning with 2.6.7), you can insert a dot and a slash into the
source path, like this:rsync -avR /foo/./bar/baz.c remote:/tmp/
非常感谢康斯坦丁的回答,我最终使用了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | --- - name: Set name of local and remote project directory set_fact: LOCAL_PROJECT_DIRECTORY: "{{ playbook_dir | dirname }}" REMOTE_PROJECT_DIRECTORY:"{{ ansible_env.HOME }}/project-remote-dir/" - name: Create remote project directory {{ REMOTE_PROJECT_DIRECTORY }} file: path={{ REMOTE_PROJECT_DIRECTORY }} state=directory - name: Copy over code synchronize: src:"{{ LOCAL_PROJECT_DIRECTORY }}/./{{ item }}" dest:"{{ REMOTE_PROJECT_DIRECTORY }}" rsync_opts: -"--relative" with_items: - lib/some/parent/directories/ - src/some/other/directories/ |
1 2 3 4 5 6 7 8 9 | myproject deploy playbook.yml roles etc... lib etc... src etc... |
这是双重的好处,因为它消除了重复的