Created
April 28, 2017 08:35
-
-
Save sm4rk0/276b1fdb529fab8d928b7c100fb4f0d8 to your computer and use it in GitHub Desktop.
jboss_cli ansible module forked from https://github.com/ansible/ansible-modules-extras/pull/1862
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright (C) 2016, Roland Wolters <rwolters@redhat.com> | |
# | |
# This program 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. | |
# | |
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# Forked from https://github.com/ansible/ansible-modules-extras/pull/1862/files | |
DOCUMENTATION = ''' | |
--- | |
module: jboss_cli | |
version_added: "2.3" | |
author: "Roland Wolters (@liquidat)" | |
short_description: Manage JBoss via jboss_cli | |
requirements: | |
- jboss_cli | |
description: | |
- This module provides JBoss management via jboss_cli for JBoss application servers. | |
options: | |
user: | |
description: | |
- The name of the management user. | |
required: false | |
default: None | |
password: | |
description: | |
- The password to assign to the username. | |
required: false | |
default: None | |
host: | |
description: | |
- The host running JBoss. | |
required: false | |
default: '127.0.0.1' | |
port: | |
description: | |
- The port of the associated host running JBoss. | |
required: false | |
default: '9999' | |
jbosshome: | |
description: | |
- The JBoss path environment variable. | |
required: false | |
default: '/usr/share/jbossas/' | |
commands: | |
description: | |
- The list of commands to manage the JBoss server. | |
required: true | |
''' | |
EXAMPLES = ''' | |
Example playbook entries using the jboss_cli module to manage JBoss. | |
tasks: | |
- name: set max-threads to a given value | |
jboss_cli: command="/subsystem=ejb3/thread-pool=default:write-attribute(name=max-threads,value=3000)" | |
- name: get all configuration and runtime details | |
jboss_cli: | |
command: ":read-resource(include-runtime=true, recursive=true, recursive-depth=10)" | |
- name: deploy a given war | |
jboss_cli: | |
command: "deploy /path-up-to/your-app.war" | |
''' | |
RETURN = ''' | |
msg: | |
description: execution results from the jboss-cli | |
returned: success, failure | |
type: string | |
sample: outcome => success | |
''' | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
commands = dict(required=True, type='list', default=None), | |
user = dict(required=False, type='str', default=None), | |
password = dict(required=False, type='str', default=None, no_log=True), | |
host = dict(required=False, type='str', default='127.0.0.1'), | |
port = dict(required=False, type='str', default='9999'), | |
jbosshome = dict(required=False, type='str', default='/usr/share/jbossas/'), | |
), | |
) | |
commands = module.params['commands'] | |
user = module.params.get('user') | |
password = module.params.get('password') | |
host = module.params.get('host') | |
port = module.params.get('port') | |
jbosshome = module.params.get('jbosshome') | |
# TODO: Add support for --force | |
# TODO: Add support for --file | |
# TODO: Add support for --properties | |
# TODO: Add support for --server-groups | |
if os.path.isdir(jbosshome): | |
jbosscli_bin = module.get_bin_path('jboss-cli.sh', True, [jbosshome + '/bin/']) | |
else: | |
module.fail_json(msg='The path ' + jbosshome + ' is not a valid system dir.') | |
cmd = jbosscli_bin | |
cmd += ' --connect' | |
if user and password: | |
cmd+= ' --user=' + user | |
cmd += ' --password=' + password | |
cmd += ' --controller=' + host + ':' + port | |
cmd += ' --commands="' + ','.join(commands) + '"' | |
try: | |
rc, stdout, stderr = module.run_command(cmd) | |
stdout_jsonized = stdout.replace("=>", ":") | |
try: | |
json_result = json.loads(stdout_jsonized) | |
except ValueError: | |
json_result = None | |
if rc != 0: | |
module.fail_json(msg="failure running jboss-cli", stderr=stderr, stdout=stdout, rc=rc, json_result=json_result, cmd=cmd) | |
else: | |
module.exit_json(rc=rc, stdout=stdout, json_result=json_result, changed=True) | |
except subprocess.CalledProcessError: | |
cpe = get_exception() | |
module.fail_json(msg=str(dir(cpe))) | |
# import module snippets | |
from ansible.module_utils.basic import * | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment