Ansible - 如何将多个不同的列表合并到任务循环中使用
Ansible - How to combine multiple disparate lists into to use in tasks loop
我有如下 3 个列表
users:
- "['user1', 'user2']"
permissions:
- "[['Read'], ['Read', 'Write']]"
topics:
- "[['Topic1', 'Topic2'], ['Topic3', 'Topic4', 'Topic5']]"
我想组合成类似这样的结构
combined_lists:
[
{'user1', 'Read','Topic1'},
{'user1', 'Read','Topic2'},
{'user2', 'Read','Topic3'},
{'user2', 'Read','Topic4'},
{'user2', 'Read','Topic5'},
{'user2', 'Write','Topic3'},
{'user2', 'Write','Topic4'},
{'user2', 'Write','Topic5'}
]
这样我就可以在下面的任务中使用它
- name: Generate ACL command and run
shell: |
echo " kafka-acls --bootstrap-server broker1:9092 --command-config /kafka/configs/client.properties --add --allow-principal User:{{ item.0 }} --operation {{ item.1 }} --topic {{ item.2 }}"
loop:
"{{ combined_lists }}"
有人可以建议我应该使用或查看哪个模块来组合列表,这些列表是可用于生成和执行 kafka-acl 命令的格式的列表列表?我试图查看笛卡尔模块,但无法使其以这种方式工作。
感谢任何有关要查看哪些模块以实现结果的指示。
谢谢
你需要处理几个 set_facts 循环:
---
- hosts: localhost
gather_facts: false
vars:
users: [ "user1", "user2" ]
permissions: [ "Read", [ "Read", "Write"]]
topics: [[ "Topic1", "Topic2"], ["Topic3", "Topic4", "Topic5"]]
tasks:
- name: SET_FACTS | Combine users and permissions
vars:
user: "{{ users[my_idx] }}"
permission: "{% if item | type_debug == 'list' %}{{ item }}{% else %}{{ [item] }}{% endif %}"
set_fact:
combined_up: "{{ combined_up | default([]) + [[user] | product(permission) | list] }}"
loop: "{{ permissions }}"
loop_control:
index_var: my_idx
- name: SET_FACTS | Combine users, permissions with topics
vars:
userpermission: "{{ combined_up[my_idx] }}"
topic: "{% if item | type_debug == 'list' %}{{ item }}{% else %}{{ [item] }}{% endif %}"
set_fact:
combined_upt: "{{ combined_upt | default([]) + userpermission | product(topic) | list }}"
loop: "{{ topics }}"
loop_control:
index_var: my_idx
- name: SET_FACTS | Create list of dictionary objects
set_fact:
combined_listd: "{{ combined_listd | default([]) + [{ 'user': item[0][0], 'permission': item[0][1], 'topic': item[1] }] }}"
loop: "{{ combined_upt }}"
- name: SET_FACTS | Create list of lists
set_fact:
combined_lists: "{{ combined_lists | default([]) + [[ item[0][0], item[0][1], item[1] ]] }}"
loop: "{{ combined_upt }}"
- name: DEBUG | list of dictionary objects
debug:
msg: "{{ item.user }} - {{ item.permission }} - {{ item.topic }}"
loop: "{{ combined_listd }}"
- name: DEBUG | list of lists
debug:
msg: "{{ item[0] }} - {{ item[1] }} - {{ item[2] }}"
loop: "{{ combined_lists }}"
这里是输出:
PLAY [localhost] **********************************************************************************
TASK [SET_FACTS | Combine users and permissions] **************************************************
ok: [localhost] => (item=Read)
ok: [localhost] => (item=['Read', 'Write'])
TASK [SET_FACTS | Combine users, permissions with topics] *****************************************
ok: [localhost] => (item=['Topic1', 'Topic2'])
ok: [localhost] => (item=['Topic3', 'Topic4', 'Topic5'])
TASK [SET_FACTS | Create list of dictionary objects] **********************************************
ok: [localhost] => (item=[['user1', 'Read'], 'Topic1'])
ok: [localhost] => (item=[['user1', 'Read'], 'Topic2'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic5'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic5'])
TASK [SET_FACTS | Create list of lists] ***********************************************************
ok: [localhost] => (item=[['user1', 'Read'], 'Topic1'])
ok: [localhost] => (item=[['user1', 'Read'], 'Topic2'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic5'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic5'])
TASK [DEBUG | list of dictionary objects] *********************************************************
ok: [localhost] => (item={'user': 'user1', 'permission': 'Read', 'topic': 'Topic1'}) => {
"msg": "user1 - Read - Topic1"
}
ok: [localhost] => (item={'user': 'user1', 'permission': 'Read', 'topic': 'Topic2'}) => {
"msg": "user1 - Read - Topic2"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Read', 'topic': 'Topic3'}) => {
"msg": "user2 - Read - Topic3"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Read', 'topic': 'Topic4'}) => {
"msg": "user2 - Read - Topic4"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Read', 'topic': 'Topic5'}) => {
"msg": "user2 - Read - Topic5"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Write', 'topic': 'Topic3'}) => {
"msg": "user2 - Write - Topic3"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Write', 'topic': 'Topic4'}) => {
"msg": "user2 - Write - Topic4"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Write', 'topic': 'Topic5'}) => {
"msg": "user2 - Write - Topic5"
}
TASK [DEBUG | list of lists] **********************************************************************
ok: [localhost] => (item=['user1', 'Read', 'Topic1']) => {
"msg": "user1 - Read - Topic1"
}
ok: [localhost] => (item=['user1', 'Read', 'Topic2']) => {
"msg": "user1 - Read - Topic2"
}
ok: [localhost] => (item=['user2', 'Read', 'Topic3']) => {
"msg": "user2 - Read - Topic3"
}
ok: [localhost] => (item=['user2', 'Read', 'Topic4']) => {
"msg": "user2 - Read - Topic4"
}
ok: [localhost] => (item=['user2', 'Read', 'Topic5']) => {
"msg": "user2 - Read - Topic5"
}
ok: [localhost] => (item=['user2', 'Write', 'Topic3']) => {
"msg": "user2 - Write - Topic3"
}
ok: [localhost] => (item=['user2', 'Write', 'Topic4']) => {
"msg": "user2 - Write - Topic4"
}
ok: [localhost] => (item=['user2', 'Write', 'Topic5']) => {
"msg": "user2 - Write - Topic5"
}
PLAY RECAP ****************************************************************************************
我有如下 3 个列表
users:
- "['user1', 'user2']"
permissions:
- "[['Read'], ['Read', 'Write']]"
topics:
- "[['Topic1', 'Topic2'], ['Topic3', 'Topic4', 'Topic5']]"
我想组合成类似这样的结构
combined_lists:
[
{'user1', 'Read','Topic1'},
{'user1', 'Read','Topic2'},
{'user2', 'Read','Topic3'},
{'user2', 'Read','Topic4'},
{'user2', 'Read','Topic5'},
{'user2', 'Write','Topic3'},
{'user2', 'Write','Topic4'},
{'user2', 'Write','Topic5'}
]
这样我就可以在下面的任务中使用它
- name: Generate ACL command and run
shell: |
echo " kafka-acls --bootstrap-server broker1:9092 --command-config /kafka/configs/client.properties --add --allow-principal User:{{ item.0 }} --operation {{ item.1 }} --topic {{ item.2 }}"
loop:
"{{ combined_lists }}"
有人可以建议我应该使用或查看哪个模块来组合列表,这些列表是可用于生成和执行 kafka-acl 命令的格式的列表列表?我试图查看笛卡尔模块,但无法使其以这种方式工作。
感谢任何有关要查看哪些模块以实现结果的指示。
谢谢
你需要处理几个 set_facts 循环:
---
- hosts: localhost
gather_facts: false
vars:
users: [ "user1", "user2" ]
permissions: [ "Read", [ "Read", "Write"]]
topics: [[ "Topic1", "Topic2"], ["Topic3", "Topic4", "Topic5"]]
tasks:
- name: SET_FACTS | Combine users and permissions
vars:
user: "{{ users[my_idx] }}"
permission: "{% if item | type_debug == 'list' %}{{ item }}{% else %}{{ [item] }}{% endif %}"
set_fact:
combined_up: "{{ combined_up | default([]) + [[user] | product(permission) | list] }}"
loop: "{{ permissions }}"
loop_control:
index_var: my_idx
- name: SET_FACTS | Combine users, permissions with topics
vars:
userpermission: "{{ combined_up[my_idx] }}"
topic: "{% if item | type_debug == 'list' %}{{ item }}{% else %}{{ [item] }}{% endif %}"
set_fact:
combined_upt: "{{ combined_upt | default([]) + userpermission | product(topic) | list }}"
loop: "{{ topics }}"
loop_control:
index_var: my_idx
- name: SET_FACTS | Create list of dictionary objects
set_fact:
combined_listd: "{{ combined_listd | default([]) + [{ 'user': item[0][0], 'permission': item[0][1], 'topic': item[1] }] }}"
loop: "{{ combined_upt }}"
- name: SET_FACTS | Create list of lists
set_fact:
combined_lists: "{{ combined_lists | default([]) + [[ item[0][0], item[0][1], item[1] ]] }}"
loop: "{{ combined_upt }}"
- name: DEBUG | list of dictionary objects
debug:
msg: "{{ item.user }} - {{ item.permission }} - {{ item.topic }}"
loop: "{{ combined_listd }}"
- name: DEBUG | list of lists
debug:
msg: "{{ item[0] }} - {{ item[1] }} - {{ item[2] }}"
loop: "{{ combined_lists }}"
这里是输出:
PLAY [localhost] **********************************************************************************
TASK [SET_FACTS | Combine users and permissions] **************************************************
ok: [localhost] => (item=Read)
ok: [localhost] => (item=['Read', 'Write'])
TASK [SET_FACTS | Combine users, permissions with topics] *****************************************
ok: [localhost] => (item=['Topic1', 'Topic2'])
ok: [localhost] => (item=['Topic3', 'Topic4', 'Topic5'])
TASK [SET_FACTS | Create list of dictionary objects] **********************************************
ok: [localhost] => (item=[['user1', 'Read'], 'Topic1'])
ok: [localhost] => (item=[['user1', 'Read'], 'Topic2'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic5'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic5'])
TASK [SET_FACTS | Create list of lists] ***********************************************************
ok: [localhost] => (item=[['user1', 'Read'], 'Topic1'])
ok: [localhost] => (item=[['user1', 'Read'], 'Topic2'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Read'], 'Topic5'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic3'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic4'])
ok: [localhost] => (item=[['user2', 'Write'], 'Topic5'])
TASK [DEBUG | list of dictionary objects] *********************************************************
ok: [localhost] => (item={'user': 'user1', 'permission': 'Read', 'topic': 'Topic1'}) => {
"msg": "user1 - Read - Topic1"
}
ok: [localhost] => (item={'user': 'user1', 'permission': 'Read', 'topic': 'Topic2'}) => {
"msg": "user1 - Read - Topic2"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Read', 'topic': 'Topic3'}) => {
"msg": "user2 - Read - Topic3"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Read', 'topic': 'Topic4'}) => {
"msg": "user2 - Read - Topic4"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Read', 'topic': 'Topic5'}) => {
"msg": "user2 - Read - Topic5"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Write', 'topic': 'Topic3'}) => {
"msg": "user2 - Write - Topic3"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Write', 'topic': 'Topic4'}) => {
"msg": "user2 - Write - Topic4"
}
ok: [localhost] => (item={'user': 'user2', 'permission': 'Write', 'topic': 'Topic5'}) => {
"msg": "user2 - Write - Topic5"
}
TASK [DEBUG | list of lists] **********************************************************************
ok: [localhost] => (item=['user1', 'Read', 'Topic1']) => {
"msg": "user1 - Read - Topic1"
}
ok: [localhost] => (item=['user1', 'Read', 'Topic2']) => {
"msg": "user1 - Read - Topic2"
}
ok: [localhost] => (item=['user2', 'Read', 'Topic3']) => {
"msg": "user2 - Read - Topic3"
}
ok: [localhost] => (item=['user2', 'Read', 'Topic4']) => {
"msg": "user2 - Read - Topic4"
}
ok: [localhost] => (item=['user2', 'Read', 'Topic5']) => {
"msg": "user2 - Read - Topic5"
}
ok: [localhost] => (item=['user2', 'Write', 'Topic3']) => {
"msg": "user2 - Write - Topic3"
}
ok: [localhost] => (item=['user2', 'Write', 'Topic4']) => {
"msg": "user2 - Write - Topic4"
}
ok: [localhost] => (item=['user2', 'Write', 'Topic5']) => {
"msg": "user2 - Write - Topic5"
}
PLAY RECAP ****************************************************************************************