I would like to build an output that shows the key and value of a variable.
The following works perfectly ...
# 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'}
What I would like is to show the key
and svn_tag
together as so:
I'm able to display either the key
or svn_tag
but getting them to go together doesn't work.
msg="{{ php_command_result.results | map(attribute='item.key') | join(', ') }}"
# Output
ui, api
However, this is what I want.
# Desired Output
api - 20150702r1_6.36_homeland
ui - 20150703r1_6.36_homeland
You can do it by using the following techniques:
Create filter_plugin
. Add filter_plugins = <path to the folder>
in ansible.cfg. Then create a file say my_plugin.py
:
class FilterModule(object):
''' Custom filter '''
def filters(self, my_arg):
return <parse it here......>
Example:
---
- 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 }}'"
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
Fast and easy approach: just use python/php/shell
or what ever you prefer with shell
module. Something like this:
- name: Pars output
shell: python -c "import json; json.loads('{{ php_command_result.results }}') ....