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