Re: [ansible-project] Re: I want to show the group variables, looping over all groups

2023-02-21 Thread Brian Coca
ansible-inventory has an --export option, this is only thing that
keeps 'group variables' as such. Ansible internally always flattens
variables to the host and groups are only a way to label hosts and to
mass assign variables, they are not a first class entity. So the
solutions above ONLY work when either variables are only defined in a
specific group or hosts are not members of more than one group that
defines the variable.


-- 
--
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/CACVha7ccp%3D7QxQuCqo1PduQ1xJAzLAENYS02vXhXKoM3JhaFUw%40mail.gmail.com.


[ansible-project] Re: I want to show the group variables, looping over all groups

2023-02-15 Thread Todd Lewis
Last week I said, "If somebody knows how to do it I'd love to see your 
solution." Well, I found the solution hiding in plain sight in 
https://docs.ansible.com/ansible/latest/playbook_guide/complex_data_manipulation.html#id15
 
and it boils down to this one-liner:

*q('vars', *q('varnames', '^group_comment_'))*

That asterisk isn't a typo; it's Python argument list unpacking 
. 
I had not appreciated how that could be used in Jinja expressions, but 
there it is.

On Wednesday, February 8, 2023 at 12:01:22 PM UTC-5 Todd Lewis wrote:
I see what you want, but it isn't possible. From each host's perspective, 
the `group_comment` variable will have the value as set in whichever one of 
that host's groups where the group name sorts last, as that's the last one 
loaded. So it is consistent, just not a solution to your problem.

You can work around it if you want to go to the trouble. Say you have 
groups named gr1, gr2, gr3, etc. Within each group, put that group's 
comment in a variable called `group_comment_`, i.e. 
`group_comment_gr1`, `group_comment_gr2`, `group_comment_gr3`, etc.

Then do something like the following to create a `group_comments` list for 
each host.

- name: Create a list of group_comment_* variable names
  ansible.builtin.set_fact:
# Two different ways to do it.
group_comment_names_a: "{{ ['group_comment_'] | 
product(vars.group_names) | map('join') | flatten }}"
group_comment_names_b: "{{ query('ansible.builtin.varnames', 
'^group_comment_.+') }}"

- name: Join group_comment_* into a list
  ansible.builtin.set_fact:
group_comments: |
{% set gclist = [] %}
{% for gcn in group_comment_names_a %}
{% set _ = gclist.append(query('ansible.builtin.vars', gcn)) %}
{% endfor %}{{ gclist | flatten }}

I tried lots of different ways to invoke `ansible.builtin.vars` on a list 
using "normal" jinja pipelines before resorting to the old-school for loop 
above. If somebody knows how to do it I'd love to see your solution.

Anyway, you end up with each host having a list containing each comment 
from each of its groups. But I somehow doubt that solves your problem 
either. It's still a host-centric view of your group comments.
On Wednesday, February 8, 2023 at 3:21:42 AM UTC-5 
norbert...@klamann-software.de wrote:
Hello all,
i use Ansible to document and manage a small network of computers and other 
devices. So I bulit an inventory with several groups and hosts and defined 
a bunch of variables  on each level to describe what i want to achieve,  
Each host and each group has for example a variable named `host_comment` or 
`group_comment` respectively.

This is very useful on the host level, but i want to show the group 
variables separately in a loop over all groups.

I can show a group variable via `hostvars[host].group_comment`but because 
every hosts belongs to several groups i cannot control, whicht name is 
displayed.

This template
```
{% for group in groups  if  group != 'ungrouped' %}
  {% for host in groups[group] %}
  {% if loop.first %}{% set hvh = hostvars[host] %}
  {{ group }} {{ hvh.group_comment }}
  {% endif %}
  {% endfor %}
{% endfor %}
```

gives 
```
  all Nicht per Ansible erreichbare Maschinen, aber sie sollen durch 
(Meta-) Variablen doukmentiert werden
  lxc_container Alle Hosts in FM 174 (soweit Ansible-fähig)
  fm174 Alle Hosts in FM 174 (soweit Ansible-fähig)
  hetzner Alle Hosts bei Hetzner (cloud)
  controllable Alle Hosts in FM 174 (soweit Ansible-fähig)
  virtual_FM174 Virtuelle Ansible Hosts in FM174
  virtual_ansible_hosts Nicht per Ansible erreichbare Maschinen, aber 
sie sollen durch (Meta-) Variablen doukmentiert werden

```
I think it is visible that this result makes no sense.

