亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Ansible playbook中的with_items循環中注冊變量

在Ansible playbook中的with_items循環中注冊變量

Git
PIPIONE 2019-09-03 19:27:04
我有一本不同名字的字典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}}"


查看完整回答
反對 回復 2019-09-03
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

最后兩個任務表明。如果你的意思是plz詳細說明的話。運行它,看看輸出

查看完整回答
反對 回復 2019-09-03
  • 2 回答
  • 0 關注
  • 1892 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號