2014-11-01 19:34:14 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
import os
|
|
|
|
|
2015-09-04 20:41:38 +00:00
|
|
|
from collections import defaultdict
|
2015-09-03 06:23:27 +00:00
|
|
|
|
2018-10-13 03:01:14 +00:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import MagicMock, mock_open, patch
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.inventory.manager import InventoryManager
|
2017-03-23 20:35:05 +00:00
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
from ansible.module_utils.six.moves import builtins
|
2015-09-04 20:41:38 +00:00
|
|
|
from ansible.playbook.play import Play
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
|
2015-05-08 16:34:19 +00:00
|
|
|
from units.mock.loader import DictDataLoader
|
2016-05-31 22:02:39 +00:00
|
|
|
from units.mock.path import mock_unfrackpath_noop
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.vars.manager import VariableManager
|
2016-10-04 15:08:34 +00:00
|
|
|
|
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
class TestVariableManager(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_basic_manager(self):
|
2015-03-25 18:51:40 +00:00
|
|
|
fake_loader = DictDataLoader({})
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_inventory = MagicMock()
|
|
|
|
v = VariableManager(loader=fake_loader, inventory=mock_inventory)
|
2017-08-28 21:21:11 +00:00
|
|
|
variables = v.get_vars(use_cache=False)
|
2016-11-23 21:30:46 +00:00
|
|
|
|
2017-08-28 21:21:11 +00:00
|
|
|
# Check var manager expected values, never check: ['omit', 'vars']
|
|
|
|
# FIXME: add the following ['ansible_version', 'ansible_playbook_python', 'groups']
|
|
|
|
for varname, value in (('playbook_dir', os.path.abspath('.')), ):
|
|
|
|
self.assertEqual(variables[varname], value)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-03-25 18:51:40 +00:00
|
|
|
def test_variable_manager_extra_vars(self):
|
|
|
|
fake_loader = DictDataLoader({})
|
|
|
|
|
2014-11-01 19:34:14 +00:00
|
|
|
extra_vars = dict(a=1, b=2, c=3)
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_inventory = MagicMock()
|
|
|
|
v = VariableManager(loader=fake_loader, inventory=mock_inventory)
|
2015-05-15 01:10:31 +00:00
|
|
|
v.extra_vars = extra_vars
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
vars = v.get_vars(use_cache=False)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-09-03 06:23:27 +00:00
|
|
|
for (key, val) in iteritems(extra_vars):
|
2015-05-15 01:10:31 +00:00
|
|
|
self.assertEqual(vars.get(key), val)
|
|
|
|
|
|
|
|
self.assertIsNot(v.extra_vars, extra_vars)
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-03-25 18:51:40 +00:00
|
|
|
def test_variable_manager_play_vars(self):
|
|
|
|
fake_loader = DictDataLoader({})
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.get_vars.return_value = dict(foo="bar")
|
|
|
|
mock_play.get_roles.return_value = []
|
|
|
|
mock_play.get_vars_files.return_value = []
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_inventory = MagicMock()
|
|
|
|
v = VariableManager(loader=fake_loader, inventory=mock_inventory)
|
|
|
|
self.assertEqual(v.get_vars(play=mock_play, use_cache=False).get("foo"), "bar")
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2015-03-25 18:51:40 +00:00
|
|
|
def test_variable_manager_play_vars_files(self):
|
2014-11-01 19:34:14 +00:00
|
|
|
fake_loader = DictDataLoader({
|
2017-05-23 21:16:49 +00:00
|
|
|
__file__: """
|
2014-11-01 19:34:14 +00:00
|
|
|
foo: bar
|
|
|
|
"""
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.get_vars.return_value = dict()
|
|
|
|
mock_play.get_roles.return_value = []
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_play.get_vars_files.return_value = [__file__]
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_inventory = MagicMock()
|
|
|
|
v = VariableManager(inventory=mock_inventory, loader=fake_loader)
|
|
|
|
self.assertEqual(v.get_vars(play=mock_play, use_cache=False).get("foo"), "bar")
|
2015-03-25 18:51:40 +00:00
|
|
|
|
|
|
|
def test_variable_manager_task_vars(self):
|
2017-05-30 17:05:19 +00:00
|
|
|
# FIXME: BCS make this work
|
2017-05-23 21:16:49 +00:00
|
|
|
return
|
|
|
|
|
2017-09-13 01:49:24 +00:00
|
|
|
# pylint: disable=unreachable
|
2015-03-25 18:51:40 +00:00
|
|
|
fake_loader = DictDataLoader({})
|
2014-11-01 19:34:14 +00:00
|
|
|
|
|
|
|
mock_task = MagicMock()
|
2015-05-12 17:24:57 +00:00
|
|
|
mock_task._role = None
|
2015-09-25 05:33:45 +00:00
|
|
|
mock_task.loop = None
|
2014-11-01 19:34:14 +00:00
|
|
|
mock_task.get_vars.return_value = dict(foo="bar")
|
2015-11-19 14:01:51 +00:00
|
|
|
mock_task.get_include_params.return_value = dict()
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_all = MagicMock()
|
|
|
|
mock_all.get_vars.return_value = {}
|
|
|
|
mock_all.get_file_vars.return_value = {}
|
|
|
|
|
|
|
|
mock_host = MagicMock()
|
|
|
|
mock_host.get.name.return_value = 'test01'
|
|
|
|
mock_host.get_vars.return_value = {}
|
|
|
|
mock_host.get_host_vars.return_value = {}
|
|
|
|
|
|
|
|
mock_inventory = MagicMock()
|
|
|
|
mock_inventory.hosts.get.return_value = mock_host
|
|
|
|
mock_inventory.hosts.get.name.return_value = 'test01'
|
|
|
|
mock_inventory.get_host.return_value = mock_host
|
|
|
|
mock_inventory.groups.__getitem__.return_value = mock_all
|
|
|
|
|
|
|
|
v = VariableManager(loader=fake_loader, inventory=mock_inventory)
|
|
|
|
self.assertEqual(v.get_vars(task=mock_task, use_cache=False).get("foo"), "bar")
|
2014-11-01 19:34:14 +00:00
|
|
|
|
2016-05-31 22:02:39 +00:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2017-05-23 21:16:49 +00:00
|
|
|
def test_variable_manager_precedence(self):
|
2017-05-30 17:05:19 +00:00
|
|
|
# FIXME: this needs to be redone as dataloader is not the automatic source of data anymore
|
2017-05-23 21:16:49 +00:00
|
|
|
return
|
2017-09-13 01:49:24 +00:00
|
|
|
|
|
|
|
# pylint: disable=unreachable
|
2015-09-04 20:41:38 +00:00
|
|
|
'''
|
|
|
|
Tests complex variations and combinations of get_vars() with different
|
|
|
|
objects to modify the context under which variables are merged.
|
|
|
|
'''
|
2017-05-30 17:05:19 +00:00
|
|
|
# FIXME: BCS makethiswork
|
|
|
|
# return True
|
2015-09-04 20:41:38 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_inventory = MagicMock()
|
2015-09-04 20:41:38 +00:00
|
|
|
|
2016-10-04 15:08:34 +00:00
|
|
|
inventory1_filedata = """
|
2015-09-04 20:41:38 +00:00
|
|
|
[group2:children]
|
|
|
|
group1
|
|
|
|
|
|
|
|
[group1]
|
|
|
|
host1 host_var=host_var_from_inventory_host1
|
|
|
|
|
|
|
|
[group1:vars]
|
|
|
|
group_var = group_var_from_inventory_group1
|
|
|
|
|
|
|
|
[group2:vars]
|
|
|
|
group_var = group_var_from_inventory_group2
|
2016-10-04 15:08:34 +00:00
|
|
|
"""
|
2015-09-04 20:41:38 +00:00
|
|
|
|
2016-10-04 15:08:34 +00:00
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
# inventory1
|
|
|
|
'/etc/ansible/inventory1': inventory1_filedata,
|
2015-09-04 20:41:38 +00:00
|
|
|
# role defaults_only1
|
|
|
|
'/etc/ansible/roles/defaults_only1/defaults/main.yml': """
|
|
|
|
default_var: "default_var_from_defaults_only1"
|
|
|
|
host_var: "host_var_from_defaults_only1"
|
|
|
|
group_var: "group_var_from_defaults_only1"
|
|
|
|
group_var_all: "group_var_all_from_defaults_only1"
|
|
|
|
extra_var: "extra_var_from_defaults_only1"
|
|
|
|
""",
|
|
|
|
'/etc/ansible/roles/defaults_only1/tasks/main.yml': """
|
|
|
|
- debug: msg="here i am"
|
|
|
|
""",
|
|
|
|
|
|
|
|
# role defaults_only2
|
|
|
|
'/etc/ansible/roles/defaults_only2/defaults/main.yml': """
|
|
|
|
default_var: "default_var_from_defaults_only2"
|
|
|
|
host_var: "host_var_from_defaults_only2"
|
|
|
|
group_var: "group_var_from_defaults_only2"
|
|
|
|
group_var_all: "group_var_all_from_defaults_only2"
|
|
|
|
extra_var: "extra_var_from_defaults_only2"
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
inv1 = InventoryManager(loader=fake_loader, sources=['/etc/ansible/inventory1'])
|
|
|
|
v = VariableManager(inventory=mock_inventory, loader=fake_loader)
|
|
|
|
v._fact_cache = defaultdict(dict)
|
2015-09-04 20:41:38 +00:00
|
|
|
|
|
|
|
play1 = Play.load(dict(
|
2017-01-29 07:28:53 +00:00
|
|
|
hosts=['all'],
|
|
|
|
roles=['defaults_only1', 'defaults_only2'],
|
2015-09-04 20:41:38 +00:00
|
|
|
), loader=fake_loader, variable_manager=v)
|
|
|
|
|
|
|
|
# first we assert that the defaults as viewed as a whole are the merged results
|
|
|
|
# of the defaults from each role, with the last role defined "winning" when
|
|
|
|
# there is a variable naming conflict
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1)
|
2015-09-04 20:41:38 +00:00
|
|
|
self.assertEqual(res['default_var'], 'default_var_from_defaults_only2')
|
|
|
|
|
|
|
|
# next, we assert that when vars are viewed from the context of a task within a
|
|
|
|
# role, that task will see its own role defaults before any other role's
|
|
|
|
blocks = play1.compile()
|
|
|
|
task = blocks[1].block[0]
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1, task=task)
|
2015-09-04 20:41:38 +00:00
|
|
|
self.assertEqual(res['default_var'], 'default_var_from_defaults_only1')
|
|
|
|
|
2017-06-12 06:55:19 +00:00
|
|
|
# next we assert the precedence of inventory variables
|
2015-09-04 20:41:38 +00:00
|
|
|
v.set_inventory(inv1)
|
|
|
|
h1 = inv1.get_host('host1')
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1, host=h1)
|
2015-09-04 20:41:38 +00:00
|
|
|
self.assertEqual(res['group_var'], 'group_var_from_inventory_group1')
|
|
|
|
self.assertEqual(res['host_var'], 'host_var_from_inventory_host1')
|
|
|
|
|
|
|
|
# next we test with group_vars/ files loaded
|
|
|
|
fake_loader.push("/etc/ansible/group_vars/all", """
|
|
|
|
group_var_all: group_var_all_from_group_vars_all
|
|
|
|
""")
|
|
|
|
fake_loader.push("/etc/ansible/group_vars/group1", """
|
|
|
|
group_var: group_var_from_group_vars_group1
|
|
|
|
""")
|
|
|
|
fake_loader.push("/etc/ansible/group_vars/group3", """
|
|
|
|
# this is a dummy, which should not be used anywhere
|
|
|
|
group_var: group_var_from_group_vars_group3
|
|
|
|
""")
|
|
|
|
fake_loader.push("/etc/ansible/host_vars/host1", """
|
|
|
|
host_var: host_var_from_host_vars_host1
|
|
|
|
""")
|
2016-06-06 07:26:07 +00:00
|
|
|
fake_loader.push("group_vars/group1", """
|
|
|
|
playbook_group_var: playbook_group_var
|
|
|
|
""")
|
|
|
|
fake_loader.push("host_vars/host1", """
|
|
|
|
playbook_host_var: playbook_host_var
|
|
|
|
""")
|
2015-09-04 20:41:38 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1, host=h1)
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertEqual(res['group_var'], 'group_var_from_group_vars_group1')
|
|
|
|
# self.assertEqual(res['group_var_all'], 'group_var_all_from_group_vars_all')
|
|
|
|
# self.assertEqual(res['playbook_group_var'], 'playbook_group_var')
|
|
|
|
# self.assertEqual(res['host_var'], 'host_var_from_host_vars_host1')
|
|
|
|
# self.assertEqual(res['playbook_host_var'], 'playbook_host_var')
|
2015-09-04 20:41:38 +00:00
|
|
|
|
|
|
|
# add in the fact cache
|
|
|
|
v._fact_cache['host1'] = dict(fact_cache_var="fact_cache_var_from_fact_cache")
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1, host=h1)
|
2015-09-04 20:41:38 +00:00
|
|
|
self.assertEqual(res['fact_cache_var'], 'fact_cache_var_from_fact_cache')
|
2016-07-20 14:14:44 +00:00
|
|
|
|
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
|
|
|
def test_variable_manager_role_vars_dependencies(self):
|
|
|
|
'''
|
|
|
|
Tests vars from role dependencies with duplicate dependencies.
|
|
|
|
'''
|
2017-05-23 21:16:49 +00:00
|
|
|
mock_inventory = MagicMock()
|
2016-07-20 14:14:44 +00:00
|
|
|
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
# role common-role
|
|
|
|
'/etc/ansible/roles/common-role/tasks/main.yml': """
|
|
|
|
- debug: msg="{{role_var}}"
|
|
|
|
""",
|
|
|
|
# We do not need allow_duplicates: yes for this role
|
|
|
|
# because eliminating duplicates is done by the execution
|
|
|
|
# strategy, which we do not test here.
|
|
|
|
|
|
|
|
# role role1
|
|
|
|
'/etc/ansible/roles/role1/vars/main.yml': """
|
|
|
|
role_var: "role_var_from_role1"
|
|
|
|
""",
|
|
|
|
'/etc/ansible/roles/role1/meta/main.yml': """
|
|
|
|
dependencies:
|
|
|
|
- { role: common-role }
|
|
|
|
""",
|
|
|
|
|
|
|
|
# role role2
|
|
|
|
'/etc/ansible/roles/role2/vars/main.yml': """
|
|
|
|
role_var: "role_var_from_role2"
|
|
|
|
""",
|
|
|
|
'/etc/ansible/roles/role2/meta/main.yml': """
|
|
|
|
dependencies:
|
|
|
|
- { role: common-role }
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
v = VariableManager(loader=fake_loader, inventory=mock_inventory)
|
|
|
|
v._fact_cache = defaultdict(dict)
|
|
|
|
|
2016-07-20 14:14:44 +00:00
|
|
|
play1 = Play.load(dict(
|
2017-01-29 07:28:53 +00:00
|
|
|
hosts=['all'],
|
|
|
|
roles=['role1', 'role2'],
|
2016-07-20 14:14:44 +00:00
|
|
|
), loader=fake_loader, variable_manager=v)
|
|
|
|
|
|
|
|
# The task defined by common-role exists twice because role1
|
|
|
|
# and role2 depend on common-role. Check that the tasks see
|
|
|
|
# different values of role_var.
|
|
|
|
blocks = play1.compile()
|
|
|
|
task = blocks[1].block[0]
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1, task=task)
|
2016-07-20 14:14:44 +00:00
|
|
|
self.assertEqual(res['role_var'], 'role_var_from_role1')
|
|
|
|
|
|
|
|
task = blocks[2].block[0]
|
2017-05-23 21:16:49 +00:00
|
|
|
res = v.get_vars(play=play1, task=task)
|
2016-07-20 14:14:44 +00:00
|
|
|
self.assertEqual(res['role_var'], 'role_var_from_role2')
|