Can anyone help here ?

Thanks a lot !

Norbert


-- 
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/9f7caef6-bfcd-4257-8128-d872a0bd0b24n%40googlegroups.com.


[ansible-project] Re: I want to show the group variables, looping over all groups

2023-02-13 Thread Todd Lewis
Maybe need to back up a bit and ask what information you hope to get from 
the display.
If you were to put together your ideal report by hand, what would it look 
like, fully articulated?

On Monday, February 13, 2023 at 5:13:54 AM UTC-5 Norbert Klamann wrote:

> " It's still a host-centric view of your group comments.  "
>
> yes thats the problem  if you want to call ut so. It would be nice if 
> groups would be objects in their own right but I presume its a can of worms 
> which nobody wants to open because of the felxibility of the concept
>
> Todd Lewis schrieb am Mittwoch, 8. Februar 2023 um 18:01:22 UTC+1:
>
>> I see what you want, but it isn't possible. From each host's perspective, 
>> the `group_comment` variable will have the value as set in whichever one of 
>> that host's groups where the group name sorts last, as that's the last one 
>> loaded. So it is consistent, just not a solution to your problem.
>>
>> You can work around it if you want to go to the trouble. Say you have 
>> groups named gr1, gr2, gr3, etc. Within each group, put that group's 
>> comment in a variable called `group_comment_`, i.e. 
>> `group_comment_gr1`, `group_comment_gr2`, `group_comment_gr3`, etc.
>>
>> Then do something like the following to create a `group_comments` list 
>> for each host.
>>
>> - name: Create a list of group_comment_* variable names
>>   ansible.builtin.set_fact:
>> # Two different ways to do it.
>> group_comment_names_a: "{{ ['group_comment_'] | 
>> product(vars.group_names) | map('join') | flatten }}"
>> group_comment_names_b: "{{ query('ansible.builtin.varnames', 
>> '^group_comment_.+') }}"
>>
>> - name: Join group_comment_* into a list
>>   ansible.builtin.set_fact:
>> group_comments: |
>> {% set gclist = [] %}
>> {% for gcn in group_comment_names_a %}
>> {% set _ = gclist.append(query('ansible.builtin.vars', gcn)) 
>> %}
>> {% endfor %}{{ gclist | flatten }}
>>
>> I tried lots of different ways to invoke `ansible.builtin.vars` on a list 
>> using "normal" jinja pipelines before resorting to the old-school for loop 
>> above. If somebody knows how to do it I'd love to see your solution.
>>
>> Anyway, you end up with each host having a list containing each comment 
>> from each of its groups. But I somehow doubt that solves your problem 
>> either. It's still a host-centric view of your group comments.
>> On Wednesday, February 8, 2023 at 3:21:42 AM UTC-5 
>> norbert...@klamann-software.de wrote:
>>
>>> Hello all,
>>> i use Ansible to document and manage a small network of computers and 
>>> other devices. So I bulit an inventory with several groups and hosts and 
>>> defined a bunch of variables  on each level to describe what i want to 
>>> achieve,  Each host and each group has for example a variable named 
>>> `host_comment` or `group_comment` respectively.
>>>
>>> This is very useful on the host level, but i want to show the group 
>>> variables separately in a loop over all groups.
>>>
>>> I can show a group variable via `hostvars[host].group_comment`but 
>>> because every hosts belongs to several groups i cannot control, whicht name 
>>> is displayed.
>>>
>>> This template
>>> ```
>>> {% for group in groups  if  group != 'ungrouped' %}
>>>   {% for host in groups[group] %}
>>>   {% if loop.first %}{% set hvh = hostvars[host] %}
>>>   {{ group }} {{ hvh.group_comment }}
>>>   {% endif %}
>>>   {% endfor %}
>>> {% endfor %}
>>> ```
>>>
>>> gives 
>>> ```
>>>   all Nicht per Ansible erreichbare Maschinen, aber sie sollen durch 
>>> (Meta-) Variablen doukmentiert werden
>>>   lxc_container Alle Hosts in FM 174 (soweit Ansible-fähig)
>>>   fm174 Alle Hosts in FM 174 (soweit Ansible-fähig)
>>>   hetzner Alle Hosts bei Hetzner (cloud)
>>>   controllable Alle Hosts in FM 174 (soweit Ansible-fähig)
>>>   virtual_FM174 Virtuelle Ansible Hosts in FM174
>>>   virtual_ansible_hosts Nicht per Ansible erreichbare Maschinen, 
>>> aber sie sollen durch (Meta-) Variablen doukmentiert werden
>>>
>>> ```
>>> I think it is visible that this result makes no sense.
>>>
>>> Can anyone help here ?
>>>
>>> Thanks a lot !
>>>
>>> Norbert
>>>
>>>
>>>

