如何根据字典的值构建列表?
How can I build list from the values of a dictionary?
我有一个 AD 用户列表,正在尝试创建一个 distinguishedName-list
vars:
admin_users: "user1;user2"
tasks:
- set_fact:
admin: "{{ admin_users.split(';') }}"
- name: Search account
community.general.ldap_search:
<...>
loop: "{{ admin }}"
register: ldap_result
- name: Build DN-array
set_fact:
group: "{{ group | default([]) }} + {{ item.value | json_query(query) }}"
with_dict: "{{ ldap_result.results }}"
when: item.key == 'results'
vars:
query: "[*].distinguishedName"
- debug:
var: "{{ group }}"
并获得
"<class 'list'>": "VARIABLE IS NOT DEFINED!"
也试过
group: "{{ group | default([]) }} + [ {{ item.value | json_query(query) }} ]"
group: "{{ group | default([]) }} + {{ [ item.value | json_query(query) ] }}"
收到相同的消息
"<class 'list'>": "VARIABLE IS NOT DEFINED!"
和
group: "{{ group | default([]) }} + [ '{{ item.value | json_query(query) }}' ]"
然后我得到错误
FAILED! => {"msg": "template error while templating string: expected
token ',', got 'CN'. String: {{[] + [ '['CN=***']' ] + [ '['CN=***']' ]}}"}
有什么建议吗?
好的,没错,我只是需要使用这样的构造
<...>
group: "{{ group | default([]) }} + [ {{ item.value | json_query(query) }} ]"
<...>
- debug: msg="{{ group }}"
不是这个
- debug:
var: "{{ group_list_two }}"
正确输出
ok: [localhost] => {
"msg": [
[
"CN=***"
],
[
"CN=***"
]
]
}
我有一个 AD 用户列表,正在尝试创建一个 distinguishedName-list
vars:
admin_users: "user1;user2"
tasks:
- set_fact:
admin: "{{ admin_users.split(';') }}"
- name: Search account
community.general.ldap_search:
<...>
loop: "{{ admin }}"
register: ldap_result
- name: Build DN-array
set_fact:
group: "{{ group | default([]) }} + {{ item.value | json_query(query) }}"
with_dict: "{{ ldap_result.results }}"
when: item.key == 'results'
vars:
query: "[*].distinguishedName"
- debug:
var: "{{ group }}"
并获得
"<class 'list'>": "VARIABLE IS NOT DEFINED!"
也试过
group: "{{ group | default([]) }} + [ {{ item.value | json_query(query) }} ]"
group: "{{ group | default([]) }} + {{ [ item.value | json_query(query) ] }}"
收到相同的消息
"<class 'list'>": "VARIABLE IS NOT DEFINED!"
和
group: "{{ group | default([]) }} + [ '{{ item.value | json_query(query) }}' ]"
然后我得到错误
FAILED! => {"msg": "template error while templating string: expected
token ',', got 'CN'. String: {{[] + [ '['CN=***']' ] + [ '['CN=***']' ]}}"}
有什么建议吗?
好的,没错,我只是需要使用这样的构造
<...>
group: "{{ group | default([]) }} + [ {{ item.value | json_query(query) }} ]"
<...>
- debug: msg="{{ group }}"
不是这个
- debug:
var: "{{ group_list_two }}"
正确输出
ok: [localhost] => {
"msg": [
[
"CN=***"
],
[
"CN=***"
]
]
}