当前位置: 首页>>代码示例>>Python>>正文


Python DataLoader.load_from_file方法代码示例

本文整理汇总了Python中ansible.parsing.dataloader.DataLoader.load_from_file方法的典型用法代码示例。如果您正苦于以下问题:Python DataLoader.load_from_file方法的具体用法?Python DataLoader.load_from_file怎么用?Python DataLoader.load_from_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ansible.parsing.dataloader.DataLoader的用法示例。


在下文中一共展示了DataLoader.load_from_file方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestDataLoader

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
class TestDataLoader(unittest.TestCase):

    def setUp(self):
        self._loader = DataLoader()

    def tearDown(self):
        pass

    @patch.object(DataLoader, '_get_file_contents')
    def test_parse_json_from_file(self, mock_def):
        mock_def.return_value = ("""{"a": 1, "b": 2, "c": 3}""", True)
        output = self._loader.load_from_file('dummy_json.txt')
        self.assertEqual(output, dict(a=1,b=2,c=3))

    @patch.object(DataLoader, '_get_file_contents')
    def test_parse_yaml_from_file(self, mock_def):
        mock_def.return_value = ("""
        a: 1
        b: 2
        c: 3
        """, True)
        output = self._loader.load_from_file('dummy_yaml.txt')
        self.assertEqual(output, dict(a=1,b=2,c=3))

    @patch.object(DataLoader, '_get_file_contents')
    def test_parse_fail_from_file(self, mock_def):
        mock_def.return_value = ("""
        TEXT:
            ***
               NOT VALID
        """, True)
        self.assertRaises(AnsibleParserError, self._loader.load_from_file, 'dummy_yaml_bad.txt')
开发者ID:ShiwenMa,项目名称:MiniProject,代码行数:34,代码来源:test_data_loader.py

示例2: TestDataLoaderWithVault

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
class TestDataLoaderWithVault(unittest.TestCase):

    def setUp(self):
        self._loader = DataLoader()
        self._loader.set_vault_password('ansible')

    def tearDown(self):
        pass

    @patch.multiple(DataLoader, path_exists=lambda s, x: True, is_file=lambda s, x: True)
    def test_parse_from_vault_1_1_file(self):
        vaulted_data = """$ANSIBLE_VAULT;1.1;AES256
33343734386261666161626433386662623039356366656637303939306563376130623138626165
6436333766346533353463636566313332623130383662340a393835656134633665333861393331
37666233346464636263636530626332623035633135363732623332313534306438393366323966
3135306561356164310a343937653834643433343734653137383339323330626437313562306630
3035
"""
        if PY3:
            builtins_name = 'builtins'
        else:
            builtins_name = '__builtin__'

        with patch(builtins_name + '.open', mock_open(read_data=vaulted_data.encode('utf-8'))):
            output = self._loader.load_from_file('dummy_vault.txt')
            self.assertEqual(output, dict(foo='bar'))
开发者ID:ShiwenMa,项目名称:MiniProject,代码行数:28,代码来源:test_data_loader.py

示例3: boilerplate_module

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
def boilerplate_module(modfile, args, interpreter, check, destfile):
    """ simulate what ansible does with new style modules """

    loader = DataLoader()

    complex_args = {}
    if args.startswith("@"):
        # Argument is a YAML file (JSON is a subset of YAML)
        complex_args = utils_vars.combine_vars(complex_args,
                                               loader.load_from_file(args[1:]))
        args = ''
    elif args.startswith("{"):
        # Argument is a YAML document (not a file)
        complex_args = utils_vars.combine_vars(complex_args, loader.load(args))
        args = ''

    if args:
        parsed_args = parse_kv(args)
        complex_args = utils_vars.combine_vars(complex_args, parsed_args)

    task_vars = {}
    if interpreter:
        if '=' not in interpreter:
            print("interpreter must by in the form of \
                   ansible_python_interpreter=/usr/bin/python")
            sys.exit(1)
        interpreter_type, interpreter_path = interpreter.split('=')
        if not interpreter_type.startswith('ansible_'):
            interpreter_type = 'ansible_%s' % interpreter_type
        if not interpreter_type.endswith('_interpreter'):
            interpreter_type = '%s_interpreter' % interpreter_type
        task_vars[interpreter_type] = interpreter_path

    if check:
        complex_args['_ansible_check_mode'] = True

    modname = os.path.basename(modfile)
    modname = os.path.splitext(modname)[0]
    (module_data, module_style, shebang) = module_common.modify_module(
        modname,
        modfile,
        complex_args,
        task_vars=task_vars
    )

    if module_style == 'new' \
       and 'ZIPLOADER_WRAPPER = True' in module_data:
        module_style = 'ziploader'

    modfile2_path = os.path.expanduser(destfile)
    print("* including generated source,\
           if any, saving to: %s" % modfile2_path)
    if module_style not in ('ziploader', 'old'):
        print("* this may offset any line numbers in tracebacks/debuggers!")
    modfile2 = open(modfile2_path, 'w')
    modfile2.write(module_data)
    modfile2.close()
    modfile = modfile2_path

    return (modfile2_path, modname, module_style)