-- 
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/afdc58d4-9641-40fc-bfa3-8884176a86a6n%40googlegroups.com.


[ansible-project] Re: I want to show the group variables, looping over all groups

2023-02-13 Thread Norbert Klamann
" It's still a host-centric view of your group comments.  "

yes thats the problem  if you want to call ut so. It would be nice if 
groups would be objects in their own right but I presume its a can of worms 
which nobody wants to open because of the felxibility of the concept

Todd Lewis schrieb am Mittwoch, 8. Februar 2023 um 18:01:22 UTC+1:

> I see what you want, but it isn't possible. From each host's perspective, 
> the `group_comment` variable will have the value as set in whichever one of 
> that host's groups where the group name sorts last, as that's the last one 
> loaded. So it is consistent, just not a solution to your problem.
>
> You can work around it if you want to go to the trouble. Say you have 
> groups named gr1, gr2, gr3, etc. Within each group, put that group's 
> comment in a variable called `group_comment_`, i.e. 
> `group_comment_gr1`, `group_comment_gr2`, `group_comment_gr3`, etc.
>
> Then do something like the following to create a `group_comments` list for 
> each host.
>
> - name: Create a list of group_comment_* variable names
>   ansible.builtin.set_fact:
> # Two different ways to do it.
> group_comment_names_a: "{{ ['group_comment_'] | 
> product(vars.group_names) | map('join') | flatten }}"
> group_comment_names_b: "{{ query('ansible.builtin.varnames', 
> '^group_comment_.+') }}"
>
> - name: Join group_comment_* into a list
>   ansible.builtin.set_fact:
> group_comments: |
> {% set gclist = [] %}
> {% for gcn in group_comment_names_a %}
> {% set _ = gclist.append(query('ansible.builtin.vars', gcn)) %}
> {% endfor %}{{ gclist | flatten }}
>
> I tried lots of different ways to invoke `ansible.builtin.vars` on a list 
> using "normal" jinja pipelines before resorting to the old-school for loop 
> above. If somebody knows how to do it I'd love to see your solution.
>
> Anyway, you end up with each host having a list containing each comment 
> from each of its groups. But I somehow doubt that solves your problem 
> either. It's still a host-centric view of your group comments.
> On Wednesday, February 8, 2023 at 3:21:42 AM UTC-5 
> norbert...@klamann-software.de wrote:
>
>> Hello all,
>> i use Ansible to document and manage a small network of computers and 
>> other devices. So I bulit an inventory with several groups and hosts and 
>> defined a bunch of variables  on each level to describe what i want to 
>> achieve,  Each host and each group has for example a variable named 
>> `host_comment` or `group_comment` respectively.
>>
>> This is very useful on the host level, but i want to show the group 
>> variables separately in a loop over all groups.
>>
>> I can show a group variable via `hostvars[host].group_comment`but because 
>> every hosts belongs to several groups i cannot control, whicht name is 
>> displayed.
>>
>> This template
>> ```
>> {% for group in groups  if  group != 'ungrouped' %}
>>   {% for host in groups[group] %}
>>   {% if loop.first %}{% set hvh = hostvars[host] %}
>>   {{ group }} {{ hvh.group_comment }}
>>   {% endif %}
>>   {% endfor %}
>> {% endfor %}
>> ```
>>
>> gives 
>> ```
>>   all Nicht per Ansible erreichbare Maschinen, aber sie sollen durch 
>> (Meta-) Variablen doukmentiert werden
>>   lxc_container Alle Hosts in FM 174 (soweit Ansible-fähig)
>>   fm174 Alle Hosts in FM 174 (soweit Ansible-fähig)
>>   hetzner Alle Hosts bei Hetzner (cloud)
>>   controllable Alle Hosts in FM 174 (soweit Ansible-fähig)
>>   virtual_FM174 Virtuelle Ansible Hosts in FM174
>>   virtual_ansible_hosts Nicht per Ansible erreichbare Maschinen, aber 
>> sie sollen durch (Meta-) Variablen doukmentiert werden
>>
>> ```
>> I think it is visible that this result makes no sense.
>>
>> Can anyone help here ?
>>
>> Thanks a lot !
>>
>> Norbert
>>
>>
>>

