From d8459425a01ec07304946d73edeb2035c5166de1 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 7 Nov 2012 23:18:28 +0100 Subject: [PATCH] Turn 'fail' and 'debug' modules into action_plugins This makes more sense as there is no purpose in running these remotely. --- lib/ansible/runner/action_plugins/debug.py | 39 ++++++++++++++++++++++ lib/ansible/runner/action_plugins/fail.py | 35 +++++++++++++++++++ library/debug | 37 ++------------------ library/fail | 25 ++------------ 4 files changed, 79 insertions(+), 57 deletions(-) create mode 100644 lib/ansible/runner/action_plugins/debug.py create mode 100644 lib/ansible/runner/action_plugins/fail.py diff --git a/lib/ansible/runner/action_plugins/debug.py b/lib/ansible/runner/action_plugins/debug.py new file mode 100644 index 00000000000..5f862add827 --- /dev/null +++ b/lib/ansible/runner/action_plugins/debug.py @@ -0,0 +1,39 @@ +# Copyright 2012, Dag Wieers +# +# 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 . + +import ansible + +from ansible import utils +from ansible.runner.return_data import ReturnData + +class ActionModule(object): + ''' Print statements during execution ''' + + def __init__(self, runner): + self.runner = runner + + def run(self, conn, tmp, module_name, module_args, inject): + args = utils.parse_kv(module_args) + if not 'msg' in args: + args['msg'] = 'Hello world!' + + if 'fail' in args and utils.boolean(args['fail']): + result = dict(failed=True, msg=args['msg']) + else: + result = dict(msg=str(args['msg'])) + + return ReturnData(conn=conn, result=result) diff --git a/lib/ansible/runner/action_plugins/fail.py b/lib/ansible/runner/action_plugins/fail.py new file mode 100644 index 00000000000..c28c1bb2b0e --- /dev/null +++ b/lib/ansible/runner/action_plugins/fail.py @@ -0,0 +1,35 @@ +# Copyright 2012, Dag Wieers +# +# 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 . + +import ansible + +from ansible import utils +from ansible.runner.return_data import ReturnData + +class ActionModule(object): + ''' Fail with custom message ''' + + def __init__(self, runner): + self.runner = runner + + def run(self, conn, tmp, module_name, module_args, inject): + args = utils.parse_kv(module_args) + if not 'msg' in args: + args['msg'] = 'Failed as requested from task' + + result = dict(failed=True, msg=args['msg']) + return ReturnData(conn=conn, result=result) diff --git a/library/debug b/library/debug index b36668eba84..acd7efd2c53 100644 --- a/library/debug +++ b/library/debug @@ -37,11 +37,6 @@ options: message. required: false default: "Hello world!" - rc: - description: - - The return code of the module. If fail=yes, this will default to 1. - required: false - default: 0 fail: description: - A boolean that indicates whether the debug module should fail or not. @@ -49,37 +44,11 @@ options: default: "no" examples: - code: | - - local_action: debug msg="System $inventory_hostname has uuid $ansible_product_uuid" - - local_action: debug msg="System $inventory_hostname lacks a gateway" fail=yes + - action: debug msg="System $inventory_hostname has uuid $ansible_product_uuid" + - action: debug msg="System $inventory_hostname lacks a gateway" fail=yes only_if: "is_unset('${ansible_default_ipv4.gateway}')" - - local_action: debug msg="System $inventory_hostname has gateway ${ansible_default_ipv4.gateway}" + - action: debug msg="System $inventory_hostname has gateway ${ansible_default_ipv4.gateway}" only_if: "is_set('${ansible_default_ipv4.gateway}')" description: "Example that prints the loopback address and gateway for each host" author: Dag Wieers ''' - -def main(): - - module = AnsibleModule( - argument_spec = dict( - fail = dict(default='no', choices=BOOLEANS), - msg = dict(default='Hello world!'), - rc = dict(default=0), - ) - ) - - fail = module.boolean(module.params.get('fail')) - msg = module.params.get('msg') - rc = module.params.get('rc') - - if fail and rc == 0: - rc = 1 - - if fail: - module.fail_json(rc=rc, msg=msg) - else: - module.exit_json(msg=msg) - -# this is magic, see lib/ansible/module_common.py -#<> -main() diff --git a/library/fail b/library/fail index 0a81aa02cd7..c6fbe603a54 100644 --- a/library/fail +++ b/library/fail @@ -32,34 +32,13 @@ options: - The customized message used for failing execution. If ommited, fail will simple bail out with a generic message. required: false - default: "'Failed because only_if condition is true'" - rc: - description: - - The return code of the failure. This is currently not used by - Ansible, but might be used in the future. - required: false - default: 1 + default: "'Failed as requested from task'" examples: - code: | - fail: msg="The system may not be provisioned according to the CMDB status." rc=100 + fail: msg="The system may not be provisioned according to the CMDB status." only_if: "'$cmdb_status' != 'to-be-staged'" description: "Example playbook using fail and only_if together" author: Dag Wieers ''' - -def main(): - - module = AnsibleModule( - argument_spec = dict( - msg = dict(default='Failed because only_if condition is true'), - rc = dict(default=1), - ) - ) - - module.fail_json(rc=module.params['rc'], msg=module.params['msg']) - -# this is magic, see lib/ansible/module_common.py -#<> -main()