开发者ID:greg-hellings,项目名称:linch-pin,代码行数:62,代码来源:module_utils.py

示例4: main

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
    def main(self, path):
        data_dir = self.conf['data_dir']
        
        loader = DataLoader()

        full_path="%s/%s" % (data_dir, path)

        if os.path.isfile("%s.yaml" % full_path):
            ds = loader.load_from_file("%s.yaml" % full_path)
        elif os.path.isfile("%s.yml" % full_path):
            ds = loader.load_from_file("%s.yml" % full_path)
        else:
            ds={}
        if ds is None:
            ds = {}

        return ds
开发者ID:butangero,项目名称:ansible-oss,代码行数:19,代码来源:echelon_yml.py

示例5: load_vars

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
def load_vars(fname):
    if not os.path.isfile(fname):
        return {}

    if HAS_ANSIBLE2:
        loader = DataLoader()
        return loader.load_from_file(file_name=fname)
    else:
        vars = {}
        return ansible.utils.load_vars(fname, vars)
开发者ID:hasiotis,项目名称:vmbuilder,代码行数:12,代码来源:utils.py

示例6: TestDataLoader

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
class TestDataLoader(unittest.TestCase):

    def setUp(self):
        self._loader = DataLoader()

    def tearDown(self):
        pass

    @patch.object(DataLoader, '_get_file_contents')
    def test_parse_json_from_file(self, mock_def):
        mock_def.return_value = (b"""{"a": 1, "b": 2, "c": 3}""", True)
        output = self._loader.load_from_file('dummy_json.txt')
        self.assertEqual(output, dict(a=1,b=2,c=3))

    @patch.object(DataLoader, '_get_file_contents')
    def test_parse_yaml_from_file(self, mock_def):
        mock_def.return_value = (b"""
        a: 1
        b: 2
        c: 3
        """, True)
        output = self._loader.load_from_file('dummy_yaml.txt')
        self.assertEqual(output, dict(a=1,b=2,c=3))

    @patch.object(DataLoader, '_get_file_contents')
    def test_parse_fail_from_file(self, mock_def):
        mock_def.return_value = (b"""
        TEXT:
            ***
               NOT VALID
        """, True)
        self.assertRaises(AnsibleParserError, self._loader.load_from_file, 'dummy_yaml_bad.txt')

    @patch('ansible.errors.AnsibleError._get_error_lines_from_file')
    @patch.object(DataLoader, '_get_file_contents')
    def test_tab_error(self, mock_def, mock_get_error_lines):
        mock_def.return_value = (u"""---\nhosts: localhost\nvars:\n  foo: bar\n\tblip: baz""", True)
        mock_get_error_lines.return_value = ('''\tblip: baz''', '''..foo: bar''')
        with self.assertRaises(AnsibleParserError) as cm:
            self._loader.load_from_file('dummy_yaml_text.txt')
        self.assertIn(yaml_strings.YAML_COMMON_LEADING_TAB_ERROR, str(cm.exception))
        self.assertIn('foo: bar', str(cm.exception))
开发者ID:2ndQuadrant,项目名称:ansible,代码行数:44,代码来源:test_dataloader.py

示例7: run

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
    def run(self, data_dir="data", conf_file="echelon.yml"):
        from ansible.parsing.dataloader import DataLoader

        loader = DataLoader()
        ds = loader.load_from_file(conf_file)
        conf_data = self.template_loader(ds)

        hierarchies = {}
        if not "hierarchy" in conf_data:
            return hierarchies

        # Load the backends
        backends = {}
        if not "backends" in conf_data:
            raise AnsibleError("No 'backends' found in echeclon config file")

        backend_plugins = []
        for backend in conf_data["backends"]:
            for k in backend:
                try:
                    backend_plugins.append(self.backend_loader(k, backend[k]))
                except Exception as e:
                    raise AnsibleError("Failed to load backend plugin (%s): %s" % (k, str(e)))

        for hierarchy in conf_data["hierarchy"]:
            for k in hierarchy:
                data = {}
                for path in hierarchy[k]:
                    for plugin in backend_plugins:
                        full_path = "%s/%s" % (k, path)
                        data_new = self.template_loader(plugin.main(full_path))
                        if data_new == {}:
                            continue
                        else:
                            data = self.merge_dicts(data_new, data)
                            break
                    hierarchies[k] = data

        return hierarchies
