mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
INI inventory plugin: add documentation about variable types (#25798)
* INI inventory: check variable types * INI inventory: add doc about variable types Fixes #25784
This commit is contained in:
parent
af4dc6d0eb
commit
2a92120ffa
3 changed files with 24 additions and 3 deletions
|
@ -60,6 +60,10 @@ Suppose you have just static IPs and want to set up some aliases that live in yo
|
|||
In the above example, trying to ansible against the host alias "jumper" (which may not even be a real hostname) will contact 192.0.2.50 on port 5555. Note that this is using a feature of the inventory file to define some special variables. Generally speaking this is not the best
|
||||
way to define variables that describe your system policy, but we'll share suggestions on doing this later. We're just getting started.
|
||||
|
||||
.. note:: Values passed in using the ``key=value`` syntax are interpreted as Python literal structure (strings, numbers, tuples, lists, dicts,
|
||||
booleans, None), alternatively as string. For example ``var=FALSE`` would create a string equal to 'FALSE'. Do not rely on types set
|
||||
during definition, always make sure you specify type with a filter when needed when consuming the variable.
|
||||
|
||||
Adding a lot of hosts? If you have a lot of hosts following similar patterns you can do this rather than listing each hostname:
|
||||
|
||||
.. code-block:: ini
|
||||
|
|
|
@ -27,6 +27,9 @@ DOCUMENTATION:
|
|||
- The C(children) modifier indicates that the section contains groups.
|
||||
- The C(vars) modifier indicates that the section contains variables assigned to members of the group.
|
||||
- Anything found outside a section is considered an 'ungrouped' host.
|
||||
- Values passed in using the C(key=value) syntax are interpreted as Python literal structure (strings, numbers, tuples, lists, dicts,
|
||||
booleans, None), alternatively as string. For example C(var=FALSE) would create a string equal to 'FALSE'. Do not rely on types set
|
||||
during definition, always make sure you specify type with a filter when needed when consuming the variable.
|
||||
notes:
|
||||
- It takes the place of the previously hardcoded INI inventory.
|
||||
- To function it requires being whitelisted in configuration.
|
||||
|
|
|
@ -22,10 +22,10 @@ __metaclass__ = type
|
|||
import string
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils._text import to_text
|
||||
|
||||
from ansible.inventory.manager import InventoryManager, split_host_pattern
|
||||
from ansible.vars.manager import VariableManager
|
||||
|
||||
from units.mock.loader import DictDataLoader
|
||||
|
||||
|
@ -109,7 +109,7 @@ class TestInventory(unittest.TestCase):
|
|||
)
|
||||
|
||||
|
||||
class InventoryDefaultGroup(unittest.TestCase):
|
||||
class IniInventory(unittest.TestCase):
|
||||
|
||||
def test_empty_inventory(self):
|
||||
inventory = self._get_inventory('')
|
||||
|
@ -142,6 +142,20 @@ class InventoryDefaultGroup(unittest.TestCase):
|
|||
host5
|
||||
""")
|
||||
|
||||
def test_ini_variables_stringify(self):
|
||||
values = ['string', 'no', 'No', 'false', 'FALSE', [], False, 0]
|
||||
|
||||
inventory_content = "host1 "
|
||||
inventory_content += ' '.join(['var%s=%s' % (i, to_text(x)) for i, x in enumerate(values)])
|
||||
inventory = self._get_inventory(inventory_content)
|
||||
|
||||
variables = inventory.get_host('host1').vars
|
||||
for i in range(len(values)):
|
||||
if isinstance(values[i], string_types):
|
||||
self.assertIsInstance(variables['var%s' % i], string_types)
|
||||
else:
|
||||
self.assertIsInstance(variables['var%s' % i], type(values[i]))
|
||||
|
||||
def _get_inventory(self, inventory_content):
|
||||
|
||||
fake_loader = DictDataLoader({__file__: inventory_content})
|
||||
|
|
Loading…
Reference in a new issue