带有同步数组的Ansible命令循环

问题描述 投票:0回答:1

我需要遍历2个同步数组才能运行bash命令

list1:a b clist2:1 2 3

我想获得a1 b2 c3并不是a1 a2 a3 b1 b2 b2 c1 c2 c3

我正在尝试“ with_togheter”,但没有成功

这是我的任务

- name: create volume
  shell: docker volume create {{ item.0 }} {{ item.1 }}
  when: volume_exists|failed
  run_once: true
  with_togheter:
    - "{{ volumename }}"
    - "{{ volumeopts }}"
  tags:
    - dockervolumenested

这是库存

[pgwatch-master]
host.domain

[pgwatch-master:vars]
volumename=["pgw-master-grafana","pgw-master-influxdb","pgw-master-persistent-config","pgw-master-postgresql"]
volumeopts=["--opt o=size=10m --opt device=local --opt type=local","--opt o=size=15000m --opt device=local --opt type=local","--opt o=size=1m --opt device=local --opt type=local","--opt o=size=200m --opt device=local --opt type=local"]

这是错误:

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/home/rgi/ansible/roles/promotedocker/tasks/main.yml': line 267, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: create volume\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'item' is undefined"}

如何做?

谢谢

arrays docker ansible ansible-inventory
1个回答
0
投票

这是zip filter的简单演示,它将解决您的问题。我使用了3个输入列表,只是为了表明它可以使用两个以上的输入(也是因为您的“ abc,123”示例在一天的剩余时间里在我的大脑中植入了一首歌。...]

演示剧本

zip

结果:

---
- hosts: localhost
  gather_facts: false

  vars:
    list1: [a, b, c]
    list2: [1, 2, 3]
    list3: [do, re, mi]

  tasks:
    - name: Love receipe
      debug:
        msg: "{{ item.0 }}, it's easy as {{ item.1 }}, as simple as {{ item.2 }}" 
      loop: "{{ list1 | zip(list2, list3) | list }}"
© www.soinside.com 2019 - 2024. All rights reserved.