def main():
    argument_spec = cassandra_common_argument_spec()
    argument_spec.update(
        state=dict(required=True, choices=['enabled', 'disabled']))
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    status_cmd = 'statushandoff'
    enable_cmd = 'enablehandoff'
    disable_cmd = 'disablehandoff'
    status_active = ['Hinted handoff is running', 'running']
    status_inactive = ['Hinted handoff is not running', 'not running']

    n = NodeTool3PairCommand(module, status_cmd, enable_cmd, disable_cmd)

    rc = None
    out = ''
    err = ''
    result = {}

    (rc, out, err) = n.status_command()
    out = out.strip()
    if module.debug:
        if out:
            result['stdout'] = out
        if err:
            result['stderr'] = err

    if module.params['state'] == "disabled":

        if rc != 0:
            module.fail_json(name=status_cmd,
                             msg="status command failed",
                             **result)
        if module.check_mode:
            if out in status_active:
                module.exit_json(changed=True, msg="check mode", **result)
            else:
                module.exit_json(changed=False, msg="check mode", **result)
        if out in status_active:
            (rc, out, err) = n.disable_command()
            if module.debug:
                if out:
                    result['stdout'] = out
                if err:
                    result['stderr'] = err
        if rc != 0:
            module.fail_json(name=disable_cmd,
                             msg="disable command failed",
                             **result)
        else:
            result['changed'] = True

    elif module.params['state'] == "enabled":

        if rc != 0:
            module.fail_json(name=status_cmd,
                             msg="status command failed",
                             **result)
        if module.check_mode:
            if out in status_inactive:
                module.exit_json(changed=True, msg="check mode", **result)
            else:
                module.exit_json(changed=False, msg="check mode", **result)
        if out in status_inactive:
            (rc, out, err) = n.enable_command()
            if module.debug:
                if out:
                    result['stdout'] = out
                if err:
                    result['stderr'] = err
        if rc is not None and rc != 0:
            module.fail_json(name=enable_cmd,
                             msg="enable command failed",
                             **result)
        else:
            result['changed'] = True

    module.exit_json(**result)
Ejemplo n.º 2
0
def main():
    argument_spec = cassandra_common_argument_spec()
    argument_spec.update(compact=dict(default=True, type='bool'))
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )

    status_cmd = 'compactionstats'
    enable_cmd = 'compact'
    disable_cmd = 'stop'
    status_inactive = 'pending tasks: 0'

    n = NodeTool3PairCommand(module, status_cmd, enable_cmd, disable_cmd)

    rc = None
    out = ''
    err = ''
    result = {}

    (rc, out, err) = n.status_command()
    out = out.strip()
    if module.params['debug']:
        if out:
            result['stdout'] = out
        if err:
            result['stderr'] = err

    if module.params['compact'] is False:

        if rc != 0:
            module.fail_json(name=status_cmd,
                             msg="{0} command failed".format(status_cmd),
                             **result)
        if module.check_mode:
            if out != status_inactive:
                module.exit_json(changed=True,
                                 msg="compaction stopped (check mode)",
                                 **result)
            else:
                module.exit_json(changed=False,
                                 msg="compaction is not running",
                                 **result)
        if out != status_inactive:
            (rc, out, err) = n.disable_command()
            result['msg'] = "compaction stopped"
            result['changed'] = True
            if module.params['debug']:
                if out:
                    result['stdout'] = out
                if err:
                    result['stderr'] = err
            if rc != 0:
                module.fail_json(name=disable_cmd,
                                 msg="{0} command failed".format(disable_cmd),
                                 **result)
        else:
            result['msg'] = "compaction is not running"
            result['changed'] = False

    elif module.params['compact'] is True:

        if rc != 0:
            module.fail_json(name=status_cmd,
                             msg="{0} command failed".format(status_cmd),
                             **result)
        if module.check_mode:
            if out == status_inactive:
                module.exit_json(changed=True,
                                 msg="compaction started (check mode)",
                                 **result)
            else:
                module.exit_json(changed=False,
                                 msg="compaction is already running",
                                 **result)
        if out == status_inactive:
            (rc, out, err) = n.enable_command()
            result['msg'] = "compaction started"
            result['changed'] = True
            if module.params['debug']:
                if out:
                    result['stdout'] = out
                if err:
                    result['stderr'] = err
            if rc is not None and rc != 0:
                module.fail_json(name=enable_cmd,
                                 msg="{0} command failed".format(enable_cmd),
                                 **result)
        else:
            result['msg'] = "compaction is already running"
            result['changed'] = False

    module.exit_json(**result)