我想构建一个输出,显示变量的键和值.
以下工作完美......
# 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'}
Run Code Online (Sandbox Code Playgroud)
我想要的是如此展示key和svn_tag一起:
我能够显示无论是key或svn_tag,但让他们走在一起也不行.
msg="{{ php_command_result.results | map(attribute='item.key') | join(', ') }}"
# Output
ui, api
Run Code Online (Sandbox Code Playgroud)
但是,这就是我想要的.
# Desired Output
api - 20150702r1_6.36_homeland
ui - 20150703r1_6.36_homeland
Run Code Online (Sandbox Code Playgroud)
使用Jinja语句:
- 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 %}"
Run Code Online (Sandbox Code Playgroud)
输出:
ok: [localhost] => {
"msg": "ui - 20150703r1_6.36_homeland | api - 20150702r1_6.36_homeland | "
}
Run Code Online (Sandbox Code Playgroud)
如果您希望结果分开:
- debug:
msg: "{% set output = [] %}\
{% for result in php_command_result.results %}\
{{ output.append( result.key ~ ' - ' ~ result.value.svn_tag) }}\
{% endfor %}\
{{ output }}"
Run Code Online (Sandbox Code Playgroud)
输出:
ok: [localhost] => {
"msg": [
"ui - 20150703r1_6.36_homeland",
"api - 20150702r1_6.36_homeland"
]
}
Run Code Online (Sandbox Code Playgroud)
如果需要,可以将这些中的任何一个放在一行上:
- 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 }}"
Run Code Online (Sandbox Code Playgroud)
当我搜索处理类似任务的方法时,我发现了这个问题,但我希望输出是另一个包含各个元素的 JSON 列表。我花了很多注意力map,filter甚至 Jinja2for循环,所以我也将在这里发布答案。只需创建以下文件并通过以下方式运行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}"
Run Code Online (Sandbox Code Playgroud)
最终结果是一个漂亮的 JSON 数组:
[{'key': 'ui', 'svn_tag': '20150703r1_6.36_homeland'}, {'key': 'api', 'svn_tag': '20150702r1_6.36_homeland'}]"
Run Code Online (Sandbox Code Playgroud)
您可以使用以下技术来做到这一点:
创建filter_plugin. 添加filter_plugins = <path to the folder>ansible.cfg。然后创建一个文件说my_plugin.py:
class FilterModule(object):
''' Custom filter '''
def filters(self, my_arg):
return <parse it here......>
Run Code Online (Sandbox Code Playgroud)例子:
---
- 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 }}'"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
快速简便的方法:只需使用模块python/php/shell或您喜欢的任何shell模块。像这样的东西:
- name: Pars output
shell: python -c "import json; json.loads('{{ php_command_result.results }}') ....
Run Code Online (Sandbox Code Playgroud)小智 5
这是没有自定义 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(',')}}"
Run Code Online (Sandbox Code Playgroud)
这是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
Run Code Online (Sandbox Code Playgroud)
假设你有这个列表,你只需要从中输入:
INTERFACES:
- { name: GigabitEthernet1/0/24 , enabled: yes, state: up , description: FRONT }
- { name: GigabitEthernet1/0/9 , enabled: yes, state: up , description: BACK }
Run Code Online (Sandbox Code Playgroud)
让我们创建另一个只过滤需要的变量 key:values
test: "{{ INTERFACES | mapattributes(['name', 'description']) }}"
Run Code Online (Sandbox Code Playgroud)
测试输出
- debug:
var: test
Run Code Online (Sandbox Code Playgroud)
ok: [R1] => {
"test": [
{
"description": "FRONT",
"name": "GigabitEthernet1/0/24"
},
{
"description": "BACK",
"name": "GigabitEthernet1/0/9"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这让我有一个大字典,只切片我需要传递给的键 ios_interface/aggregate
感谢 Nee6ione,我在托盘/jinja github问题上找到了它