Skip to content
Snippets Groups Projects
Commit 4f131462 authored by jurgenhaas's avatar jurgenhaas
Browse files

Adjust plugin for Ansible version 2

parent a6cf58d6
No related branches found
No related tags found
No related merge requests found
import json import json
import requests import requests
from ansible.callbacks import vv
from ansible.errors import AnsibleError as ae from ansible.errors import AnsibleError as ae
from ansible.runner.return_data import ReturnData from ansible.plugins.action import ActionBase
from ansible.utils import parse_kv try:
from __main__ import display
class ActionModule(object): except ImportError:
from ansible.utils.display import Display
display = Display()
class ActionModule(ActionBase):
''' Manage hosts and infrastructure at JiffyBox''' ''' Manage hosts and infrastructure at JiffyBox'''
### We need to be able to modify the inventory ### We need to be able to modify the inventory
BYPASS_HOST_LOOP = True BYPASS_HOST_LOOP = True
TRANSFERS_FILES = False TRANSFERS_FILES = False
def __init__(self, runner): def __init__(self, task, connection, play_context, loader, templar,
self.runner = runner shared_loader_obj):
super(ActionModule, self).__init__(task, connection, play_context,
loader, templar, shared_loader_obj)
self.api_token = None self.api_token = None
self.url = None self.url = None
self.args = {} self.args = {}
self.vars = {}
self.devices = [] self.devices = []
self.backups = [] self.backups = []
...@@ -28,22 +34,24 @@ class ActionModule(object): ...@@ -28,22 +34,24 @@ class ActionModule(object):
self.contacts = [] self.contacts = []
self.documents = [] self.documents = []
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): def run(self, tmp=None, task_vars=None):
''' handler for file transfer operations '''
if task_vars is None:
task_vars = dict()
self.vars = task_vars
if self.runner.noop_on_check(inject): result = super(ActionModule, self).run(tmp, task_vars)
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module'))
if complex_args: if self._play_context.check_mode:
self.args.update(complex_args) result['skipped'] = True
self.args.update(parse_kv(module_args)) result['msg'] = 'check mode not supported for this module'
return result
self.api_token = self._get_var('jiffy_api_token', self.args.get('api_token', False)) self.api_token = self._task.args.get('api_token', task_vars.get('jiffy_api_token', False))
if not self.api_token: if not self.api_token:
raise ae("'api_token' is a required argument or you define the variable 'jiffy_api_token' in your inventory.") raise ae("'api_token' is a required argument or you define the variable 'jiffy_api_token' in your inventory.")
action = self.args.get('action', 'list') action = self._task.args.get('action', 'list')
result = {}
if action == 'list': if action == 'list':
self.list_devices() self.list_devices()
...@@ -52,16 +60,7 @@ class ActionModule(object): ...@@ -52,16 +60,7 @@ class ActionModule(object):
else: else:
raise ae("'%s' is an unknown action" % action) raise ae("'%s' is an unknown action" % action)
return ReturnData(conn=conn, comm_ok=True, result=result) result['changed'] = False
def _get_var(self, key, default):
firsthost = self.runner.host_set[0]
variables = self.runner.inventory.get_variables(firsthost)
if not variables.has_key(key):
self.runner.inventory._vars_per_host[firsthost].__setitem__(key, default)
result = default
else:
result = variables.get(key)
return result return result
def _request(self, path, data = None, method = 'GET'): def _request(self, path, data = None, method = 'GET'):
...@@ -102,17 +101,12 @@ class ActionModule(object): ...@@ -102,17 +101,12 @@ class ActionModule(object):
return content['result'] return content['result']
def _load_objects(self, type, path): def _load_objects(self, type, path):
vv("Reading %s from JiffyBox" % type) display.vv("Reading %s from JiffyBox" % type)
changed = False changed = False
allgroup = self.runner.inventory.get_group('all') if not self.vars.has_key('_jiffybox_' + type):
allvariables = allgroup.get_variables()
if not allvariables.has_key('_jiffybox_' + type):
changed = True changed = True
objects = self._request(path) self.vars.set('_jiffybox_' + type, self._request(path))
allgroup.set_variable('_jiffybox_' + type, objects) return changed, self.vars.get('_jiffybox_' + type)
else:
objects = allvariables.get('_jiffybox_' + type)
return changed, objects
def load_devices(self): def load_devices(self):
if len(self.devices) == 0: if len(self.devices) == 0:
...@@ -169,7 +163,7 @@ class ActionModule(object): ...@@ -169,7 +163,7 @@ class ActionModule(object):
id = str(self.devices[device].get('id')) id = str(self.devices[device].get('id'))
name = self.devices[device].get('name') name = self.devices[device].get('name')
f_ips = [] f_ips = []
if name in self.runner.host_set: if name in self.vars.get('ansible_play_hosts'):
for address in self.devices[device].get('ips').get('public'): for address in self.devices[device].get('ips').get('public'):
ip = self.find_ip(address) ip = self.find_ip(address)
if ip: if ip:
...@@ -198,29 +192,29 @@ class ActionModule(object): ...@@ -198,29 +192,29 @@ class ActionModule(object):
return '' return ''
def move_ip(self): def move_ip(self):
vv("Moving IP ...") display.vv("Moving IP ...")
target = self.args.get('target', False) target = self.args.get('target', False)
if not target: if not target:
raise ae("'target' is a required argument.") raise ae("'target' is a required argument.")
targethost = self.find_host(target) targethost = self.find_host(target)
if not targethost: if not targethost:
raise ae("Target host %s unknown in this JiffyBox account." % target) raise ae("Target host %s unknown in this JiffyBox account." % target)
vv("- target host: %s" % target) display.vv("- target host: %s" % target)
ips = self.find_floating_ip() ips = self.find_floating_ip()
for ip in ips: for ip in ips:
if not ip: if not ip:
raise ae('No ip defined for moving.') raise ae('No ip defined for moving.')
vv("- floating IP: %s" % ip.get('ip')) display.vv("- floating IP: %s" % ip.get('ip'))
sourcehost = self.find_host_by_ip(ip) sourcehost = self.find_host_by_ip(ip)
if not sourcehost: if not sourcehost:
vv('Current host for the floating ip can not be found.') display.vv('Current host for the floating ip can not be found.')
else: else:
vv("- source host: %s" % sourcehost.get('name')) display.vv("- source host: %s" % sourcehost.get('name'))
if sourcehost.get('id') == targethost.get('id'): if sourcehost.get('id') == targethost.get('id'):
vv('- cancelled as source and target are the same') display.vv('- cancelled as source and target are the same')
else: else:
path = 'ips/' + str(sourcehost.get('id')) + '/' + str(ip.get('id')) + '/move' path = 'ips/' + str(sourcehost.get('id')) + '/' + str(ip.get('id')) + '/move'
self._request(path, {'targetid': targethost.get('id')}) self._request(path, {'targetid': targethost.get('id')})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment