Re: [ansible-project] Looking for alternative to set_fact + loop

2022-10-11 Thread Dick Visser
On Tue, 11 Oct 2022 at 17:57, Vladimir Botka  wrote:

> the declaration below
>
> discos: "{{ [defaults] +
> [defaults]|product(extra)|map('combine')|list }}"
>
> gives the expected list
>
>   discos:
>   - bar: true
> desc: standard
> foo: true
> name: default
>   - bar: true
> desc: dedicated
> foo: true
> name: foobar
>   - bar: true
> desc: discovery
> foo: true
> name: noli


Perfect!!!
V much appreciated

-- 
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/CAF8BbLbz0pUMDCsJfc0Htp_u_bhF%2Bs99k%2BMxdKw2Hpzmnzg%2BAw%40mail.gmail.com.


Re: [ansible-project] Looking for alternative to set_fact + loop

2022-10-11 Thread Vladimir Botka
On Tue, 11 Oct 2022 16:05:42 +0200
Dick Visser  wrote:

> list structure that is defaulted to a single (dict) item ...
> additional items use the keys of the first item as their default.

For example, given the default dictionary and the extra list

defaults:
  name: default
  desc: standard
  foo: true
  bar: true

extra:
  - name: foobar
desc: dedicated
  - name: noli
desc: discovery

the declaration below

discos: "{{ [defaults] +
[defaults]|product(extra)|map('combine')|list }}"

gives the expected list

  discos:
  - bar: true
desc: standard
foo: true
name: default
  - bar: true
desc: dedicated
foo: true
name: foobar
  - bar: true
desc: discovery
foo: true
name: noli

-- 
Vladimir Botka

-- 
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/20221011175709.189ee983%40gmail.com.


pgpSJiTRJybHe.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Looking for alternative to set_fact + loop

2022-10-11 Thread Brian Coca
I thought i had it:

vars:
  discos: "{{ discos_extra|map('combine', disco_default) }}"

But it is the wrong precedence, need to think about how to reverse that.


-- 
--
Brian Coca

-- 
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/CACVha7en5pbPuHWrRdtOdDyRt6e9nDK_3fPR-zRRWZ%2Bhn1jS%3Dw%40mail.gmail.com.


[ansible-project] Looking for alternative to set_fact + loop

2022-10-11 Thread Dick Visser
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.