def get_configs(organization, git_url): host = get_host() script_path = os.path.join(os.path.abspath(__file__), 'scripts/') git_dir = os.path.join('/opt/', git_url.split('/')[-1]) # for loop at bottom of function calls this function... # this will get the config files, we still need to get the playbooks def get_config(organization, config_path, service_name): base_name = os.path.basename(config_path) script_call = "{0}/redis_push_config.py \ --config={1} \ --service={2} \ --organization={3}\ --rhost={4} \ --rpass={5}".format(script_path, config_path, organization, service_name, redis_host, redis_auth) r = runner.Runner(transport='paramiko', pattern=host, module_name='script', module_args=script_call) return r.run() # checkout git project r = runner.Runner(transport='paramiko', pattern=host, module_name='git', module_args='repo={0} dest={1}'.format(git_url, git_dir)) r.run() # install python redis client so we can push configuration to redis # we'll pick up the configuration afterwards in order to configure the machine r = runner.Runner(transport='paramiko', pattern=host, module_name='command', module_args='easy_install redis') r.run() # find all app_settings file so we can iterate over them and parse. r = runner.Runner(transport='paramiko', pattern=host, module_name='shell', module_args='find {0} -name app_settings.json'.format(git_dir)) results = r.run() configs = results.get('contacted').get(host).get('stdout') for config in configs.splitlines(): base_name = os.path.basename(config) service_name = os.path.dirname(config).split('/')[-1] get_config(organization, config, service_name)
def addLineInFile(self, fqdn, dest, line, regexp): hostname = self.getHostname(fqdn) # assemble args args = 'dest=' + dest + ' ' args += 'line="' + line + '" ' args += 'regexp="' + regexp + '" ' args += 'state=present ' runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_name='lineinfile', module_args=args, inventory=self.inventory) results = runner.run() if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log('FATAL: can\'t find ' + hostname + ' in results') return False host = contacted[hostname] if 'changed' not in host: self.log('FATAL: unable to find \'changed\' in results') return False if not host['changed']: self.log('line was already present') return True
def destroyDirectory(self, fqdn, dirname): hostname = self.getHostname(fqdn) args = 'path=' + dirname + ' ' args += 'state=absent ' runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_name='file', module_args=args, inventory=self.inventory) results = runner.run() if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log('FATAL: can\'t find ' + unicode(hostname) + ' in results') return False host = contacted[hostname] if 'state' not in host: self.log('FATAL: can\'t find \'state\' in host') self.log(unicode(host)) return False state = host['state'] if state != 'absent': return False return True
def shell(self, fqdn, command): hostname = self.getHostname(fqdn) args = command runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_name='shell', module_args=args, inventory=self.inventory) results = runner.run() if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log('FATAL: can\'t find ' + hostname + ' in results') return False host = contacted[hostname] res = {} res['rc'] = host['rc'] res['stdout'] = host['stdout'] res['stderr'] = host['stderr'] return res
def twikiUserInactive(self, fqdn, twikibase, twikiname): hostname = self.getHostname(fqdn) args = u'user='******' ' args += u'twikibase=' + unicode(twikibase) + u' ' args += u'state=inactive' import os.path path = os.path.dirname(__file__) runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_path=path, module_name='twiki-user.py', module_args=args, inventory=self.inventory) results = runner.run() #self.log(unicode(results)) if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log(str(results)) self.log('FATAL: can\'t find ' + unicode(hostname) + ' in results') return False host = contacted[hostname] if 'failed' in host: self.log('Failed deactivating user ' + unicode(twikiname) + ' ' + host['msg']) return False # if we are here, all is ok... return True
def data(): if not session: return redirect("/") #res={k:v[0] for k,v in dict(request.get).itmes()} module = request.args.get('module') cmd = request.args.get('cmd') pattern = request.args.get('pattern') forks = request.args.get('forks') result = [] data = a.Runner(module_name=module, module_args=cmd, pattern=pattern, forks=forks).run() times = time.strftime('%Y-%m-%d %H:%M') log = "%s-%s-%s-%s\n" % (times, session['username'], pattern, cmd) print log with open('/tmp/ansible.log', 'a+') as f: f.write(log) #print data['contacted'] for k, v in data['contacted'].items(): time1 = v['end'] name = session['username'] cmd = v['cmd'] stdout = v['stdout'] status = v['changed'] result.append({ 'hostname': k, 'time': time1, 'name': name, 'cmd': cmd, 'status': status, 'stdout': stdout }) return json.dumps(result)
def modrun(inventory, mod, args): result = runner.Runner( pattern=inventory, module_name=mod, module_args=args, ).run() print result return result
def get_configs(redis_host, redis_pass, host, script_path, git_url, git_dir): host = host script_path = script_path # for loop at bottom of function calls this function... def get_config(config_path, service_name): base_name = os.path.basename(config_path) script_call = "{0}/redis_push_config.py \ --config={1} \ --service={2} \ --rhost={3} \ --rpass={4}".format(script_path, config_path, service_name, redis_host, redis_pass) r = runner.Runner(transport='paramiko', pattern=host, host_list='{0}/inventory'.format(script_path), module_name='script', module_args=script_call) return r.run() r = runner.Runner(transport='paramiko', pattern=host, host_list='{0}/inventory'.format(script_path), module_name='git', module_args='repo={0} dest={1}'.format(git_url, git_dir)) r.run() r = runner.Runner( transport='paramiko', pattern=host, host_list='{0}/inventory'.format(script_path), module_name='shell', module_args='find {0} -name app_settings.json'.format(git_dir)) results = r.run() configs = results.get('contacted').get(host).get('stdout') services = {} for config in configs.splitlines(): base_name = os.path.basename(config) service_name = os.path.dirname(config).split('/')[-1] get_config(config, service_name)
def hostname(): if not session: return redirect("/") result = [] data = a.Runner(module_name='shell', module_args='hostname', pattern='all', forks=5).run() for i in data['contacted']: result.append(i) return json.dumps(result)
def _mariadb_check(self): module_args = "mysql -u{} -p{} -e \"show databases;\"" \ "| grep nova".format(self.db_user, self.db_password) for host_name in self.inventory.keys(): host_data = self.inventory[host_name] results = ansible_runner.Runner( host_list=[host_data["ip_or_hostname"]], remote_user=host_data["username"], remote_pass=host_data["password"], module_args=module_args, module_name="shell", forks=2).run() LOGGER.debug(results)
def get_config(organization, config_path, service_name): base_name = os.path.basename(config_path) script_call = "{0}/redis_push_config.py \ --config={1} \ --service={2} \ --organization={3}\ --rhost={4} \ --rpass={5}".format(script_path, config_path, organization, service_name, redis_host, redis_auth) r = runner.Runner(transport='paramiko', pattern=host, module_name='script', module_args=script_call) return r.run()
def copy(self, fqdn, uid, gid, src, dest, modes): ansible.utils.VERBOSITY = 4 hostname = self.getHostname(fqdn) # assemble args args = 'src=' + src + ' ' args += 'dest=' + dest + ' ' args += 'owner=' + str(uid) + ' ' args += 'group=' + str(gid) + ' ' args += 'mode=' + modes + ' ' runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_name='copy', module_args=args, inventory=self.inventory) results = runner.run() if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log('FATAL: can\'t find ' + hostname + ' in results') # extract message if 'dark' in results: dark = results['dark'] if hostname in dark: host = dark[hostname] if 'msg' in host: msg = host['msg'] self.log(msg) return False host = contacted[hostname] if 'changed' not in host: if 'msg' in host: self.log(host['msg']) self.log('FATAL: unable to find \'changed\' in results') return False if host['changed']: #self.log ('File copied over') pass else: self.log('Destination file is identical to source') return True
def get_config(config_path, service_name): base_name = os.path.basename(config_path) script_call = "{0}/redis_push_config.py \ --config={1} \ --service={2} \ --rhost={3} \ --rpass={4}".format(script_path, config_path, service_name, redis_host, redis_pass) r = runner.Runner(transport='paramiko', pattern=host, host_list='{0}/inventory'.format(script_path), module_name='script', module_args=script_call) return r.run()
def fetch(self, fqdn, src, dest, fail_on_missing=True): hostname = self.getHostname(fqdn) #assemble args args = 'src=' + src + ' ' args += 'dest=' + dest + ' ' args += 'fail_on_missing=' + ('yes' if fail_on_missing else 'no') runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_name='fetch', module_args=args, inventory=self.inventory) results = runner.run() if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log('FATAL: can\'t find ' + hostname + ' in results') if 'dark' in results: dark = results['dark'] if hostname in dark: host = dark[hostname] if 'msg' in host: msg = host['msg'] self.log(msg) return False host = contacted[hostname] if 'changed' not in host: if 'msg' in host: self.log(host['msg']) self.log('FATAL: unable to find \'changed\' in results') return False if host['changed']: pass else: self.log('Source file is identical to destination') return True
def get_info(): ass_list = [] runner = arun.Runner(module_name='setup', module_args='', pattern='%s' % gro, forks=10) results = runner.run() INFO = results['contacted'] for host, val in INFO.items(): for i in val['ansible_facts']["ansible_devices"]: if i[0:2] not in ("sr"): dis = str(i + '_' + val['ansible_facts']['ansible_devices'][i]['size']) ass = host, str(val['ansible_facts']["ansible_product_name"]), str( val['ansible_facts']["ansible_distribution"]) + "_" + str( val['ansible_facts']["ansible_distribution_version"]), str( val['ansible_facts']['ansible_processor'][0] ), str(val['ansible_facts']["ansible_processor_count"]), str( val['ansible_facts']["ansible_processor_vcpus"] ), str(val['ansible_facts']["ansible_memtotal_mb"]), str( val['ansible_facts']['ansible_swaptotal_mb']), dis, str( val['ansible_facts']['ansible_default_ipv4'] ['address']), str( val['ansible_facts']['ansible_default_ipv4'] ['macaddress']), str( val['ansible_facts']['ansible_product_serial']) ass_list.append(ass) #for i in ass_list: # print i Msql = "INSERT INTO `ansible_host` (`hostname`,`manufacturer`,`os`,`cpu_model`,`cpu_count`,`cpu_core`,`memory_totally`,`swap_totally`,`disk`,`ip`,`mac_address`,`sn`) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" try: gobal_fun.cur.executemany(Msql, ass_list) gobal_fun.conn.commit() gobal_fun.cur.close() gobal_fun.conn.close() print "success insert " except Exceptioin, e: conn.rollback() print e
def createDirectory(self, fqdn, dirname, uid, gid, modes): hostname = self.getHostname(fqdn) # assemble args args = 'path=' + dirname + ' ' args += 'state=directory ' args += 'owner=' + str(uid) + ' ' args += 'group=' + str(gid) + ' ' args += 'mode=' + modes + ' ' runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_name='file', module_args=args, inventory=self.inventory) results = runner.run() # success is demonstrated by several things if 'contacted' not in results: self.log('FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log('FATAL: can\'t find ' + hostname + ' in results') return False host = contacted[hostname] if 'state' not in host: return False state = host['state'] # this is the actual value we're looking for if state != 'directory': return False return True
def run(self): return runner.Runner(pattern=self.inventory, module_name=self.mod, module_args=self.args).run()
def applyQuotas(self, fqdn, user, dirname, soft=0, hard=0, present=True): hostname = self.getHostname(fqdn) args = u'user='******' ' # should handle escaping ? args += u'dirname=' + unicode(dirname) + u' ' # special case, both quota values at 0 == no quotas if soft == 0 and hard == 0: present = False if present: state = u'present' args += u'state=' + state + u' ' args += u'soft=' + unicode(soft) + u' ' args += u'hard=' + unicode(hard) + u' ' else: state = u'absent' args += u'state=' + state + u' ' # add this scripts' directory in the ansible directory list import os.path path = os.path.dirname(__file__) runner = ar.Runner(pattern=hostname, forks=1, sudo=True, module_path=path, module_name='linux-quota.py', module_args=args, inventory=self.inventory) results = runner.run() #print results # error handling if 'contacted' not in results: self.log(u'FATAL: no \'contacted\' in results') return False contacted = results['contacted'] if hostname not in contacted: self.log(u'FATAL: can\'t find ' + unicode(hostname) + u' in results') return False host = contacted[hostname] if 'failed' in host: msg = '' if 'msg' in host: msg = host['msg'] self.log(u'The plugin encountered an issue : ' + unicode(msg)) return False if 'state' not in host: self.log(u'FATAL: can\'t find the state in the response') self.log(results) return False tstate = host['state'] if tstate != state: self.log( u'FATAL: the state in the response does not correspond to the requested state' ) return False return True