On Tue, 12 Jul 2022 02:18:57 -0700 (PDT)
Softmax23 <[email protected]> wrote:

For example, the playbook below

shell> cat pb.yml
- hosts: localhost
  vars:
    servers:
      srv01: a
      srv02: a
      srv03: a
      srv04: b
      srv05: b
      srv06: b
      srv07: c
      srv08: c
      srv09: c
    arr: "{{ servers|dict2items|
                     groupby('value')|
                     map(attribute=1)|
                     map('map', attribute='key')|
                     list }}"
    list_of_servers: "{{ tarr|flatten }}"
  tasks:
    - set_fact:
        tarr: "{{ tarr|d(arr.0)|zip(item)|map('flatten') }}"
      loop: "{{ arr[1:] }}"
    - debug:
        var: list_of_servers[:5]|to_yaml

1) Creates the matrix

  arr:
    - [srv01, srv02, srv03]
    - [srv04, srv05, srv06]
    - [srv07, srv08, srv09]

2) Transpose the matrix

  tarr:
    - [srv01, srv04, srv07]
    - [srv02, srv05, srv08]
    - [srv03, srv06, srv09]

3) Get the slice

  list_of_servers[:5]: [srv01, srv04, srv07, srv02, srv05]

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20220712115932.3ac6a25b%40gmail.com.

Attachment: pgpOknimSvIKx.pgp
Description: OpenPGP digital signature

Reply via email to