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

Arrays 带同步数组的Ansible命令循环,arrays,docker,ansible,ansible-inventory,Arrays,Docker,Ansible,Ansible Inventory,我需要遍历2个同步数组来运行bash命令 清单1:a、b、c 清单2:13 我想获得 a1 b2 c3 而不是 a1 a2 a3 b1 b2 c1 c2 c3 我试着用“和你在一起”,但没有成功 这是我的任务 - name: create volume shell: docker volume create {{ item.0 }} {{ item.1 }} when: volume_exists|failed run_once: true with_togheter:

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

清单1:a、b、c

清单2:13

我想获得

a1 b2 c3

而不是

a1 a2 a3 b1 b2 c1 c2 c3

我试着用“和你在一起”,但没有成功

这是我的任务

- 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"}
失败!=>{“msg”:"该任务包含一个带有未定义变量的选项。错误是:“item”未定义\n\n错误似乎出现在“/home/rgi/ansible/roles/promotedocker/tasks/main.yml”:第267行第3列,但可能\n位于文件的其他位置,具体取决于语法问题。\n\n出现问题的行是:\n\n\n-name:create volume\n^here\n\n异常类型:\n异常:“项”未定义“}
怎么做


谢谢

这是一个简单的演示,可以解决您的问题。我使用了3个输入列表,只是为了显示它可以与两个以上的输入列表一起工作(还因为您的“abc,123”示例在我的大脑中植入了一首歌,这一天剩下的时间……)

注意:在您的评论之后,您可以通过将
循环
替换为
与列表

演示剧本

---
- 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 }}"
结果是:

PLAY [localhost] ***************************************************************************************************************************************************************

TASK [Love receipe] ************************************************************************************************************************************************************
ok: [localhost] => (item=['a', 1, 'do']) => {
    "msg": "a, it's easy as 1, as simple as do"
}
ok: [localhost] => (item=['b', 2, 're']) => {
    "msg": "b, it's easy as 2, as simple as re"
}
ok: [localhost] => (item=['c', 3, 'mi']) => {
    "msg": "c, it's easy as 3, as simple as mi"
}

PLAY RECAP *********************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

肯定还有其他错误消息。
pgwatch master
不是有效的组名。Ansible中没有筛选器
失败
。这是一个测试。任务也应该失败。正确的循环指令是
with_together
而不是
with_together
。您好@VladimirBotka。问题是“中的打字错误。”和你在一起" !!!! 谢谢我会解决所有其他的问题谢谢。目前,我必须使用版本为2.4的ansible服务器,因此我不能使用loop,但我将编写一个并行任务,以备将来使用。谢谢zip是在ansible 2.3中引入的,在这种情况下,您可以将
循环
替换为
和_item
。因此,这应该不是问题。因为zip结果是一个列表列表,并且
with_items
执行隐式第一级展平,所以在ansible 2.4中使用的正确循环运算符是
with_list
。见我的编辑上面。