2015-06-09 00:20:07 +00:00
|
|
|
# (c) 2015, Ansible Inc,
|
2015-06-09 00:10:45 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
from ansible.plugins.action import ActionBase
|
|
|
|
|
2015-11-11 16:29:37 +00:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
2015-06-09 00:10:45 +00:00
|
|
|
class ActionModule(ActionBase):
|
|
|
|
|
2015-07-19 22:00:27 +00:00
|
|
|
TRANSFERS_FILES = False
|
2015-06-09 00:10:45 +00:00
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
def run(self, tmp=None, task_vars=None):
|
2015-06-09 00:10:45 +00:00
|
|
|
''' handler for package operations '''
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
self._supports_check_mode = True
|
|
|
|
self._supports_async = True
|
2015-10-22 23:07:26 +00:00
|
|
|
|
|
|
|
result = super(ActionModule, self).run(tmp, task_vars)
|
2015-06-09 00:10:45 +00:00
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
if result.get('skipped', False):
|
|
|
|
return result
|
|
|
|
|
2015-07-16 20:44:33 +00:00
|
|
|
module = self._task.args.get('use', 'auto')
|
2015-06-09 00:10:45 +00:00
|
|
|
|
2015-07-16 20:44:33 +00:00
|
|
|
if module == 'auto':
|
2015-06-09 00:10:45 +00:00
|
|
|
try:
|
2016-09-09 21:26:47 +00:00
|
|
|
if self._task.delegate_to: # if we delegate, we should use delegated host's facts
|
|
|
|
module = self._templar.template("{{hostvars['%s']['ansible_pkg_mgr']}}" % self._task.delegate_to)
|
|
|
|
else:
|
|
|
|
module = self._templar.template('{{ansible_pkg_mgr}}')
|
2015-06-09 00:10:45 +00:00
|
|
|
except:
|
|
|
|
pass # could not get it from template!
|
|
|
|
|
2015-07-16 20:44:33 +00:00
|
|
|
if module == 'auto':
|
2016-06-03 13:16:07 +00:00
|
|
|
facts = self._execute_module(module_name='setup', module_args=dict(filter='ansible_pkg_mgr', gather_subset='!all'), task_vars=task_vars)
|
2015-11-11 16:29:37 +00:00
|
|
|
display.debug("Facts %s" % facts)
|
2017-01-27 23:45:23 +00:00
|
|
|
if 'ansible_facts' in facts and 'ansible_pkg_mgr' in facts['ansible_facts']:
|
2015-08-15 15:29:10 +00:00
|
|
|
module = getattr(facts['ansible_facts'], 'ansible_pkg_mgr', 'auto')
|
2015-06-09 00:10:45 +00:00
|
|
|
|
2015-07-16 20:44:33 +00:00
|
|
|
if module != 'auto':
|
2015-08-31 17:25:07 +00:00
|
|
|
|
|
|
|
if module not in self._shared_loader_obj.module_loader:
|
2015-10-22 23:07:26 +00:00
|
|
|
result['failed'] = True
|
|
|
|
result['msg'] = 'Could not find a module for %s.' % module
|
2016-12-14 17:52:18 +00:00
|
|
|
else:
|
|
|
|
# run the 'package' module
|
|
|
|
new_module_args = self._task.args.copy()
|
|
|
|
if 'use' in new_module_args:
|
|
|
|
del new_module_args['use']
|
2015-06-09 00:10:45 +00:00
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
display.vvvv("Running %s" % module)
|
|
|
|
result.update(self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async))
|
2015-06-09 00:10:45 +00:00
|
|
|
else:
|
2015-10-22 23:07:26 +00:00
|
|
|
result['failed'] = True
|
|
|
|
result['msg'] = 'Could not detect which package manager to use. Try gathering facts or setting the "use" option.'
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
return result
|