开发者ID:tlipatov,项目名称:ansible-oss,代码行数:41,代码来源:echelon.py

示例8: parse_yaml_from_file

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
 def parse_yaml_from_file(filepath):
     dl = DataLoader()
     return dl.load_from_file(filepath)
开发者ID:Lowess,项目名称:ansible-lint,代码行数:5,代码来源:utils.py

示例9: __init__

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
class Transport:
    """
    Transport using Ansible.
    """

    def __init__(self, remote_user='root'):
        """
        Creates an instance of the Transport.
        """
        self.logger = logging.getLogger('transport')
        self.Options = namedtuple(
            'Options', ['connection', 'module_path', 'forks', 'remote_user',
                        'private_key_file', 'ssh_common_args',
                        'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args',
                        'become', 'become_method', 'become_user', 'verbosity',
                        'check'])
        # initialize needed objects
        self.variable_manager = VariableManager()
        self.loader = DataLoader()
        self.passwords = {}
        self.remote_user = remote_user

    def _run(self, ips, key_file, play_file,
             expected_results=[0], play_vars={}):
        """
        Common code used for each run.

        :param ips: IP address(es) to check.
        :type ips: str or list
        :param key_file: Full path to the file holding the private SSH key.
        :type key_file: string
        :param play_file: Path to the ansible play file.
        :type play_file: str
        :param expected_results: List of expected return codes. Default: [0]
        :type expected_results: list
        :returns: Ansible exit code
        :type: int
        """
        if type(ips) != list:
            ips = [ips]

        ssh_args = ('-o StrictHostKeyChecking=no -o '
                    'ControlMaster=auto -o ControlPersist=60s')
        become = {
            'become': None,
            'become_user': None,
        }
        if self.remote_user != 'root':
            self.logger.debug('Using user {0} for ssh communication.'.format(
                self.remote_user))
            become['become'] = True
            become['become_user'] = 'root'

        options = self.Options(
            connection='ssh', module_path=None, forks=1,
            remote_user=self.remote_user, private_key_file=key_file,
            ssh_common_args=ssh_args, ssh_extra_args=ssh_args,
            sftp_extra_args=None, scp_extra_args=None,
            become=become['become'], become_method='sudo',
            become_user=become['become_user'],
            verbosity=None, check=False)
        # create inventory and pass to var manager
        inventory = Inventory(
            loader=self.loader,
            variable_manager=self.variable_manager,
            host_list=ips)
        self.logger.debug('Options: {0}'.format(options))

        group = Group('commissaire_targets')
        for ip in ips:
            host = Host(ip, 22)
            group.add_host(host)

        inventory.groups.update({'commissaire_targets': group})
        self.logger.debug('Inventory: {0}'.format(inventory))

        self.variable_manager.set_inventory(inventory)

        play_source = self.loader.load_from_file(play_file)[0]
        play = Play().load(
            play_source,
            variable_manager=self.variable_manager,
            loader=self.loader)

        # Add any variables provided into the play
        play.vars.update(play_vars)

        self.logger.debug(
            'Running play for hosts {0}: play={1}, vars={2}'.format(
                ips, play_source, play.vars))

        # actually run it
        for cnt in range(0, 3):
            tqm = None
            try:
                tqm = TaskQueueManager(
                    inventory=inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    options=options,
#.........这里部分代码省略.........
开发者ID:cooktheryan,项目名称:commissaire,代码行数:103,代码来源:ansibleapi.py

示例10: parse_yaml_from_file

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
 def parse_yaml_from_file(filepath):
     dl = DataLoader()
     if hasattr(dl, 'set_vault_password'):
         dl.set_vault_password(DEFAULT_VAULT_PASSWORD)
     return dl.load_from_file(filepath)
开发者ID:willthames,项目名称:ansible-lint,代码行数:7,代码来源:utils.py

示例11: main

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
def main():
    args = parse_args()

    for config_file in CONFIG_FILES:
        if os.path.exists(config_file):
            break
    else:
        sys.stdout.write('unable to locate config file at /etc/ansible/infoblox.yaml\n')
        sys.exit(-1)

    try:
        loader = DataLoader()
        config = loader.load_from_file(config_file)
        provider = config.get('provider') or {}
        wapi = WapiInventory(provider)
    except Exception as exc:
        sys.stdout.write(to_text(exc))
        sys.exit(-1)

    if args.host:
        host_filter = {'name': args.host}
    else:
        host_filter = {}

    config_filters = config.get('filters')

    if config_filters.get('view') is not None:
        host_filter['view'] = config_filters['view']

    if config_filters.get('extattrs'):
        extattrs = normalize_extattrs(config_filters['extattrs'])
    else:
        extattrs = {}

    hostvars = {}
    inventory = {
        '_meta': {
            'hostvars': hostvars
        }
    }

    return_fields = ['name', 'view', 'extattrs', 'ipv4addrs']

    hosts = wapi.get_object('record:host',
                            host_filter,
                            extattrs=extattrs,
                            return_fields=return_fields)

    if hosts:
        for item in hosts:
            view = item['view']
            name = item['name']

            if view not in inventory:
                inventory[view] = {'hosts': []}

            inventory[view]['hosts'].append(name)

            hostvars[name] = {
                'view': view
            }

            if item.get('extattrs'):
                for key, value in iteritems(flatten_extattrs(item['extattrs'])):
                    if key.startswith('ansible_'):
                        hostvars[name][key] = value
                    else:
                        if 'extattrs' not in hostvars[name]:
                            hostvars[name]['extattrs'] = {}
                        hostvars[name]['extattrs'][key] = value

    sys.stdout.write(json.dumps(inventory, indent=4))
    sys.exit(0)
开发者ID:awiddersheim,项目名称:ansible,代码行数:75,代码来源:infoblox.py

示例12: _main

# 需要导入模块: from ansible.parsing.dataloader import DataLoader [as 别名]
# 或者: from ansible.parsing.dataloader.DataLoader import load_from_file [as 别名]
def _main():
  parser = argparse.ArgumentParser(
    description="template Ansible style YAML files that only contain variables, using Ansible's codebase",
  )
  parser.add_argument(
    "yaml_files_dir",
    help="folder where the YAML files to template are stored",
  )
  parser.add_argument(
    "--output-as-yaml",
    dest="output_as_yaml",
    action="store_true",
    help="Output resulting variables as YAML instead of JSON",
  )
  args = parser.parse_args()

  # Load variables from the YAML files
  yaml_files_dir = os.path.join(args.yaml_files_dir)
  var_files = [
    os.path.join(yaml_files_dir, file_name)
      for file_name in os.listdir(yaml_files_dir)
  ]
  dl = DataLoader()
  vars_to_template = dict()
  for var_file in var_files:
    vars_to_template.update(dl.load_from_file(var_file))

  templar = Templar(loader=dl)
  result_vars = dict()
  # because some variables depend on the value of other variables and we don't
  # want to spend the effort to do a topological sort on them, we adopt the
  # following strategy:
  #
  # 1. maintain a dict of all successfully templated variables in `result_vars`
  # 2. until the `vars_to_template` dict is empty, do the following:
  #
  #    Try templating each variable using `ansible.template.Templar.template`.
  #
  #    If we get a `AnsibleUndefinedVariable` error, this means that the current
  #    variable depends on another variable. We ignore the error and keep the
  #    variable around for a future round of templating.
  #
  #    Otherwise, we have successfully templated the variable and add it to the
  #    `result_vars` variable. We also add the variable name to the
  #    `successfully_templated_vars` list.
  #
  #    At the end of each templating round, remove all variables in
  #    `successfully_templated_vars` from `vars_to_template`.
  #
  #
  # Note that the above algorithm will only work if all variables required for
  # interpolation are present. Otherwise, it will be stuck in an infinite loop.
  while vars_to_template:
    successfully_templated_vars = []
    for var_name, value in vars_to_template.items():
      try:
        templated_value = templar.template(value)
        result_vars[var_name] = templated_value
        successfully_templated_vars.append(var_name)
        templar.set_available_variables(result_vars.copy())
      except AnsibleUndefinedVariable:
        pass
    for var_name in successfully_templated_vars:
      del vars_to_template[var_name]

  if args.output_as_yaml:
    # NOTE: While it seems from printing `result_vars` that the most fundamental
    #       values are strings, they are in fact
    #       `ansible.parsing.yaml.objects.AnsibleUnicode` objects. Hence when
    #       we use `yaml.dump` to serialize `result_vars`, we get some rather
    #       intimidating-looking stuff that may make it seem like we've gotten
    #       an error when in fact we haven't. So do not be too alarmed by the
    #       voluminous output.
    import yaml
    print yaml.dump(result_vars)
  else:
    import json
    print json.dumps(result_vars)
开发者ID:yanhan,项目名称:templating-ansible-style-yaml-files-with-only-variables,代码行数:80,代码来源:main.py


注:本文中的ansible.parsing.dataloader.DataLoader.load_from_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。