summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/plugin_config_for_inventory/test_inventory.py
blob: f937c03806c5d739022a00a253c96f1634779a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
        cache:
            description: cache
            type: bool
            default: false
            required: False
        cache_plugin:
            description: cache plugin
            type: str
            default: none
            required: False
        cache_timeout:
            description: test cache parameter
            type: integer
            default: 7
            required: False
        cache_connection:
            description: cache connection
            type: str
            default: /tmp/foo
            required: False
        cache_prefix:
            description: cache prefix
            type: str
            default: prefix_
            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)
        config_data = self._read_config_data(path=path)
        self._consume_options(config_data)

        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)

        # Ensure the timeout we're given gets sent to the cache plugin
        if self.get_option('cache'):
            given_timeout = self.get_option('cache_timeout')
            cache_timeout = self._cache._plugin.get_option('_timeout')
            self.inventory.set_variable(host, 'given_timeout', given_timeout)
            self.inventory.set_variable(host, 'cache_timeout', cache_timeout)