def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): try: args = {} if complex_args: args.update(complex_args) args.update(parse_kv(module_args)) envdict={} if self.runner.environment: env=template.template(self.runner.basedir, self.runner.environment, inject, convert_bare=True) env = utils.safe_eval(env) testQueue = args["queue_name"] region = args["region"] spec_file = path.expanduser("~/.nucleator/contrib/Bucketandq/orchestrator/specification.json") queue = self.initQueue(testQueue, region, env) self.main(self.addMessageToQueue, queue, spec_file) return ReturnData(conn=conn, comm_ok=True, result=dict(failed=False, changed=False, msg="Bucketandq Messages Created!")) except Exception, e: # deal with failure gracefully result = dict(failed=True, msg=type(e).__name__ + ": " + str(e)) return ReturnData(conn=conn, comm_ok=False, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): if not module_args: result = dict(failed=True, msg="No source file given") return ReturnData(conn=conn, comm_ok=True, result=result) source = module_args source = template.template(self.runner.basedir, source, inject) if '_original_file' in inject: source = utils.path_dwim_relative(inject['_original_file'], 'vars', source, self.runner.basedir) else: source = utils.path_dwim(self.runner.basedir, source) if os.path.exists(source): data = utils.parse_yaml_from_file( source, vault_password=self.runner.vault_pass) if type(data) != dict: raise errors.AnsibleError( "%s must be stored as a dictionary/hash" % source) result = dict(ansible_facts=data) return ReturnData(conn=conn, comm_ok=True, result=result) else: result = dict(failed=True, msg="Source file not found.", file=source) return ReturnData(conn=conn, comm_ok=True, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): if self.runner.check: # in --check mode, always skip this module execution return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True)) executable = '' # From library/command, keep in sync r = re.compile( r'(^|\s)(executable)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)\s|$)' ) for m in r.finditer(module_args): v = m.group(4).replace("\\", "") if m.group(2) == "executable": executable = v module_args = r.sub("", module_args) return ReturnData(conn=conn, result=self.runner._low_level_exec_command( conn, module_args, tmp, sudoable=True, executable=executable))
def run(self, conn, tmp, module_name, inject): ''' handler for file transfer operations ''' # load up options options = utils.parse_kv(self.runner.module_args) source = options.get('src', None) dest = options.get('dest', None) if (source is None and not 'first_available_file' in inject) or dest is None: result = dict(failed=True, msg="src and dest are required") return ReturnData(conn=conn, result=result) # if we have first_available_file in our vars # look up the files and use the first one we find as src if 'first_available_file' in inject: found = False for fn in inject.get('first_available_file'): fn = utils.template(fn, inject) if os.path.exists(fn): source = fn found = True break if not found: results = dict( failed=True, msg="could not find src in first_available_file list") return ReturnData(conn=conn, results=results) source = utils.template(source, inject) source = utils.path_dwim(self.runner.basedir, source) local_md5 = utils.md5(source) if local_md5 is None: result = dict(failed=True, msg="could not find src=%s" % source) return ReturnData(conn=conn, result=result) remote_md5 = self.runner._remote_md5(conn, tmp, dest) exec_rc = None if local_md5 != remote_md5: # transfer the file to a remote tmp location tmp_src = tmp + os.path.basename(source) conn.put_file(source, tmp_src) # fix file permissions when the copy is done as a different user if self.runner.sudo and self.runner.sudo_user != 'root': self.runner._low_level_exec_command(conn, "chmod a+r %s" % tmp_src, tmp) # run the copy module self.runner.module_args = "%s src=%s" % (self.runner.module_args, tmp_src) return self.runner._execute_module( conn, tmp, 'copy', self.runner.module_args, inject=inject).daisychain('file') else: # no need to transfer the file, already correct md5 result = dict(changed=False, md5sum=remote_md5, transferred=False) return ReturnData(conn=conn, result=result).daisychain('file')
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) src = options.get('src', None) dest = options.get('dest', None) delimiter = options.get('delimiter', None) remote_src = utils.boolean(options.get('remote_src', 'yes')) if src is None or dest is None: result = dict(failed=True, msg="src and dest are required") return ReturnData(conn=conn, comm_ok=False, result=result) if remote_src: return self.runner._execute_module(conn, tmp, 'assemble', module_args, inject=inject, complex_args=complex_args) elif '_original_file' in inject: src = utils.path_dwim_relative(inject['_original_file'], 'files', src, self.runner.basedir) else: # the source is local, so expand it here src = os.path.expanduser(src) # Does all work assembling the file path = self._assemble_from_fragments(src, delimiter) pathmd5 = utils.md5s(path) remote_md5 = self.runner._remote_md5(conn, tmp, dest) if pathmd5 != remote_md5: resultant = file(path).read() if self.runner.diff: dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, inject=inject, persist_files=True) if 'content' in dest_result.result: dest_contents = dest_result.result['content'] if dest_result.result['encoding'] == 'base64': dest_contents = base64.b64decode(dest_contents) else: raise Exception("unknown encoding, failed: %s" % dest_result.result) xfered = self.runner._transfer_str(conn, tmp, 'src', resultant) # fix file permissions when the copy is done as a different user if self.runner.sudo and self.runner.sudo_user != 'root': self.runner._low_level_exec_command(conn, "chmod a+r %s" % xfered, tmp) # run the copy module module_args = "%s src=%s dest=%s original_basename=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest), pipes.quote(os.path.basename(src))) if self.runner.noop_on_check(inject): return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=src, after=resultant)) else: res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject) res.diff = dict(after=resultant) return res else: module_args = "%s src=%s dest=%s original_basename=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest), pipes.quote(os.path.basename(src))) return self.runner._execute_module(conn, tmp, 'file', module_args, inject=inject)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): ''' handler for template operations ''' # note: since this module just calls the copy module, the --check mode support # can be implemented entirely over there if not self.runner.is_playbook: raise errors.AnsibleError("in current versions of ansible, templates are only usable in playbooks") # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) source = options.get('src', None) dest = options.get('dest', None) if (source is None and 'first_available_file' not in inject) or dest is None: result = dict(failed=True, msg="src and dest are required") return ReturnData(conn=conn, comm_ok=False, result=result) # if we have first_available_file in our vars # look up the files and use the first one we find as src if 'first_available_file' in inject: found = False for fn in self.runner.module_vars.get('first_available_file'): fn_orig = fn fnt = template.template(self.runner.basedir, fn, inject) fnd = utils.path_dwim(self.runner.basedir, fnt) if not os.path.exists(fnd) and '_original_file' in inject: fnd = utils.path_dwim_relative(inject['_original_file'], 'templates', fnt, self.runner.basedir, check=False) if os.path.exists(fnd): source = fnd found = True break if not found: result = dict(failed=True, msg="could not find src in first_available_file list") return ReturnData(conn=conn, comm_ok=False, result=result) else: source = template.template(self.runner.basedir, source, inject) if '_original_file' in inject: source = utils.path_dwim_relative(inject['_original_file'], 'templates', source, self.runner.basedir) else: source = utils.path_dwim(self.runner.basedir, source) if dest.endswith("/"): # CCTODO: Fix path for Windows hosts. base = os.path.basename(source) dest = os.path.join(dest, base) # template the source data locally & get ready to transfer try: resultant = template.template_from_file(self.runner.basedir, source, inject, vault_password=self.runner.vault_pass) except Exception, e: result = dict(failed=True, msg=type(e).__name__ + ": " + str(e)) return ReturnData(conn=conn, comm_ok=False, result=result)
def run(self, conn, tmp_path, module_name, module_args, inject, complex_args=None, **kwargs): ''' handler for file transfer operations ''' # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) source = options.get('src', None) content = options.get('content', None) dest = options.get('dest', None) raw = utils.boolean(options.get('raw', 'no')) force = utils.boolean(options.get('force', 'yes')) if (source is None and content is None and not 'first_available_file' in inject) or dest is None: result = dict(failed=True, msg="src (or content) and dest are required") return ReturnData(conn=conn, result=result) elif (source is not None or 'first_available_file' in inject) and content is not None: result = dict(failed=True, msg="src and content are mutually exclusive") return ReturnData(conn=conn, result=result) # Check if the source ends with a "/" source_trailing_slash = False if source: source_trailing_slash = source.endswith("/") # Define content_tempfile in case we set it after finding content populated. content_tempfile = None # If content is defined make a temp file and write the content into it. if content is not None: try: # If content comes to us as a dict it should be decoded json. # We need to encode it back into a string to write it out. if type(content) is dict: content_tempfile = self._create_content_tempfile( json.dumps(content)) else: content_tempfile = self._create_content_tempfile(content) source = content_tempfile except Exception, err: result = dict(failed=True, msg="could not write content temp file: %s" % err) return ReturnData(conn=conn, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): ''' handler for file transfer operations ''' # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) source = options.get('src', None) dest = options.get('dest', None) if source is None or dest is None: result = dict(failed=True, msg="src (or content) and dest are required") return ReturnData(conn=conn, result=result) source = template.template(self.runner.basedir, source, inject) if '_original_file' in inject: source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir) else: source = utils.path_dwim(self.runner.basedir, source) remote_md5 = self.runner._remote_md5(conn, tmp, dest) if remote_md5 != '3': result = dict(failed=True, msg="dest must be an existing dir") return ReturnData(conn=conn, result=result) # transfer the file to a remote tmp location tmp_src = tmp + 'source' conn.put_file(source, tmp_src) # handle diff mode client side # handle check mode client side # fix file permissions when the copy is done as a different user if self.runner.sudo and self.runner.sudo_user != 'root': self.runner._low_level_exec_command(conn, "chmod a+r %s" % tmp_src, tmp) module_args = "%s src=%s original_basename=%s" % ( module_args, pipes.quote(tmp_src), pipes.quote(os.path.basename(source))) return self.runner._execute_module(conn, tmp, 'unarchive', module_args, inject=inject, complex_args=complex_args)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): ''' handler for file transfer operations ''' # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) source = options.get('src', None) content = options.get('content', None) dest = options.get('dest', None) raw = utils.boolean(options.get('raw', 'no')) force = utils.boolean(options.get('force', 'yes')) if (source is None and content is None and not 'first_available_file' in inject) or dest is None: result=dict(failed=True, msg="src (or content) and dest are required") return ReturnData(conn=conn, result=result) elif (source is not None or 'first_available_file' in inject) and content is not None: result=dict(failed=True, msg="src and content are mutually exclusive") return ReturnData(conn=conn, result=result) source_trailing_slash = False if source: source_trailing_slash = source.endswith("/") # if we have first_available_file in our vars # look up the files and use the first one we find as src if 'first_available_file' in inject: found = False for fn in inject.get('first_available_file'): fn_orig = fn fnt = template.template(self.runner.basedir, fn, inject) fnd = utils.path_dwim(self.runner.basedir, fnt) if not os.path.exists(fnd) and '_original_file' in inject: fnd = utils.path_dwim_relative(inject['_original_file'], 'files', fnt, self.runner.basedir, check=False) if os.path.exists(fnd): source = fnd found = True break if not found: results=dict(failed=True, msg="could not find src in first_available_file list") return ReturnData(conn=conn, result=results) elif content is not None: fd, tmp_content = tempfile.mkstemp() f = os.fdopen(fd, 'w') try: f.write(content) except Exception, err: os.remove(tmp_content) result = dict(failed=True, msg="could not write content temp file: %s" % err) return ReturnData(conn=conn, result=result) f.close() source = tmp_content
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): """ Run the action plugin. """ # save standard module args for convenience self.conn, self.tmp, self.inject = conn, tmp, inject if self.runner.check: return ReturnData(conn=conn, result=dict(failed=False, changed=self.changed, msg="ok")) try: # preserve and validate options self.set_options(module_args, complex_args) # pull image first (to allow for container restart) if self.images_state in (PRESENT, LATEST): self.pull_images() if self.containers_state in (ABSENT, ): if self.has_docker_compose_file(): self.remove_docker_compose_containers() self.remove_docker_compose_file() self.remove_docker_compose_configuration_directory() elif self.containers_state in (PRESENT, STARTED, RESTARTED): self.create_docker_compose_configuration_directory() self.create_docker_compose_file() if self.containers_state in (STARTED, RESTARTED): self.create_docker_compose_containers() # remove image last (to allow for container removal first) if self.images_state in (ABSENT, ): self.remove_images() except ModuleError as error: return ReturnData(conn=conn, result=dict(failed=True, changed=self.changed, msg=error.message)) else: return ReturnData(conn=conn, result=dict(failed=False, changed=self.changed, msg="ok"))
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): args = parse_kv(module_args) environment = args.get('environment') account_id = args.get('account_id') assume_role = args.get('assume_role') for name, val in (("environment", environment), ("account_id", account_id), ("assume_role", assume_role)): if val is None: result = dict(failed=True, msg="No {0} specified".format(name)) return ReturnData(conn=conn, comm_ok=True, result=result) source = template.template(self.runner.basedir, environment, inject) if '_original_file' in inject: source = utils.path_dwim_relative(inject['_original_file'], 'vars', source, self.runner.basedir) else: source = utils.path_dwim(self.runner.basedir, source) if os.path.exists(source): decrypted_data = {} data = json.load(open(source)) with assume(account_id, assume_role): kms = boto.kms.connect_to_region('ap-southeast-2') for key, val in data.items(): data_key = kms.decrypt(base64.b64decode( val['key']))["Plaintext"] content = base64.b64decode(val['content']) counter = Counter.new(128) decryptor = AES.new(data_key[:32], AES.MODE_CTR, counter=counter) decrypted_data[key] = decryptor.decrypt(content) result = dict(ansible_facts=decrypted_data) return ReturnData(conn=conn, comm_ok=True, result=result) else: result = dict(failed=True, msg="Couldn't find secrets!", file=source) return ReturnData(conn=conn, comm_ok=True, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): if self.runner.check: return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module')) args = {} if complex_args: args.update(complex_args) args.update(parse_kv(module_args)) if not 'hostname' in args and not 'name' in args: raise ae("'name' is a required argument.") result = {} # Parse out any hostname:port patterns new_name = args.get('name', args.get('hostname', None)) vv("creating host via 'add_host': hostname=%s" % new_name) if ":" in new_name: new_name, new_port = new_name.split(":") args['ansible_ssh_port'] = new_port # create host and get inventory new_host = Host(new_name) inventory = self.runner.inventory # Add any variables to the new_host for k in args.keys(): if not k in [ 'name', 'hostname', 'groupname', 'groups' ]: new_host.set_variable(k, args[k]) # add the new host to the 'all' group allgroup = inventory.get_group('all') allgroup.add_host(new_host) groupnames = args.get('groupname', args.get('groups', '')) # add it to the group if that was specified if groupnames != '': for group_name in groupnames.split(","): if not inventory.get_group(group_name): new_group = Group(group_name) inventory.add_group(new_group) grp = inventory.get_group(group_name) grp.add_host(new_host) vv("added host to group via add_host module: %s" % group_name) result['new_groups'] = groupnames.split(",") result['new_host'] = new_name return ReturnData(conn=conn, comm_ok=True, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): ''' handler for file transfer operations ''' # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) source = options.get('src', None) dest = options.get('dest', None) copy = utils.boolean(options.get('copy', 'yes')) if source is None or dest is None: result = dict(failed=True, msg="src (or content) and dest are required") return ReturnData(conn=conn, result=result) dest = os.path.expanduser(dest) # CCTODO: Fix path for Windows hosts. source = template.template(self.runner.basedir, os.path.expanduser(source), inject) if copy: if '_original_file' in inject: source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir) else: source = utils.path_dwim(self.runner.basedir, source) remote_md5 = self.runner._remote_md5(conn, tmp, dest) if remote_md5 != '3': result = dict(failed=True, msg="dest '%s' must be an existing dir" % dest) return ReturnData(conn=conn, result=result) if copy: # transfer the file to a remote tmp location tmp_src = tmp + 'source' conn.put_file(source, tmp_src) # handle diff mode client side # handle check mode client side # fix file permissions when the copy is done as a different user if copy: if self.runner.sudo and self.runner.sudo_user != 'root' or self.runner.su and self.runner.su_user != 'root': self.runner._remote_chmod(conn, 'a+r', tmp_src, tmp) # Build temporary module_args. new_module_args = dict( src=tmp_src, original_basename=os.path.basename(source), ) module_args = utils.merge_module_args(module_args, new_module_args) else: module_args = "%s original_basename=%s" % (module_args, pipes.quote(os.path.basename(source))) return self.runner._execute_module(conn, tmp, 'unarchive', module_args, inject=inject, complex_args=complex_args)
def run(self, conn, tmp, module_name, module_args, inject): ''' handler for template operations ''' if not self.runner.is_playbook: raise errors.AnsibleError( "in current versions of ansible, templates are only usable in playbooks" ) # load up options options = utils.parse_kv(module_args) source = options.get('src', None) dest = options.get('dest', None) if dest.endswith("/"): base = os.path.basename(source) dest = os.path.join(dest, base) if (source is None and 'first_available_file' not in inject) or dest is None: result = dict(failed=True, msg="src and dest are required") return ReturnData(conn=conn, comm_ok=False, result=result) # if we have first_available_file in our vars # look up the files and use the first one we find as src if 'first_available_file' in inject: found = False for fn in self.runner.module_vars.get('first_available_file'): fnt = utils.template(self.runner.basedir, fn, inject) fnd = utils.path_dwim(self.runner.basedir, fnt) if os.path.exists(fnd): source = fnt found = True break if not found: result = dict( failed=True, msg="could not find src in first_available_file list") return ReturnData(conn=conn, comm_ok=False, result=result) else: source = utils.template(self.runner.basedir, source, inject) # template the source data locally & transfer try: resultant = utils.template_from_file(self.runner.basedir, source, inject) except Exception, e: result = dict(failed=True, msg=str(e)) return ReturnData(conn=conn, comm_ok=False, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): self.print_disks() while (self.disk is None): disk = self.user_ask_disk() if (self.disk_exists(disk)): self.disk = disk else: print("Error: %s is not a valid block device.\n" % disk) confirmed = self.user_ask_confirm() result = {'disk': self.disk, 'confirmed': confirmed} if (confirmed): unmount_disk(self.disk) partition_identifier = format_disk(self.disk) result['partition'] = get_device_info(partition_identifier) return ReturnData(conn=conn, comm_ok=True, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): ''' transfer & execute a module that is not 'copy' or 'template' ''' # 传输并执行一个模块,除了“copy”和“template”模块 module_args = self.runner._complex_args_hack(complex_args, module_args) if self.runner.noop_on_check(inject): if module_name in [ 'shell', 'command' ]: return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for %s' % module_name)) # else let the module parsing code decide, though this will only be allowed for AnsibleModuleCommon using # python modules for now module_args += " CHECKMODE=True" if self.runner.no_log: module_args += " NO_LOG=True" # shell and command are the same module if module_name == 'shell': module_name = 'command' module_args += " #USE_SHELL" if self.runner.no_log: module_display_args = "(no_log enabled, args censored)" else: module_display_args = module_args vv("REMOTE_MODULE %s %s" % (module_name, module_display_args), host=conn.host) return self.runner._execute_module(conn, tmp, module_name, module_args, inject=inject, complex_args=complex_args)
def get_checksum(self, conn, tmp, dest, inject, try_directory=False, source=None): remote_checksum = self.runner._remote_checksum(conn, tmp, dest, inject) if remote_checksum in ('0', '2', '3', '4'): # Note: 1 means the file is not present which is fine; template # will create it. 3 means directory was specified instead of file # which requires special handling if try_directory and remote_checksum == '3' and source: # If the user specified a directory name as their dest then we # have to check the checksum of dest/basename(src). This is # the same behaviour as cp foo.txt /var/tmp/ so users expect # it to work. base = os.path.basename(source) dest = os.path.join(dest, base) remote_checksum = self.get_checksum(conn, tmp, dest, inject, try_directory=False) if remote_checksum not in ('0', '2', '3', '4'): return remote_checksum result = dict(failed=True, msg="failed to checksum remote file." " Checksum error code: %s" % remote_checksum) return ReturnData(conn=conn, comm_ok=True, result=result) return remote_checksum
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) src = options.get('src', None) dest = options.get('dest', None) remote_src = utils.boolean(options.get('remote_src', 'no')) if src is None: result = dict(failed=True, msg="src is required") return ReturnData(conn=conn, comm_ok=False, result=result) if remote_src: return self.runner._execute_module(conn, tmp, 'patch', module_args, inject=inject, complex_args=complex_args) # Source is local if '_original_file' in inject: src = utils.path_dwim_relative(inject['_original_file'], 'files', src, self.runner.basedir) else: src = utils.path_dwim(self.runner.basedir, src) if tmp is None or "-tmp-" not in tmp: tmp = self.runner._make_tmp_path(conn) tmp_src = conn.shell.join_path(tmp, os.path.basename(src)) conn.put_file(src, tmp_src) if self.runner.become and self.runner.become_user != 'root': if not self.runner.noop_on_check(inject): self.runner._remote_chmod(conn, 'a+r', tmp_src, tmp) new_module_args = dict(src=tmp_src, ) if self.runner.noop_on_check(inject): new_module_args['CHECKMODE'] = True module_args = utils.merge_module_args(module_args, new_module_args) return self.runner._execute_module(conn, tmp, 'patch', module_args, inject=inject, complex_args=complex_args)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): stdouts = glob.glob('/tmp/jobs-work/*/*-*') stats = {} for filename in stdouts: builder = filename.split('/')[3] if builder in IGNORED_BUILDERS: continue if filename.endswith('bz2'): data = bz2.BZ2File(filename).readlines() else: data = file(filename).readlines() for line in data: if '... [ERROR]' in line: test = builder + ':' + line.split(' ')[0] if test not in stats: stats[test] = 0 stats[test] += 1 stats = stats.items() stats.sort(key=lambda x: x[1]) out = file('out/test-count.csv', 'w') for stat in stats: out.write('%s:%s\n' % stat) out.close() return ReturnData(conn=conn, result=dict( changed=True ))
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): args = {} if complex_args: args.update(complex_args) # attempt to prevent confusing messages when the variable didn't interpolate module_args = module_args.replace("{{ ", "{{").replace(" }}", "}}") kv = utils.parse_kv(module_args) args.update(kv) if not 'msg' in args: args['msg'] = 'Hello world!' if 'fail' in args and utils.boolean(args['fail']): result = dict(failed=True, msg=args['msg']) else: result = dict(msg=args['msg']) return ReturnData(conn=conn, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): args = {} if complex_args: args.update(complex_args) args.update(parse_kv(module_args)) hostname = args.get("hostname", "null") key_path = args.get("key_path", "null") failed = True command = 'ssh -i %s -q -o "StrictHostKeyChecking=no" -o "BatchMode=yes" ec2-user@%s "echo 2>&1" && echo "UP" || echo "DOWN"' % (key_path, hostname) process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) output, error = process.communicate() failed = False if output.find("UP") < 0: failed = True message = "Successfully tested SSH connectivity to %s" % hostname if failed: message = "Failed to successfully connect via SSH to %s. Please check your SSH configuration." % hostname return ReturnData(conn=conn, comm_ok=True, result=dict(failed=failed, changed=False, msg=message))
def run(self, conn, tmp, module_name, module_args, inject): args = parse_kv(module_args) if not 'hostname' in args: raise ae("'hostname' is a required argument.") vv("created 'add_host' ActionModule: hostname=%s" % (args['hostname'])) result = {'changed': True} new_host = Host(args['hostname']) inventory = self.runner.inventory # add the new host to the 'all' group allgroup = inventory.get_group('all') allgroup.add_host(new_host) result['changed'] = True # add it to the group if that was specified if 'groupname' in args: if not inventory.get_group(args['groupname']): new_group = Group(args['groupname']) inventory.add_group(new_group) ngobj = inventory.get_group(args['groupname']) ngobj.add_host(new_host) vv("created 'add_host' ActionModule: groupname=%s" % (args['groupname'])) result['new_group'] = args['groupname'] result['new_host'] = args['hostname'] return ReturnData(conn=conn, comm_ok=True, result=result)
def get_checksum(self, conn, tmp, dest, inject, try_directory=False, source=None): remote_checksum = self.runner._remote_checksum(conn, tmp, dest, inject) if remote_checksum in ('0', '2', '3', '4'): # Note: 1 means the file is not present which is fine; template # will create it. 3 means directory was specified instead of file if try_directory and remote_checksum == '3' and source: base = os.path.basename(source) dest = os.path.join(dest, base) remote_checksum = self.get_checksum(conn, tmp, dest, inject, try_directory=False) if remote_checksum not in ('0', '2', '3', '4'): return remote_checksum result = dict(failed=True, msg="failed to checksum remote file." " Checksum error code: %s" % remote_checksum) return ReturnData(conn=conn, comm_ok=True, result=result) return remote_checksum
def run(self, conn, tmp, module_name, module_args, inject): args = utils.parse_kv(module_args) if not 'msg' in args: args['msg'] = 'Failed as requested from task' result = dict(failed=True, msg=args['msg']) return ReturnData(conn=conn, result=result)
def run_old(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): """Run the old version.""" result = dict(changed=False, payload=None, doctype='gitrepos') # Only save data if cloud snitch is enabled if not os.environ.get('CLOUD_SNITCH_ENABLED'): return ReturnData(conn=conn, result=result) result['payload'] = self.get_repo_data(_REPO_LIST) return ReturnData(conn=conn, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): args = {} if complex_args: args.update(complex_args) # attempt to prevent confusing messages when the variable didn't interpolate module_args = module_args.replace("{{ ","{{").replace(" }}","}}") kv = utils.parse_kv(module_args) args.update(kv) if not 'msg' in args and not 'var' in args: args['msg'] = 'Hello world!' result = {} if 'msg' in args: if 'fail' in args and utils.boolean(args['fail']): result = dict(failed=True, msg=args['msg']) else: result = dict(msg=args['msg']) elif 'var' in args: results = template.template(None, "{{ %s }}" % args['var'], inject) result[args['var']] = results # force flag to make debug output module always verbose result['verbose_always'] = True return ReturnData(conn=conn, result=result)
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): args = self._merge_args(module_args, complex_args) path = args.get('path') backup_dir = self._arg_or_fact('backup_dir', 'deployment_backup_dir', args, inject) if not backup_dir: return ReturnData(conn=conn, result=dict( failed=True, msg="Please define either backup_dir parameter or deployment_backup_dir variable" )) timestamp_generated, timestamp = False, self._arg_or_fact('timestamp', 'deployment_backup_timestamp', args, inject) if not timestamp: timestamp_generated, timestamp = True, _generate_timestamp() module_args_tmp = "path=%s backup_dir=%s timestamp=%s" % (args.get('path'), backup_dir, timestamp) module_return = self.runner._execute_module(conn, tmp, 'backup', module_args_tmp, inject=inject, complex_args=complex_args, persist_files=True) # uncomment out to make debug output module always verbose # module_return.result['verbose_always'] = True if timestamp_generated: facts = module_return.result.get('ansible_facts', {}) if not facts: module_return.result['ansible_facts'] = facts facts['deployment_backup_timestamp'] = timestamp return module_return
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): # the group_by module does not need to pay attention to check mode. # it always runs. args = {} if complex_args: args.update(complex_args) args.update(parse_kv(self.runner.module_args)) if not 'key' in args: raise ae("'key' is a required argument.") vv("created 'group_by' ActionModule: key=%s" % (args['key'])) inventory = self.runner.inventory result = {'changed': False} ### find all groups groups = {} for host in self.runner.host_set: data = inject['hostvars'][host] if not check_conditional( template.template(self.runner.basedir, self.runner.conditional, data)): continue group_name = template.template(self.runner.basedir, args['key'], data) group_name = group_name.replace(' ', '-') if group_name not in groups: groups[group_name] = [] groups[group_name].append(host) result['groups'] = groups ### add to inventory for group, hosts in groups.items(): inv_group = inventory.get_group(group) if not inv_group: inv_group = ansible.inventory.Group(name=group) inventory.add_group(inv_group) for host in hosts: del self.runner.inventory._vars_per_host[host] inv_host = inventory.get_host(host) if not inv_host: inv_host = ansible.inventory.Host(name=host) if inv_group not in inv_host.get_groups(): result['changed'] = True inv_group.add_host(inv_host) return ReturnData(conn=conn, comm_ok=True, result=result)
def run_old(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): """Run the old style action module.""" # Only save data if cloud snitch is enabled if not os.environ.get('CLOUD_SNITCH_ENABLED'): result = dict(changed=False, payload=None, doctype='file_dict') return ReturnData(conn=conn, result=result) complex_args = self._build_args() # Call the file_snitch module. result = self.runner._execute_module(conn, tmp, module_name, module_args, inject=inject, complex_args=complex_args) result.result['changed'] = False result.result['doctype'] = 'file_dict' return result
class ActionModule(object): def __init__(self, runner): self.runner = runner def run(self, conn, tmp_path, module_name, module_args, inject, complex_args=None, **kwargs): ''' handler for file transfer operations ''' # load up options options = {} if complex_args: options.update(complex_args) options.update(utils.parse_kv(module_args)) source = options.get('src', None) content = options.get('content', None) dest = options.get('dest', None) raw = utils.boolean(options.get('raw', 'no')) force = utils.boolean(options.get('force', 'yes')) if (source is None and content is None and not 'first_available_file' in inject) or dest is None: result=dict(failed=True, msg="src (or content) and dest are required") return ReturnData(conn=conn, result=result) elif (source is not None or 'first_available_file' in inject) and content is not None: result=dict(failed=True, msg="src and content are mutually exclusive") return ReturnData(conn=conn, result=result) # Check if the source ends with a "/" source_trailing_slash = False if source: source_trailing_slash = source.endswith("/") # Define content_tempfile in case we set it after finding content populated. content_tempfile = None # If content is defined make a temp file and write the content into it. if content is not None: try: content_tempfile = self._create_content_tempfile(content) source = content_tempfile except Exception, err: result = dict(failed=True, msg="could not write content temp file: %s" % err) return ReturnData(conn=conn, result=result) # if we have first_available_file in our vars # look up the files and use the first one we find as src elif 'first_available_file' in inject: found = False for fn in inject.get('first_available_file'): fn_orig = fn fnt = template.template(self.runner.basedir, fn, inject) fnd = utils.path_dwim(self.runner.basedir, fnt) if not os.path.exists(fnd) and '_original_file' in inject: fnd = utils.path_dwim_relative(inject['_original_file'], 'files', fnt, self.runner.basedir, check=False) if os.path.exists(fnd): source = fnd found = True break if not found: results = dict(failed=True, msg="could not find src in first_available_file list") return ReturnData(conn=conn, result=results)