我有一本不同名字的字典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
2 回答

慕蓋茨4494581
TA貢獻1850條經驗 獲得超11個贊
那么如何根據迭代的列表正確保存變量名中的操作結果呢?
你不需要。為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}}"
- 2 回答
- 0 關注
- 1892 瀏覽
添加回答
舉報
0/150
提交
取消