Skip to content

Commit

Permalink
INI inventory plugin: add documentation about variable types (ansible…
Browse files Browse the repository at this point in the history
…#25798)

* INI inventory: check variable types
* INI inventory: add doc about variable types

Fixes ansible#25784
  • Loading branch information
pilou- authored and mattclay committed Jul 13, 2017
1 parent af4dc6d commit 2a92120
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/docsite/rst/intro_inventory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/ansible/plugins/inventory/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
- 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.
Expand Down
20 changes: 17 additions & 3 deletions test/units/inventory/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
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

Expand Down Expand Up @@ -109,7 +109,7 @@ def test_ranges(self):
)


class InventoryDefaultGroup(unittest.TestCase):
class IniInventory(unittest.TestCase):

def test_empty_inventory(self):
inventory = self._get_inventory('')
Expand Down Expand Up @@ -142,6 +142,20 @@ def test_ini_explicit_ungrouped(self):
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})
Expand Down

0 comments on commit 2a92120

Please sign in to comment.