-- 
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/4ed204c4-1397-4689-a46c-0ea0d1d9eaabn%40googlegroups.com.


[ansible-project] Re: I want to show the group variables, looping over all groups

2023-02-08 Thread Todd Lewis
I see what you want, but it isn't possible. From each host's perspective, 
the `group_comment` variable will have the value as set in whichever one of 
that host's groups where the group name sorts last, as that's the last one 
loaded. So it is consistent, just not a solution to your problem.

You can work around it if you want to go to the trouble. Say you have 
groups named gr1, gr2, gr3, etc. Within each group, put that group's 
comment in a variable called `group_comment_`, i.e. 
`group_comment_gr1`, `group_comment_gr2`, `group_comment_gr3`, etc.

Then do something like the following to create a `group_comments` list for 
each host.

- name: Create a list of group_comment_* variable names
  ansible.builtin.set_fact:
# Two different ways to do it.
group_comment_names_a: "{{ ['group_comment_'] | 
product(vars.group_names) | map('join') | flatten }}"
group_comment_names_b: "{{ query('ansible.builtin.varnames', 
'^group_comment_.+') }}"

- name: Join group_comment_* into a list
  ansible.builtin.set_fact:
group_comments: |
{% set gclist = [] %}
{% for gcn in group_comment_names_a %}
{% set _ = gclist.append(query('ansible.builtin.vars', gcn)) %}
{% endfor %}{{ gclist | flatten }}

I tried lots of different ways to invoke `ansible.builtin.vars` on a list 
using "normal" jinja pipelines before resorting to the old-school for loop 
above. If somebody knows how to do it I'd love to see your solution.

Anyway, you end up with each host having a list containing each comment 
from each of its groups. But I somehow doubt that solves your problem 
either. It's still a host-centric view of your group comments.
On Wednesday, February 8, 2023 at 3:21:42 AM UTC-5 
norbert...@klamann-software.de wrote:

> Hello all,
> i use Ansible to document and manage a small network of computers and 
> other devices. So I bulit an inventory with several groups and hosts and 
> defined a bunch of variables  on each level to describe what i want to 
> achieve,  Each host and each group has for example a variable named 
> `host_comment` or `group_comment` respectively.
>
> This is very useful on the host level, but i want to show the group 
> variables separately in a loop over all groups.
>
> I can show a group variable via `hostvars[host].group_comment`but because 
> every hosts belongs to several groups i cannot control, whicht name is 
> displayed.
>
> This template
> ```
> {% for group in groups  if  group != 'ungrouped' %}
>   {% for host in groups[group] %}
>   {% if loop.first %}{% set hvh = hostvars[host] %}
>   {{ group }} {{ hvh.group_comment }}
>   {% endif %}
>   {% endfor %}
> {% endfor %}
> ```
>
> gives 
> ```
>   all Nicht per Ansible erreichbare Maschinen, aber sie sollen durch 
> (Meta-) Variablen doukmentiert werden
>   lxc_container Alle Hosts in FM 174 (soweit Ansible-fähig)
>   fm174 Alle Hosts in FM 174 (soweit Ansible-fähig)
>   hetzner Alle Hosts bei Hetzner (cloud)
>   controllable Alle Hosts in FM 174 (soweit Ansible-fähig)
>   virtual_FM174 Virtuelle Ansible Hosts in FM174
>   virtual_ansible_hosts Nicht per Ansible erreichbare Maschinen, aber 
> sie sollen durch (Meta-) Variablen doukmentiert werden
>
> ```
> I think it is visible that this result makes no sense.
>
> Can anyone help here ?
>
> Thanks a lot !
>
> Norbert
>
>
>

-- 
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/694d03f5-c1f0-4745-bcc1-2eb97ab9ac20n%40googlegroups.com.