1. Input / Outputs
  2. Formatting
  3. Compatibility across various OS
  4. Input / Output Validation & Verification
  5. No Need to create/manage SSH or WINRM or API Sessions
# -*- coding: utf-8 -*-

# Copyright 2021 Davinder Pal <>

from __future__ import absolute_import, division, print_function
__metaclass__ = type
# Continue Part 1....DOCUMENTATION = '''
module: mapr_service
version_added: 0.0.1
author: "Davinder Pal (@116davinder)"
short_description: Manage MapR Services by rest api.
- Manage MapR Services
- username for MapR MCS
required: true
type: string

- mapr_service:
username: mapr
# Continue Part 2....
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url

def main():

module = AnsibleModule(
username=dict(type='str', required=True),
password=dict(type='str', required=True, no_log=True),
service_name=dict(type='str', required=True),
mcs_url=dict(type='str', required=True),
mcs_port=dict(type='str', default='8443', required=False),
state=dict(type='str', required=True),
validate_certs=dict(type='bool', default='False'),
# Continue Part 3 .... def get_current_hostname():
cmd = module.get_bin_path('hostname', True)
rc, out, err = module.run_command(cmd)
if rc != 0:
msg="Command failed rc=%d, out=%s, err=%s" % (rc, out, err))
return out.strip()
# Continue Part 4....     maprUsername = module.params['username']
maprPassword = module.params['password']
serviceName = module.params['service_name'].lower()
serviceState = module.params['state'].lower()
mcsUrl = module.params['mcs_url']
mcsPort = module.params['mcs_port']
mapr_default_service_state = ['start', 'stop', 'restart']
# Hack to add basic auth username and password the way fetch_url expects
module.params['url_username'] = maprUsername
module.params['url_password'] = maprPassword
if not maprUsername or not maprPassword:
module.fail_json(msg="Username and Password should be defined")
elif not serviceName or not serviceState:
module.fail_json(msg="Service Name & Service State should be defined")
elif not mcsUrl:
module.fail_json(msg="MCS Url Should be Defined")
elif serviceState not in mapr_default_service_state:
module.fail_json(msg="state should be start/stop/restart only")
# Continue Part 5 ....        host = get_current_hostname()
url_parameters = "?action=" + serviceState + "&nodes=" + \
str(host) + "&name=" + serviceName
complete_url = "https://" + mcsUrl + ":" + mcsPort + \
"/rest/node/services" + url_parameters
headers = {'Content-Type': 'application/json'}
(resp, info) = fetch_url(module,
if info['status'] >= 400:
module.fail_json(msg="Unauthorized Access to MapR Services")
elif info['status'] == 200:
body = json.loads(
if body['status'] == 'ERROR':
msg="Unknown Response from MapR API: %s" %
# Continue Part 6....if __name__ == '__main__':
- mapr_service:
username: mapr
password: mapr
service_name: nfs
mcs_port: 8443
state: restart
validate_certs: false




Senior Software Engineer III ( R&D )

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrate WhatsApp Into an App with Maytapi API [JavaScript]

This is a test post.

How to stop expired Windows Server auto-shutdown every hour

THM Sysmon Room walkthrough

Automating a Custom Jenkins in OpenShift 3.x with Your Plugins and Your Configuration

Download ilok

Implement Your Own Operating System (week 04)

DevOps / SRE — Top Links Last Week

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Davinder Pal

Davinder Pal

Senior Software Engineer III ( R&D )

More from Medium

Detect unused secrets in your Github repositories

How I passed the PCEP Certification

Create a Simple Python Web Server on OCI

CherryPy- A Python Web Framework