mirror of
https://github.com/ansible/ansible.git
synced 2025-11-30 23:16:08 +07:00
generated inventory plugin, add extra vars option (#85372)
This commit is contained in:
3
changelogs/fragments/generator_use_extra.yml
Normal file
3
changelogs/fragments/generator_use_extra.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
minor_changes:
|
||||
- generator - add support for extra vars (https://github.com/ansible/ansible/issues/83270).
|
||||
@@ -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.21'
|
||||
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):
|
||||
@@ -128,10 +143,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,8 @@
|
||||
plugin: ansible.builtin.generator
|
||||
use_extra_vars: True
|
||||
hosts:
|
||||
name: "{{ region }}_{{ machine_type }}"
|
||||
layers:
|
||||
machine_type:
|
||||
- web
|
||||
- db
|
||||
@@ -3,3 +3,8 @@
|
||||
set -eux
|
||||
|
||||
ansible-playbook verify.yml -i generator.yml "${@}"
|
||||
|
||||
ANSIBLE_INVENTORY_USE_EXTRA_VARS=True ansible-inventory -i extra_vars_generator.yml --graph -e "region=pune" > out.txt
|
||||
|
||||
grep 'pune_web' out.txt
|
||||
grep 'pune_db' out.txt
|
||||
|
||||
Reference in New Issue
Block a user