具有同步数组的 Ansible 命令循环
Ansible command loop with synched array
我需要通过 2 个同步数组迭代到 运行 bash 命令
list1: a b c
列表 2: 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"}
怎么做?
谢谢
这是 zip
filter 的简单演示,可以解决您的问题。我使用了 3 个输入列表只是为了表明它可以处理两个以上的输入列表(还因为你的 "abc, 123" 示例在我今天余下的时间里植入了一首歌....)
注意:根据您的评论,您可以通过将 loop
替换为 with_list
在 ansible 2.4 中获得与下面完全相同的结果
演示手册
---
- 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
我需要通过 2 个同步数组迭代到 运行 bash 命令
list1: a b c
列表 2: 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"}
怎么做?
谢谢
这是 zip
filter 的简单演示,可以解决您的问题。我使用了 3 个输入列表只是为了表明它可以处理两个以上的输入列表(还因为你的 "abc, 123" 示例在我今天余下的时间里植入了一首歌....)
注意:根据您的评论,您可以通过将 loop
替换为 with_list
演示手册
---
- 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