是否可以使用Jinja / Ansible映射多个属性?

时间:2015-07-28 19:09:15

标签: jinja2 ansible

我想构建一个输出,显示变量的键和值。

以下作品完美无缺......

# Format in Ansible

msg="{{ php_command_result.results | map(attribute='item') | join(', ') }}"

# Output
{'value': {'svn_tag': '20150703r1_6.36_homeland'}, 'key': 'ui'}, {'value': {'svn_tag': '20150702r1_6.36_homeland'}, 'key': 'api'}

我希望将keysvn_tag一起显示为:

我可以显示keysvn_tag但是将它们放在一起不起作用。

msg="{{ php_command_result.results | map(attribute='item.key') | join(', ') }}"

# Output
ui, api

但是,这就是我想要的。

# Desired Output
api - 20150702r1_6.36_homeland
ui - 20150703r1_6.36_homeland

5 个答案:

答案 0 :(得分:7)

使用Jinja statements

- set_fact:
    php_command_result:
      results: [{"value":{"svn_tag":"20150703r1_6.36_homeland"},"key":"ui"},{"value":{"svn_tag":"20150702r1_6.36_homeland"},"key":"api"}]

- debug:
    msg: "{% for result in php_command_result.results %}\
        {{ result.key }} - {{ result.value.svn_tag }} |
      {% endfor %}"

输出:

ok: [localhost] => {
    "msg": "ui - 20150703r1_6.36_homeland | api - 20150702r1_6.36_homeland | "
}

如果您希望结果分开:

- debug:
    msg: "{% set output = [] %}\
        {% for result in php_command_result.results %}\
          {{ output.append( result.key ~ ' - ' ~ result.value.svn_tag) }}\
        {% endfor %}\
      {{ output }}"

输出:

ok: [localhost] => {
    "msg": [
        "ui - 20150703r1_6.36_homeland", 
        "api - 20150702r1_6.36_homeland"
    ]
}

如果需要,可将其中任何一个放在一行:

- debug:
    msg: "{% for result in php_command_result.results %}{{ result.key }} - {{ result.value.svn_tag }} | {% endfor %}"

- debug:
    msg: "{% set output = [] %}{% for result in php_command_result.results %}{{ output.append( result.key ~ ' - ' ~ result.value.svn_tag) }}{% endfor %}{{ output }}"

答案 1 :(得分:3)

您可以使用以下技术来实现:

  1. 创建filter_plugin。在ansible.cfg中添加filter_plugins = <path to the folder>。然后创建一个文件my_plugin.py

    class FilterModule(object):
    ''' Custom filter '''
        def filters(self, my_arg):
           return <parse it here......>
    
  2. 示例:

    playbook.yml

    ---
    - hosts: localhost
      gather_facts: no
      connection: local
      tasks:
        - set_fact: 
            php_command_result:
              results: {'value': {'svn_tag': '20150703r1_6.36_homeland'}, 'key': 'ui'}
        - debug: msg="Hey look what I got '{{ php_command_result.results | a }}'"
    

    my_plugin.py

    import json
    
    class FilterModule(object):
        def filters(self):
          return {'a': a}
    
    def a(a):
      r = '%s - %s' % (a['key'], a['value']['svn_tag'])
      return r
    
    1. 快速简便的方法:只需使用python/php/shell或您喜欢的shell模块。像这样:

      - name: Pars output
        shell: python -c "import json; json.loads('{{ php_command_result.results }}') ....
      

答案 2 :(得分:3)

这是没有自定义filter_plugin或运行shell命令的解决方案。但是,它需要在with_items循环( php_fmt )中设置其他事实。

- hosts: localhost
  connection: local
  gather_facts: false 
  tasks:
    - set_fact: 
        php_command_result:
          results: '[{"value":{"svn_tag":"20150703r1_6.36_homeland"},"key":"ui"},{"value":{"svn_tag":"20150702r1_6.36_homeland"},"key":"api"}]'

    - set_fact:
        php_fmt: "{{ php_fmt|default([])|union([item.key+' -- '+item.value.svn_tag ]) }}"
      with_items: "{{ php_command_result.results }}"

    - debug: 
        msg: "{{php_fmt|join(',')}}"

答案 3 :(得分:2)

这是使用filter_plugins的另一个答案,我发现它非常易于使用。

如果仍然有人需要此代码,则可以使用以下代码(放入playbooks / filter_plugins / mapattributes.py):

#!/usr/bin/env python
class FilterModule(object):
  def filters(self):
    return { 'mapattributes': self.mapattributes }

  def mapattributes(self, list_of_dicts, list_of_keys):
    l = []
    for di in list_of_dicts:
      newdi = { }
      for key in list_of_keys:
        # newdi[key] = di[key]
        if di.get(key, None) != None:
          newdi[key] = di[key]

      l.append(newdi)
    return l

并且说您有此列表,您只需要键入以下内容即可:

    INTERFACES:
      - { name: GigabitEthernet1/0/24 , enabled: yes, state: up , description: FRONT }
      - { name: GigabitEthernet1/0/9  , enabled: yes, state: up , description: BACK  }

让我们创建另一个仅过滤所需key:values

的变量
    test: "{{ INTERFACES | mapattributes(['name', 'description']) }}"

测试输出

    - debug:
        var: test
ok: [R1] => {
    "test": [
        {
            "description": "FRONT",
            "name": "GigabitEthernet1/0/24"
        },
        {
            "description": "BACK",
            "name": "GigabitEthernet1/0/9"
        }
    ]
}

这使我拥有一本大字典,只切了我需要传递给ios_interface/aggregate的键

感谢Nee6ione,我在托盘/ jinja github issue

上找到了它

答案 4 :(得分:0)

当我寻找一种处理类似任务的方法时,我发现了这个问题,但我希望输出是包含各个元素的另一个JSON列表。我花了很多时间在mapfilter甚至Jinja2 for循环上进行转移,所以我也将答案发布在这里。只需创建以下文件并通过ansible-playbook运行即可:

- hosts: localhost
  connection: local
  vars:
    to_test: [ {'value': {'svn_tag': '20150703r1_6.36_homeland'}, 'key': 'ui'}, {'value': {'svn_tag': '20150702r1_6.36_homeland'}, 'key': 'api'} ]
  tasks:
  - debug:
      msg: "to_test: {{ to_test }}"
  - debug:
      msg: "to_test reduced: {{ to_test | json_query('[].{key: key, svn_tag: value.svn_tag}') | list }}"
  - debug:
      msg: "to_test reduced: {{ to_test | json_query(query1) | list }}"
    vars:
      query1: "[].{key: key, svn_tag: value.svn_tag}"

最终结果是一个不错的JSON数组:

[{'key': 'ui', 'svn_tag': '20150703r1_6.36_homeland'}, {'key': 'api', 'svn_tag': '20150702r1_6.36_homeland'}]"