Re: [ansible-project] Special variable group_names list items out of order

2022-11-03 Thread Vladimir Botka
On Thu, 3 Nov 2022 03:57:19 -0700 (PDT)
Goran Kuzmanović  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.


pgplg59gGHEsu.pgp
Description: OpenPGP digital signature


[ansible-project] Special variable group_names list items out of order

2022-11-03 Thread Goran Kuzmanović
Hello,

While searching for my use-case, I stumbled upon this issue 
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." However, this 
explanation doesn't tell me for what purpose (in my mind, I could sort the 
list if I needed it that way), while I would benefit of having it sorted 
depending on the level. Nevertheless, is there a workaround to get the list 
of groups on which you can rely, level-wise ?

Thanks,
Goran

-- 
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/2b9e1a49-7eda-44e8-a6ce-823ef8c9f244n%40googlegroups.com.