我有一個不同名稱的字典,例如vars: images: - foo - bar不,我想簽出存儲庫,然后僅在源發生更改時才構建docker映像。由于獲取源代碼和構建圖像對于所有項目都是相同的,除了我創建任務的名稱,with_items: images 并嘗試將結果注冊到:register: "{{ item }}"并嘗試了register: "src_{{ item }}"然后我嘗試了以下條件when: "{{ item }}|changed"和when: "{{ src_item }}|changed"這總是導致 fatal: [piggy] => |changed expects a dictionary那么,如何根據我遍歷的列表將操作結果正確保存在變量名稱中?更新:我希望有這樣的東西:- hosts: all vars: images: - foo - bar tasks: - name: get src git: repo: [email protected]/repo.git dest: /tmp/repo register: "{{ item }}_src" with_items: images - name: build image shell: "docker build -t repo ." args: chdir: /tmp/repo when: "{{ item }}_src"|changed register: "{{ item }}_image" with_items: images - name: push image shell: "docker push repo" when: "{{ item }}_image"|changed with_items: images
1 回答

慕尼黑5688855
TA貢獻1848條經驗 獲得超2個贊
那么,如何根據我遍歷的列表將操作結果正確保存在變量名稱中?
不用了 為with_items具有不同格式的任務注冊的變量,它們包含所有項目的結果。
- hosts: localhost
gather_facts: no
vars:
images:
- foo
- bar
tasks:
- shell: "echo result-{{item}}"
register: "r"
with_items: "{{ images }}"
- debug: var=r
- debug: msg="item.item={{item.item}}, item.stdout={{item.stdout}}, item.changed={{item.changed}}"
with_items: "{{r.results}}"
- debug: msg="Gets printed only if this item changed - {{item}}"
when: item.changed == true
with_items: "{{r.results}}"
- 1 回答
- 0 關注
- 804 瀏覽
添加回答
舉報
0/150
提交
取消