Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取嵌套字典中的值的键 总结:_Python_Ansible_Jinja2_Ansible Template - Fatal编程技术网

Python 如何获取嵌套字典中的值的键 总结:

Python 如何获取嵌套字典中的值的键 总结:,python,ansible,jinja2,ansible-template,Python,Ansible,Jinja2,Ansible Template,我有一个嵌套的dict,如示例中所示,我想对它进行循环。对于每个“项”,我希望有嵌套键和值f.e 输入 输出: 为什么我需要它: 我正在通过ansible创建用于管理visual studio代码的角色。我想安装它,创建settings.json(这个问题适用于本例),安装/删除插件。配置应该从以下变量创建:扩展名列表、设置字典、repo路径等。通过此角色,我可以为我的开发环境创建一个文件配置,我可以通过“单击”进行设置 拥有dict configuration.yml: 和示例剧本 file.

我有一个嵌套的dict,如示例中所示,我想对它进行循环。对于每个“项”,我希望有嵌套键和值f.e

输入 输出: 为什么我需要它: 我正在通过ansible创建用于管理visual studio代码的角色。我想安装它,创建settings.json(这个问题适用于本例),安装/删除插件。配置应该从以下变量创建:扩展名列表、设置字典、repo路径等。通过此角色,我可以为我的开发环境创建一个文件配置,我可以通过“单击”进行设置

拥有dict configuration.yml: 和示例剧本 file.j2 希望有如下输出:
使用标准Ansible/Jinja2过滤器很难实现这一点,但您可以创建自己的自定义过滤器。例如

  • 在playbook目录中创建一个名为
    filter\u plugins
  • 创建包含以下内容的python脚本
    filter_plugins/dict2settings.py
  • 在剧本中使用自定义过滤器:
  • 输出文件将包含以下内容:

    {
        "python.linting.enabled": "true",
        "python.linting.ignorePath": [
            ".code",
            ".vscode"
        ],
        "python.linting.pep8Enabled": "true",
        "python.pythonPath": "/usr/bin/python"
    }
    

    考虑到问题中的预期输出实际上不是有效的JSON,您可能需要使用模板或其他方式稍微调整一下格式。但是自定义筛选器只提供了一个简单的键/值字典,因此应该很容易使用。

    欢迎使用堆栈溢出。这个社区是用来回答非常具体的问题的。因此,人们期望你已经做了一些事情,并且正在为一个特定的问题而挣扎。请试着自己写脚本,但是如果你陷入困境,你可以回来问一个关于你遇到的具体问题的问题。阅读了解更多详细信息。您可能会从中受益
    python.lint.where: 'here'
    
    ## vscode config
    code_config:
      python:
        pythonPath: '/usr/bin/python'
        linting:
          enabled: "true"
          pep8Enabled: "true"
          ignorePath:
            - '.code'
            - '.vscode'
    
    - hosts: localhost
      vars_files: 'configuration.yml'
      tasks:
        - name: Hello
          template:
            src: file.j2
            dest: /etc/file.conf
    
    {
    {% for key,value in code_config %}
    {{ key }}: {{ value }},
    {% endfor %}
    }
    
    {
    python.pythonPath: '/usr/bin/python',
    python.linting.enabled: 'true',
    python.linting.pep8Enabled: 'true',
    }
    
    #!/usr/bin/env python
    
    class FilterModule(object):
        def filters(self):
            return {'dict2settings': dict2settings}
    
    def dict2settings(var, config={}, parent_keys=[]):
        if isinstance(var, dict):
            for key,value in var.items():
                parent_keys.append(key)
                dict2settings(value, config, parent_keys)
        else:
            config['.'.join(parent_keys)] = var
            parent_keys.pop()
        return config
    
    - hosts: localhost
      gather_facts: no
    
      vars:
        code_config:
          python:
            pythonPath: '/usr/bin/python'
            linting:
              enabled: "true"
              pep8Enabled: "true"
              ignorePath:
                - '.code'
                - '.vscode'
    
      tasks:
        - copy:
            content: "{{ code_config | dict2settings | to_nice_json }}"
            dest: /etc/file.conf
    
    {
        "python.linting.enabled": "true",
        "python.linting.ignorePath": [
            ".code",
            ".vscode"
        ],
        "python.linting.pep8Enabled": "true",
        "python.pythonPath": "/usr/bin/python"
    }