Convert an XML to JSON using ansible

Hi,

THe quickest way is to create your own filter:

  1. Create a ‘filter_plugins’ directory (in the same directory as you playbook or roles).
  2. Create a xml2json.py file in that directory with the following content:

############## cut below

class FilterModule(object):

def filters(self):
return {
‘xml2json’: self.xml2json,
}

def xml2json(self, value):
import xmltodict, json
return json.dumps(xmltodict.parse(value))

############# cut above

Once its there - you can call the filter:

set_fact:
content_json: “{{ content_xml | xml2json }}”

You can obviously also call this ‘in place’ as any other filter.

kind regards
Pshem