Re: [ansible-project] build groups based on hosts and host-variables

2024-07-02 Thread [email protected]
thx a lot for the input. i will read and test all the proposals. THX
regards,
andre

Vladimir Botka schrieb am Montag, 1. Juli 2024 um 21:27:41 UTC+2:

> On Mon, 1 Jul 2024 06:17:26 -0700 (PDT)
> "[email protected]"  wrote:
>
> > ... put group-information into each host ... per host to
> > control if a playbook may or may not work on a host
>
> The playbook name is not available to the inventory
> plugins. The below solution provides the inventory plugin
> with the playbook name in the extra variable. The allowed
> hosts are listed in a dictionary in group_vars.
>
> * Create a dictionary with the lists of the allowed hosts
>
> shell> cat inventory/alllinux/01-hosts.yml 
> alllinux:
> hosts:
> test_01:
> test_02:
> test_03:
> vars:
> play_allow:
> default: []
> playbook_01: [test_01, test_02]
> playbook_02: [test_02, test_03]
>
> For example, the below playbook shall run on the hosts
> test_01 and test_02 only
>
> shell> cat playbook_01.yml 
> - name: playbook_01
> hosts: my_hosts
> tasks:
> - debug:
> msg: Start play
>
> * Use the inventory plugin *constructed* and create the
> group *my_hosts*
>
> shell> cat inventory/alllinux/02-constructed.yml 
> plugin: ansible.builtin.constructed
> use_extra_vars: true
> use_vars_plugins: true
> compose:
> my_hosts_allow: play_allow[my_play|d('default')]
> groups:
> my_hosts: inventory_hostname in my_hosts_allow
>
> * Unfortunately, the name of the playbook is not available
> in the inventory plugins. Therefor, you have to provide
> it explicitly. For example, in the extra vars *my_play*
>
> shell> ansible-playbook -i inventory/alllinux -e my_play=playbook_01 
> playbook_01.yml
>
> gives abridged
>
> ok: [test_01] =>
> msg: Start play
> ok: [test_02] => 
> msg: Start play
>
> HTH,
>
> -- 
> 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/4b762370-2006-4750-95eb-fdec7eba95a0n%40googlegroups.com.


Re: [ansible-project] build groups based on hosts and host-variables

2024-07-01 Thread Vladimir Botka
On Mon, 1 Jul 2024 06:17:26 -0700 (PDT)
"[email protected]"  wrote:

> ... put group-information into each host ... per host to
> control if a playbook may or may not work on a host

The playbook name is not available to the inventory
plugins. The below solution provides the inventory plugin
with the playbook name in the extra variable. The allowed
hosts are listed in a dictionary in group_vars.

* Create a dictionary with the lists of the allowed hosts

shell> cat inventory/alllinux/01-hosts.yml 
alllinux:
  hosts:
test_01:
test_02:
test_03:
  vars:
play_allow:
  default: []
  playbook_01: [test_01, test_02]
  playbook_02: [test_02, test_03]

For example, the below playbook shall run on the hosts
test_01 and test_02 only

shell> cat playbook_01.yml 
- name: playbook_01
  hosts: my_hosts
  tasks:
- debug:
msg: Start play

* Use the inventory plugin *constructed* and create the
  group *my_hosts*

shell> cat inventory/alllinux/02-constructed.yml 
plugin: ansible.builtin.constructed
use_extra_vars: true
use_vars_plugins: true
compose:
  my_hosts_allow: play_allow[my_play|d('default')]
groups:
  my_hosts: inventory_hostname in my_hosts_allow

* Unfortunately, the name of the playbook is not available
  in the inventory plugins. Therefor, you have to provide
  it explicitly. For example, in the extra vars *my_play*

shell> ansible-playbook -i inventory/alllinux -e my_play=playbook_01 
playbook_01.yml

gives abridged

ok: [test_01] =>
  msg: Start play
ok: [test_02] => 
  msg: Start play

HTH,

-- 
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/20240701212715.68ce7c50%40gmail.com.


pgpeFZSch24wO.pgp
Description: OpenPGP digital signature


Re: [ansible-project] build groups based on hosts and host-variables

2024-07-01 Thread Brian Coca
The problem was posed with setting variables per host in inventory,
you can use the constructed inventory to use those variables to create
groups, the plays can filter only for the groups they are known to
work with or not, i.e  hosts: valid_groups or  hosts:
normalgropus:!invalid_for_this_play.

if you are getting teh variables dynamically, you can do same/similar
with group_by, but then you need 3 plays, 1 to gather the info, 2 to
use the group_by 3 the original play that processes the 'valid
group(s)'
-- 
--
Brian Coca (he/him/yo)

-- 
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/CACVha7di%3DPkVEnnefWKAy2XL749JCNK_Jrs2K6sF%3Dt_EGO%2BGeA%40mail.gmail.com.


Re: [ansible-project] build groups based on hosts and host-variables

2024-07-01 Thread Vladimir Botka
Hi!,

On Mon, 1 Jul 2024 10:56:51 -0400
Brian Coca  wrote:

> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/constructed_inventory.html
> ^ use 3rd inventory source that can construct groups and assign group
> vars based on data from previous inventory sources.

The requirement is to: "control if a playbook may or may not
work on a host". How does the inventory plugin know which
playbook is running? The special variable ansible_play_name
is not available in the inventory plugin.

Thank you,

-- 
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/20240701180542.23ce39e3%40gmail.com.


pgpS0lQrRymv9.pgp
Description: OpenPGP digital signature


Re: [ansible-project] build groups based on hosts and host-variables

2024-07-01 Thread Brian Coca
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/constructed_inventory.html
^ use 3rd inventory source that can construct groups and assign group
vars based on data from previous inventory sources.

-- 
--
Brian Coca (he/him/yo)

-- 
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/CACVha7f-rzTARQLSPO-ZhOZyO--PoPcDSF9nVNyk4WvO9yEjAg%40mail.gmail.com.