我只想强制复制projectA
以下剧本中的项目的文件。
- name: Populate /usr/local/mywork/ binaries where necessary
copy:
src: "output/here/usr/local/mywork/{{item}}"
dest: "/usr/local/mywork/{{ item }}"
force: '{{ item == "projectA" }}'
with_items: "{{ server_files }}"
我补充道force: '{{ item == "projectA" }}'
。
但这不起作用。某些部分不正确?
答案1
给定树
shell> tree /tmp/test
/tmp/test
├── files
│ ├── projectA
│ ├── projectB
│ └── projectC
└── mywork
├── projectA
├── projectB
└── projectC
2 directories, 6 files
文件内容不同
shell> cat /tmp/test/files/*
1
1
1
shell> cat /tmp/test/mywork/*
0
0
0
选项力量:
如果为 true,则当内容与源文件不同时,远程文件将被替换。
如果为 false,则仅当目标不存在时才会传输文件。
下面的剧本
shell> cat pb.yml
- hosts: localhost
vars:
server_files: [projectA, projectB, projectC]
tasks:
- copy:
src: "/tmp/test/files/{{ item }}"
dest: "/tmp/test/mywork/{{ item }}"
force: "{{ item == 'projectA' }}"
loop: "{{ server_files }}"
按预期工作并仅复制文件项目A。正在运行该剧--检查--差异模式给出
shell> ansible-playbook pb.yml -CD
PLAY [localhost] *****************************************************************************
TASK [copy] **********************************************************************************
--- before: /tmp/test/mywork/projectA
+++ after: /tmp/test/files/projectA
@@ -1 +1 @@
-0
+1
changed: [localhost] => (item=projectA)
ok: [localhost] => (item=projectB)
ok: [localhost] => (item=projectC)
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如果文件内容不变,则不会发生任何变化项目A是一样的
shell> cat /tmp/test/files/projectA
1
shell> cat /tmp/test/mywork/projectA
1
shell> ansible-playbook pb.yml -CD
PLAY [localhost] *****************************************************************************
TASK [copy] **********************************************************************************
ok: [localhost] => (item=projectA)
ok: [localhost] => (item=projectB)
ok: [localhost] => (item=projectC)
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0