build groups based on hosts and host-variables

24 views
Skip to first unread message

andregr...@gmail.com

unread,
Jul 1, 2024, 9:17:26 AM (2 days ago) Jul 1
to Ansible Project
hi,
i would like to organize my inventory (which is set up and maintained manually):
- one inventory file per os (currently linux and windows) with one group in each file (e.g. alllinux and allwindows)
- put group-information into each host (in the inventory file or in host-vars-directory)

my goal: i want to configure information per host to control if a playbook may or may not work on a host. but i don't want to have different inventory files. i think it would be easier to have it "my way" (kind of tags per host).

is that possible? i didn't find a way to do it.

regards,
andre

Brian Coca

unread,
Jul 1, 2024, 10:57:23 AM (2 days ago) Jul 1
to ansible...@googlegroups.com
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)

Vladimir Botka

unread,
Jul 1, 2024, 12:06:14 PM (2 days ago) Jul 1
to Brian Coca, ansible...@googlegroups.com
Hi!,

On Mon, 1 Jul 2024 10:56:51 -0400
Brian Coca <bc...@redhat.com> 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

Brian Coca

unread,
Jul 1, 2024, 12:33:15 PM (2 days ago) Jul 1
to Vladimir Botka, ansible...@googlegroups.com
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)'

Vladimir Botka

unread,
Jul 1, 2024, 3:27:41 PM (2 days ago) Jul 1
to andregr...@gmail.com, ansible...@googlegroups.com
On Mon, 1 Jul 2024 06:17:26 -0700 (PDT)
"andregr...@gmail.com" <andregr...@gmail.com> 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

andregr...@gmail.com

unread,
Jul 2, 2024, 4:36:00 PM (8 hours ago) Jul 2
to Ansible Project
thx a lot for the input. i will read and test all the proposals. THX
regards,
andre

Reply all
Reply to author
Forward
0 new messages