On Thu, 3 Nov 2022 03:57:19 -0700 (PDT)
Goran Kuzmanović <gorank....@gmail.com> wrote:

> https://github.com/ansible/ansible/issues/73208 when the explanation 
> provided was "The group_names variable is purposefully alphanumerically 
> sorted by name, and not sorted by definition order." ... get the list 
> of groups on which you can rely, level-wise ?

Use *ansible-inventory* and parse the inventory on your own. Given
the example from the issue

shell> cat hosts
all:
  children:
    NonprodAppDefault:
      children:
        CHG0240322:
          vars:
            controller_env: test
          hosts:
            hdqqclansiap001:
            hdqqclbcbwtb001:


1) Declare the variables

    inventory: "{{ out.stdout|from_yaml }}"
    my_groups: {}
    my_groups_0: "{{ inventory.all.children }}"

2) Parse the inventory

    - command: ansible-inventory --list --yaml
      register: out

gives

  inventory:
    all:
      children:
        NonprodAppDefault:
          children:
            CHG0240322:
              hosts:
                hdqqclansiap001:
                  controller_env: test
                hdqqclbcbwtb001:
                  controller_env: test
        ungrouped: {}

3) Create the dictionary of your groups

    - set_fact:
        my_groups: "{{ my_groups|
                       combine({item: _groups}) }}"
      loop: "{{ my_groups_0.keys()|list }}"
      vars:
        _groups: "{{ (my_groups_0[item].children|
                      default({})).keys()|list }}"


gives (probably what you want)

  my_groups:
    NonprodAppDefault:
    - CHG0240322
    ungrouped: []


-- 
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/20221103155539.740d0389%40gmail.com.

Attachment: pgplg59gGHEsu.pgp
Description: OpenPGP digital signature

Reply via email to