为了账号安全,请及时绑定邮箱和手机立即绑定

在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: git@foobar.com/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 关注
  • 1820 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信