mirror of https://github.com/ansible/ansible.git
* 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
(cherry picked from commit 06f5e49dfb)
* Update aliases
pull/43942/head
parent
dabd81deba
commit
76392c3ddd
@ -0,0 +1 @@
|
||||
shippable/posix/group3
|
||||
@ -0,0 +1,3 @@
|
||||
plugin: test_inventory
|
||||
departments:
|
||||
- paris
|
||||
@ -0,0 +1 @@
|
||||
plugin: test_inventory
|
||||
@ -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 New Issue