generator: add support for extra vars usage (#84544)

* generator: add support for extra vars usage

Fixes: #83270

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

* CI green

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

* Review requests

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

* Review requests II

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

---------

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde
2025-01-21 15:20:51 -05:00
committed by GitHub
parent 4953fc7b26
commit 186c716af1
7 changed files with 85 additions and 1 deletions
@@ -0,0 +1,3 @@
---
minor_changes:
- generator - add support for extra vars (https://github.com/ansible/ansible/issues/83270).
+20 -1
View File
@@ -32,6 +32,20 @@ DOCUMENTATION = """
description:
- A dictionary of layers, with the key being the layer name, used as a variable name in the C(host)
C(name) and C(parents) keys. Each layer value is a list of possible values for that layer.
use_extra_vars:
version_added: '2.19'
description:
- Merge extra vars into the available variables for composition (highest precedence).
type: bool
default: false
ini:
- section: inventory_plugins
key: use_extra_vars
- section: inventory_plugin_generator
key: use_extra_vars
env:
- name: ANSIBLE_INVENTORY_USE_EXTRA_VARS
- name: ANSIBLE_GENERATOR_USE_EXTRA_VARS
"""
EXAMPLES = """
@@ -77,6 +91,7 @@ from itertools import product
from ansible import constants as C
from ansible.errors import AnsibleParserError
from ansible.plugins.inventory import BaseInventoryPlugin
from ansible.utils.vars import load_extra_vars
class InventoryModule(BaseInventoryPlugin):
@@ -123,10 +138,14 @@ class InventoryModule(BaseInventoryPlugin):
super(InventoryModule, self).parse(inventory, loader, path, cache=cache)
config = self._read_config_data(path)
if self.get_option('use_extra_vars'):
extra_vars = load_extra_vars(loader)
else:
extra_vars = {}
template_inputs = product(*config['layers'].values())
for item in template_inputs:
template_vars = dict()
template_vars.update(extra_vars)
for i, key in enumerate(config['layers'].keys()):
template_vars[key] = item[i]
host = self.template(config['hosts']['name'], template_vars)
@@ -0,0 +1 @@
shippable/posix/group5
@@ -0,0 +1,8 @@
plugin: ansible.builtin.generator
use_extra_vars: True
hosts:
name: "{{ region }}_{{ machine_type }}"
layers:
machine_type:
- web
- db
@@ -0,0 +1,10 @@
plugin: ansible.builtin.generator
hosts:
name: "{{ region }}_{{ machine_type }}"
layers:
machine_type:
- web
- db
region:
- mumbai
- pune
@@ -0,0 +1,18 @@
plugin: ansible.builtin.generator
hosts:
name: "{{ application }}_{{ operation }}_runner"
parents:
- name: "{{ environment }}"
vars:
log: "s"
layers:
operation:
- build
- launch
environment:
- dev
- test
- prod
application:
- web
- api
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -eux
ansible-inventory -i generator.yml --graph | tee out.txt
grep 'mumbai_web' out.txt
grep 'mumbai_db' out.txt
grep 'pune_web' out.txt
grep 'pune_db' out.txt
ANSIBLE_INVENTORY_USE_EXTRA_VARS=True ansible-inventory -i extra_vars_generator.yml --graph -e "region=pune"
grep 'pune_web' out.txt
grep 'pune_db' out.txt
ansible-inventory -i generator_parent.yml --graph | tee out.txt
grep 'web_build_runner' out.txt
grep 'api_build_runner' out.txt
grep 'web_launch_runner' out.txt
grep 'api_launch_runner' out.txt
grep '@dev' out.txt
grep '@test' out.txt
grep '@prod' out.txt