Hi
I have a recurring challenge in our deployment, whereby we have a list
structure that is defaulted to a single (dict) item.
The challenge is that I would like to extend that in a way so that
additional items use the keys of the first item as their default.
I have gotten this work as in the example playbook:
---
- name: List with default items test
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: show list of default discoveries (just one)
debug: var=discos
- name: Override and construct custom sets
set_fact:
discos: "{{ discos | default([disco_default]) | union(
[disco_default | combine(item)] ) }}"
loop: "{{ discos_extra }}"
loop_control:
label: "{{ item.name }}"
- name: show list with extra discoveries appended
debug: var=discos
vars:
discos: "{{ [disco_default] }}"
disco_default:
name: default
desc: Standard discovery interface
foo_service_enable: true
bar_service_enable: true
baz_service_enable: true
purge_expired_entities: true
dy_fqdn: dy.foo.bar
dx_fqdn: dx.foo.bar
frontend_fqdn: frontend.foo.bar
metadata:
- https://www.bar.org/saml2
- https://api.bar.org/v2
entities_remove: []
js_tls_port: 8443
# Example definition where we define extra items, with keys based
off the first (default) item
discos_extra:
- name: foobar
desc: Dedicated discovery interface for the Foobar customer
foo_service_enable: false
dy_fqdn: custom.dy.foo.bar
js_tls_port: 12345
- name: noli
desc: Discovery interface without LinkedIn
dy_fqdn: clean.dy.foobar.org
entities_remove:
- https://linkedin.com/evil/saml2
The above works as I want, there are now 2 extra dicts, which have
'inherited' the keys from the first dict:
PLAY [List with default items test]
**
TASK [show list of default discoveries (just one)]
***
ok: [localhost] =>
discos:
- bar_service_enable: true
baz_service_enable: true
desc: Standard discovery interface
dx_fqdn: dx.foo.bar
dy_fqdn: dy.foo.bar
entities_remove: []
foo_service_enable: true
frontend_fqdn: frontend.foo.bar
js_tls_port: 8443
metadata:
- https://www.bar.org/saml2
- https://api.bar.org/v2
name: default
purge_expired_entities: true
TASK [Override and construct custom sets]
ok: [localhost] => (item=foobar)
ok: [localhost] => (item=noli)
TASK [show list with extra discoveries appended]
*
ok: [localhost] =>
discos:
- bar_service_enable: true
baz_service_enable: true
desc: Standard discovery interface
dx_fqdn: dx.foo.bar
dy_fqdn: dy.foo.bar
entities_remove: []
foo_service_enable: true
frontend_fqdn: frontend.foo.bar
js_tls_port: 8443
metadata:
- https://www.bar.org/saml2
- https://api.bar.org/v2
name: default
purge_expired_entities: true
- bar_service_enable: true
baz_service_enable: true
desc: Dedicated discovery interface for the Foobar customer
dx_fqdn: dx.foo.bar
dy_fqdn: custom.dy.foo.bar
entities_remove: []
foo_service_enable: false
frontend_fqdn: frontend.foo.bar
js_tls_port: 12345
metadata:
- https://www.bar.org/saml2
- https://api.bar.org/v2
name: foobar
purge_expired_entities: true
- bar_service_enable: true
baz_service_enable: true
desc: Discovery interface without LinkedIn
dx_fqdn: dx.foo.bar
dy_fqdn: clean.dy.foobar.org
entities_remove:
- https://linkedin.com/evil/saml2
foo_service_enable: true
frontend_fqdn: frontend.foo.bar
js_tls_port: 8443
metadata:
- https://www.bar.org/saml2
- https://api.bar.org/v2
name: noli
purge_expired_entities: true
But, I was wondering if there is an alternative to the set_fact + loop task.
Ideally something that does not include a task, perhaps with
product,map,json_query, or something else. I can't wrap my head around
it, hoping someone else can...?
thx!!
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAF8BbLae4WfrotdOSP8zT98LcB99NHYfeU6z9r1Jt0-MM80r4Q%40mail.gmail.com.