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

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

在Ansible劇本的with_items循環中注冊變量

在Ansible劇本的with_items循環中注冊變量

GCT1015 2019-11-19 15:10:03
我有一個不同名稱的字典,例如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}}"


查看完整回答
反對 回復 2019-11-19
  • 1 回答
  • 0 關注
  • 804 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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