2014-11-14 22:14:08 +00:00
|
|
|
# Copyright 2012, Dag Wieers <dag@wieers.com>
|
2016-08-24 17:37:15 +00:00
|
|
|
# Copyright 2016, Toshio Kuratomi <tkuratomi@ansible.com>
|
2014-11-14 22:14:08 +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/>.
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-08-24 17:37:15 +00:00
|
|
|
from ansible.errors import AnsibleUndefinedVariable
|
2017-03-23 20:35:05 +00:00
|
|
|
from ansible.module_utils.six import string_types
|
2016-09-07 05:54:17 +00:00
|
|
|
from ansible.module_utils._text import to_text
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.plugins.action import ActionBase
|
2016-08-24 17:37:15 +00:00
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
class ActionModule(ActionBase):
|
|
|
|
''' Print statements during execution '''
|
|
|
|
|
|
|
|
TRANSFERS_FILES = False
|
2018-08-30 13:40:36 +00:00
|
|
|
_VALID_ARGS = frozenset(('msg', 'var', 'verbosity'))
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
if task_vars is None:
|
|
|
|
task_vars = dict()
|
|
|
|
|
2015-12-13 05:13:13 +00:00
|
|
|
if 'msg' in self._task.args and 'var' in self._task.args:
|
|
|
|
return {"failed": True, "msg": "'msg' and 'var' are incompatible options"}
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
result = super(ActionModule, self).run(tmp, task_vars)
|
2018-02-07 23:11:36 +00:00
|
|
|
del tmp # tmp no longer has any effect
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-02-18 14:58:43 +00:00
|
|
|
# get task verbosity
|
2017-11-02 22:11:31 +00:00
|
|
|
verbosity = int(self._task.args.get('verbosity', 0))
|
2015-12-13 05:13:13 +00:00
|
|
|
|
2016-02-18 14:58:43 +00:00
|
|
|
if verbosity <= self._display.verbosity:
|
|
|
|
if 'msg' in self._task.args:
|
|
|
|
result['msg'] = self._task.args['msg']
|
2015-12-13 05:13:13 +00:00
|
|
|
|
2016-02-18 14:58:43 +00:00
|
|
|
elif 'var' in self._task.args:
|
|
|
|
try:
|
2018-08-23 16:31:16 +00:00
|
|
|
results = self._templar.template(self._task.args['var'], convert_bare=True, fail_on_undefined=True)
|
2016-02-18 14:58:43 +00:00
|
|
|
if results == self._task.args['var']:
|
2016-05-01 13:42:09 +00:00
|
|
|
# if results is not str/unicode type, raise an exception
|
2016-08-24 17:37:15 +00:00
|
|
|
if not isinstance(results, string_types):
|
2016-05-01 13:42:09 +00:00
|
|
|
raise AnsibleUndefinedVariable
|
|
|
|
# If var name is same as result, try to template it
|
|
|
|
results = self._templar.template("{{" + results + "}}", convert_bare=True, fail_on_undefined=True)
|
2017-11-02 22:11:31 +00:00
|
|
|
except AnsibleUndefinedVariable as e:
|
|
|
|
results = u"VARIABLE IS NOT DEFINED!"
|
|
|
|
if self._display.verbosity > 0:
|
|
|
|
results += u": %s" % to_text(e)
|
2016-02-18 14:58:43 +00:00
|
|
|
|
2016-08-24 17:37:15 +00:00
|
|
|
if isinstance(self._task.args['var'], (list, dict)):
|
2016-02-18 14:58:43 +00:00
|
|
|
# If var is a list or dict, use the type as key to display
|
2016-09-07 05:54:17 +00:00
|
|
|
result[to_text(type(self._task.args['var']))] = results
|
2016-02-18 14:58:43 +00:00
|
|
|
else:
|
|
|
|
result[self._task.args['var']] = results
|
2015-11-23 15:58:24 +00:00
|
|
|
else:
|
2016-02-18 14:58:43 +00:00
|
|
|
result['msg'] = 'Hello world!'
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-02-18 14:58:43 +00:00
|
|
|
# force flag to make debug output module always verbose
|
|
|
|
result['_ansible_verbose_always'] = True
|
|
|
|
else:
|
2016-06-28 19:24:18 +00:00
|
|
|
result['skipped_reason'] = "Verbosity threshold not met."
|
2016-02-18 14:58:43 +00:00
|
|
|
result['skipped'] = True
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2019-02-19 20:24:11 +00:00
|
|
|
result['failed'] = False
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
return result
|