def test_one(self): pb = os.path.join(self.test_dir, 'playbook1.yml') expected = os.path.join(self.test_dir, 'playbook1.events') expected = utils.json_loads(file(expected).read()) actual = self._run(pb) # if different, this will output to screen print utils.bigjson(actual) assert cmp(expected, actual) == 0, "expected events match actual events" # make sure the template module took options from the vars section data = file('/tmp/ansible_test_data_template.out').read() assert data.find("ears") != -1, "template success"
def _execute_module(self, conn, tmp, remote_module_path, args, async_jid=None, async_module=None, async_limit=None): """ runs a module that has already been transferred """ inject = self.setup_cache.get(conn.host, {}).copy() host_variables = self.inventory.get_variables(conn.host) inject.update(host_variables) inject.update(self.module_vars) conditional = utils.double_template(self.conditional, inject, self.setup_cache) if not eval(conditional): return [utils.smjson(dict(skipped=True)), None, "skipped"] if self.module_name == "setup": if not args: args = {} args = self._add_setup_vars(inject, args) args = self._add_setup_metadata(args) if type(args) == dict: args = utils.bigjson(args) args = utils.template(args, inject, self.setup_cache) module_name_tail = remote_module_path.split("/")[-1] argsfile = self._transfer_str(conn, tmp, "arguments", args) if async_jid is None: cmd = "%s %s" % (remote_module_path, argsfile) else: cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]]) res, err = self._exec_command(conn, cmd, tmp, sudoable=True) client_executed_str = "%s %s" % (module_name_tail, args.strip()) return (res, err, client_executed_str)
def _execute_module(self, conn, tmp, remote_module_path, args, async_jid=None, async_module=None, async_limit=None): ''' runs a module that has already been transferred ''' inject = self.setup_cache.get(conn.host,{}) conditional = utils.double_template(self.conditional, inject) if not eval(conditional): return [ utils.smjson(dict(skipped=True)), None, 'skipped' ] if Runner._external_variable_script is not None: self._add_variables_from_script(conn, inject) if self.module_name == 'setup': args = self._add_setup_vars(inject, args) args = self._add_setup_metadata(args) if type(args) == dict: args = utils.bigjson(args) args = utils.template(args, inject) module_name_tail = remote_module_path.split("/")[-1] argsfile = self._transfer_str(conn, tmp, 'arguments', args) if async_jid is None: cmd = "%s %s" % (remote_module_path, argsfile) else: cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]]) res, err = self._exec_command(conn, cmd, tmp, sudoable=True) client_executed_str = "%s %s" % (module_name_tail, args.strip()) return ( res, err, client_executed_str )
def _execute_module(self, conn, tmp, remote_module_path, args, async_jid=None, async_module=None, async_limit=None): ''' runs a module that has already been transferred ''' inject = self.setup_cache.get(conn.host,{}) conditional = utils.double_template(self.conditional, inject, self.setup_cache) if not eval(conditional): return [ utils.smjson(dict(skipped=True)), None, 'skipped' ] host_variables = self.inventory.get_variables(conn.host) inject.update(host_variables) if self.module_name == 'setup': args = self._add_setup_vars(inject, args) args = self._add_setup_metadata(args) if type(args) == dict: args = utils.bigjson(args) args = utils.template(args, inject, self.setup_cache) module_name_tail = remote_module_path.split("/")[-1] argsfile = self._transfer_str(conn, tmp, 'arguments', args) if async_jid is None: cmd = "%s %s" % (remote_module_path, argsfile) else: cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]]) res, err = self._exec_command(conn, cmd, tmp, sudoable=True) client_executed_str = "%s %s" % (module_name_tail, args.strip()) return ( res, err, client_executed_str )
def _execute_module(self, conn, tmp, remote_module_path, args, async_jid=None, async_module=None, async_limit=None): ''' runs a module that has already been transferred ''' inject = self.setup_cache.get(conn.host,{}).copy() host_variables = self.inventory.get_variables(conn.host) inject.update(host_variables) inject.update(self.module_vars) if self.module_name == 'setup': if not args: args = {} args = self._add_setup_vars(inject, args) args = self._add_setup_metadata(args) if type(args) == dict: args = utils.bigjson(args) args = utils.template(args, inject, self.setup_cache) module_name_tail = remote_module_path.split("/")[-1] argsfile = self._transfer_str(conn, tmp, 'arguments', args) if async_jid is None: cmd = "%s %s" % (remote_module_path, argsfile) else: cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]]) res = self._low_level_exec_command(conn, cmd, tmp, sudoable=True) result1 = utils.parse_json(res) executed_str = "%s %s" % (module_name_tail, args.strip()) return ReturnData(host=conn.host, result=res, executed_str=executed_str)
def _execute_module(self, conn, tmp, remote_module_path, args, async_jid=None, async_module=None, async_limit=None): ''' runs a module that has already been transferred ''' inject = self.setup_cache.get(conn.host,{}).copy() host_variables = self.inventory.get_variables(conn.host) inject.update(host_variables) inject.update(self.module_vars) if self.module_name == 'setup': if not args: args = {} args = self._add_setup_vars(inject, args) args = self._add_setup_metadata(args) if type(args) == dict: args = utils.bigjson(args) args = utils.template(args, inject, self.setup_cache) module_name_tail = remote_module_path.split("/")[-1] argsfile = self._transfer_str(conn, tmp, 'arguments', args) if async_jid is None: cmd = "%s %s" % (remote_module_path, argsfile) else: cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]]) res = self._low_level_exec_command(conn, cmd, tmp, sudoable=True) executed_str = "%s %s" % (module_name_tail, args.strip()) return ReturnData(host=conn.host, result=res, executed_str=executed_str)
def _run(self, test_playbook): """ run a module and get the localhost results """ self.test_callbacks = TestCallbacks() self.playbook = ansible.playbook.PlayBook( playbook=test_playbook, host_list="test/ansible_hosts", module_path="library/", forks=1, timeout=5, remote_user=self.user, remote_pass=None, stats=ans_callbacks.AggregateStats(), callbacks=self.test_callbacks, runner_callbacks=self.test_callbacks, ) result = self.playbook.run() print utils.bigjson(dict(events=EVENTS)) return result
def _execute_template(self, conn, host, tmp): ''' handler for template operations ''' # load up options options = utils.parse_kv(self.module_args) source = options.get('src', None) dest = options.get('dest', None) metadata = options.get('metadata', None) if source is None or dest is None: return (host, True, dict(failed=True, msg="src and dest are required"), '') if metadata is None: if self.remote_user == 'root': metadata = '/etc/ansible/setup' else: metadata = '~/.ansible/setup' # first copy the source template over temppath = tmp + os.path.split(source)[-1] conn.put_file(utils.path_dwim(self.basedir, source), temppath) # install the template module template_module = self._transfer_module(conn, tmp, 'template') # transfer module vars if self.module_vars: vars = utils.bigjson(self.module_vars) vars_path = self._transfer_str(conn, tmp, 'module_vars', vars) vars_arg = " vars=%s" % (vars_path) else: vars_arg = "" # run the template module args = "src=%s dest=%s metadata=%s%s" % (temppath, dest, metadata, vars_arg) (result1, err, executed) = self._execute_module(conn, tmp, template_module, args) (host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed) if ok: return self._chain_file_module(conn, tmp, data, err, options, executed) else: return (host, ok, data, err)
def test_one(self): pb = os.path.join(self.test_dir, "playbook1.yml") actual = self._run(pb) # if different, this will output to screen print "**ACTUAL**" print utils.bigjson(actual) expected = {"127.0.0.2": {"changed": 9, "failures": 0, "ok": 12, "skipped": 1, "unreachable": 0}} print "**EXPECTED**" print utils.bigjson(expected) assert utils.bigjson(expected) == utils.bigjson(actual) # make sure the template module took options from the vars section data = file("/tmp/ansible_test_data_template.out").read() assert data.find("ears") != -1, "template success"
def _execute_template(self, conn, host, tmp): ''' handler for template operations ''' # load up options options = utils.parse_kv(self.module_args) source = options.get('src', None) dest = options.get('dest', None) metadata = options.get('metadata', None) if source is None or dest is None: return (host, True, dict(failed=True, msg="src and dest are required"), '') if metadata is None: if self.remote_user == 'root': metadata = '/etc/ansible/setup' else: metadata = '~/.ansible/setup' # first copy the source template over temppath = tmp + os.path.split(source)[-1] conn.put_file(utils.path_dwim(self.basedir, source), temppath) # install the template module template_module = self._transfer_module(conn, tmp, 'template') # transfer module vars if self.module_vars: vars = utils.bigjson(self.module_vars) vars_path = self._transfer_str(conn, tmp, 'module_vars', vars) vars_arg=" vars=%s"%(vars_path) else: vars_arg="" # run the template module args = "src=%s dest=%s metadata=%s%s" % (temppath, dest, metadata, vars_arg) (result1, err, executed) = self._execute_module(conn, tmp, template_module, args) (host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed) if ok: return self._chain_file_module(conn, tmp, data, err, options, executed) else: return (host, ok, data, err)
def test_one(self): pb = os.path.join(self.test_dir, 'playbook1.yml') actual = self._run(pb) # if different, this will output to screen print "**ACTUAL**" print utils.bigjson(actual) expected = { "127.0.0.2": { "changed": 9, "failures": 0, "ok": 12, "skipped": 1, "unreachable": 0 } } print "**EXPECTED**" print utils.bigjson(expected) assert utils.bigjson(expected) == utils.bigjson(actual) # make sure the template module took options from the vars section data = file('/tmp/ansible_test_data_template.out').read() print data assert data.find("ears") != -1, "template success"
''' runs a module that has already been transferred ''' inject = self.setup_cache.get(conn.host,{}) conditional = utils.double_template(self.conditional, inject) if not eval(conditional): return [ utils.smjson(dict(skipped=True)), None, 'skipped' ] host_variables = self.inventory.get_variables(conn.host, self.extra_vars) inject.update(host_variables) if self.module_name == 'setup': args = self._add_setup_vars(inject, args) args = self._add_setup_metadata(args) if type(args) == dict: args = utils.bigjson(args) args = utils.template(args, inject) module_name_tail = remote_module_path.split("/")[-1] argsfile = self._transfer_str(conn, tmp, 'arguments', args) if async_jid is None: cmd = "%s %s" % (remote_module_path, argsfile) else: cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]]) res, err = self._exec_command(conn, cmd, tmp, sudoable=True) client_executed_str = "%s %s" % (module_name_tail, args.strip()) return ( res, err, client_executed_str ) # *****************************************************