From 5cd3be9129c81388c213bcd6b5de7741b9eee8c4 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Tue, 17 Sep 2019 18:35:44 +0530 Subject: [PATCH] gather_facts action plugin: Fix loading network facts modules for smart gathering (#59856) * fix smart gathering for network_os in gather_facts action plugin Signed-off-by: Trishna Guha * Fix detection of network_os for smart gathering Signed-off-by: Trishna Guha * Add unittest Signed-off-by: Trishna Guha * make pep8 happy Signed-off-by: Trishna Guha --- lib/ansible/plugins/action/gather_facts.py | 4 +- .../units/plugins/action/test_gather_facts.py | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 test/units/plugins/action/test_gather_facts.py diff --git a/lib/ansible/plugins/action/gather_facts.py b/lib/ansible/plugins/action/gather_facts.py index d95b6125ce..393df0b0fa 100644 --- a/lib/ansible/plugins/action/gather_facts.py +++ b/lib/ansible/plugins/action/gather_facts.py @@ -54,10 +54,10 @@ class ActionModule(ActionBase): modules = C.config.get_config_value('FACTS_MODULES', variables=task_vars) parallel = task_vars.pop('ansible_facts_parallel', self._task.args.pop('parallel', None)) - if 'smart' in modules: connection_map = C.config.get_config_value('CONNECTION_FACTS_MODULES', variables=task_vars) - modules.extend([connection_map.get(self._connection._load_name, 'setup')]) + network_os = self._task.args.get('network_os', task_vars.get('ansible_network_os', task_vars.get('ansible_facts', {}).get('network_os'))) + modules.extend([connection_map.get(network_os or self._connection._load_name, 'setup')]) modules.pop(modules.index('smart')) failed = {} diff --git a/test/units/plugins/action/test_gather_facts.py b/test/units/plugins/action/test_gather_facts.py new file mode 100644 index 0000000000..d65542fb64 --- /dev/null +++ b/test/units/plugins/action/test_gather_facts.py @@ -0,0 +1,64 @@ +# (c) 2016, Saran Ahluwalia +# +# 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 . + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from units.compat import unittest +from units.compat.mock import MagicMock + +from ansible import constants as C +from ansible.plugins.action.gather_facts import ActionModule +from ansible.playbook.task import Task +from ansible.template import Templar + +from units.mock.loader import DictDataLoader + + +class TestNetworkFacts(unittest.TestCase): + task = MagicMock(Task) + task_vars = {'ansible_network_os': 'ios'} + play_context = MagicMock() + play_context.check_mode = False + connection = MagicMock() + fake_loader = DictDataLoader({ + }) + templar = Templar(loader=fake_loader) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_network_gather_facts(self): + self.task.action = 'gather_facts' + self.task.async_val = False + self.task.args = {'gather_subset': 'min'} + self.task.module_defaults = [{'ios_facts': {'gather_subset': 'min'}}] + + plugin = ActionModule(self.task, self.connection, self.play_context, loader=None, templar=self.templar, shared_loader_obj=None) + plugin._execute_module = MagicMock() + + res = plugin.run(task_vars=self.task_vars) + self.assertEqual(res['ansible_facts']['_ansible_facts_gathered'], True) + + mod_args = plugin._get_module_args('ios_facts', task_vars=self.task_vars) + self.assertEqual(mod_args['gather_subset'], 'min') + + facts_modules = C.config.get_config_value('FACTS_MODULES', variables=self.task_vars) + self.assertEqual(facts_modules, ['ios_facts'])