В моем первом примере я начал с объединения массивов, для которых вышеупомянутое решение (с поисковыми переменными) работает довольно хорошо. Продолжая работу над моим проектом, мне тоже нужно объединить подэлементы типизированных словарей.
- name: My test play
hosts : localhost
gather_facts: false
vars:
types:
- rs
- gg
- notexists
# backend
be_components: {}
solr_cores: {}
backend_config_rs:
be_components :
rs-document: { dbaccess: True, version: latest, mvn_id: rs-document }
rs-attachments:{ dbaccess: True, version: latest, mvn_id: rs-attachments }
cores:
rs: { name: rs_core, dir: /var/solr/data/jurisdict }
backend_config_gg:
be_components :
gg-document: { dbaccess: True, version: latest, mvn_id: gg-document }
gg-importer: { dbaccess: True, version: latest, mvn_id: gg-importer }
cores:
rs: { name: gg_core, dir: /var/solr/data/law }
tasks:
# Static demo of what I currently do
- name: "dyn-config | combine backend variables with RS backend config"
set_fact:
be_components: "{{ be_components | combine ( backend_config_rs.be_components ) }}"
solr_cores: "{{ solr_cores | combine ( backend_config_rs.cores ) }}"
when: "backend_config_rs is defined"
- name: "dyn-config | combine backend variables with GG backend config"
set_fact:
be_components: "{{ be_components | combine ( backend_config_gg.be_components ) }}"
solr_cores: "{{ solr_cores | combine ( backend_config_gg.cores ) }}"
when: "backend_config_gg is defined"
- debug: var=be_components
- debug: var=solr_cores
Но затем я пытаюсь сделать это динамически, я обнаружил, что доступ к поиску vars не возвращает подэлементы dicts.
- name: "try with lookup"
vars:
listname: "backend_config_{{ item }}"
set_fact:
be_components: "{{ be_components | combine (lookup('vars', listname.components)) }}"
loop: "{{ types }}"
loop_control:
label: "{{ listname }}"
when: vars[listname] is defined
Я получил
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'components'\n
Есть ли способ получить доступ к подэлементам диктов через переменные поиска?