mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
inventory plugins: add test about config API usage (#41888)
* Check get_option method works with inventory plugins This use case is already tested by some cloud inventoty plugin but these tests are slow and aren't always executed, hence this new quick test. * AnsiblePlugin: Fix typo in docstring
This commit is contained in:
parent
2653bd3f50
commit
06f5e49dfb
6 changed files with 73 additions and 1 deletions
|
@ -67,7 +67,7 @@ class AnsiblePlugin(with_metaclass(ABCMeta, object)):
|
|||
Sets the _options attribute with the configuration/keyword information for this plugin
|
||||
|
||||
:arg task_keys: Dict with playbook keywords that affect this option
|
||||
:arg var_options: Dict with either 'conneciton variables'
|
||||
:arg var_options: Dict with either 'connection variables'
|
||||
:arg direct: Dict with 'direct assignment'
|
||||
'''
|
||||
self._options = C.config.get_plugin_options(get_plugin_class(self), self._load_name, keys=task_keys, variables=var_options, direct=direct)
|
||||
|
|
1
test/integration/targets/inventory_plugin_config/aliases
Normal file
1
test/integration/targets/inventory_plugin_config/aliases
Normal file
|
@ -0,0 +1 @@
|
|||
posix/ci/group3
|
|
@ -0,0 +1,3 @@
|
|||
plugin: test_inventory
|
||||
departments:
|
||||
- paris
|
|
@ -0,0 +1 @@
|
|||
plugin: test_inventory
|
16
test/integration/targets/inventory_plugin_config/runme.sh
Executable file
16
test/integration/targets/inventory_plugin_config/runme.sh
Executable file
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit -o nounset -o xtrace
|
||||
|
||||
export ANSIBLE_INVENTORY_PLUGINS=./
|
||||
export ANSIBLE_INVENTORY_ENABLED=test_inventory
|
||||
|
||||
# check default values
|
||||
ansible-inventory --list -i ./config_without_parameter.yml --export | \
|
||||
env python -c "import json, sys; inv = json.loads(sys.stdin.read()); \
|
||||
assert set(inv['_meta']['hostvars']['test_host']['departments']) == set(['seine-et-marne', 'haute-garonne'])"
|
||||
|
||||
# check values
|
||||
ansible-inventory --list -i ./config_with_parameter.yml --export | \
|
||||
env python -c "import json, sys; inv = json.loads(sys.stdin.read()); \
|
||||
assert set(inv['_meta']['hostvars']['test_host']['departments']) == set(['paris'])"
|
|
@ -0,0 +1,51 @@
|
|||
from __future__ import (absolute_import, division, print_function)
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = '''
|
||||
name: test_inventory
|
||||
plugin_type: inventory
|
||||
authors:
|
||||
- Pierre-Louis Bonicoli (@pilou-)
|
||||
short_description: test inventory
|
||||
description:
|
||||
- test inventory (fetch parameters using config API)
|
||||
options:
|
||||
departments:
|
||||
description: test parameter
|
||||
type: list
|
||||
default:
|
||||
- seine-et-marne
|
||||
- haute-garonne
|
||||
required: False
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Example command line: ansible-inventory --list -i test_inventory.yml
|
||||
|
||||
plugin: test_inventory
|
||||
departments:
|
||||
- paris
|
||||
'''
|
||||
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin
|
||||
|
||||
|
||||
class InventoryModule(BaseInventoryPlugin):
|
||||
NAME = 'test_inventory'
|
||||
|
||||
def verify_file(self, path):
|
||||
return True
|
||||
|
||||
def parse(self, inventory, loader, path, cache=True):
|
||||
super(InventoryModule, self).parse(inventory, loader, path)
|
||||
self._read_config_data(path=path)
|
||||
|
||||
departments = self.get_option('departments')
|
||||
|
||||
group = 'test_group'
|
||||
host = 'test_host'
|
||||
|
||||
self.inventory.add_group(group)
|
||||
self.inventory.add_host(group=group, host=host)
|
||||
self.inventory.set_variable(host, 'departments', departments)
|
Loading…
Reference in a new issue