Skip to content

Instantly share code, notes, and snippets.

@KeyboardInterrupt
Last active August 14, 2019 11:49
Show Gist options
  • Save KeyboardInterrupt/4d6e2be651a0d5d646df8070ddbb20dd to your computer and use it in GitHub Desktop.
Save KeyboardInterrupt/4d6e2be651a0d5d646df8070ddbb20dd to your computer and use it in GitHub Desktop.
Ansible Custom Fact to gather Pacemaker Cluster Information - This script is a custom fact for ansible that turns `crm configure show` and `crm_mon` output into a fact that can be used in Ansible. i.E. to iterate over all nodes in a cluster, check for the location of a resource and so on.
#!/usr/bin/python
#
# Description:
# This script is a custom fact for ansible that turns `crm configure show` and `crm_mon`
# output into a fact that can be used in Ansible. i.E. to iterate over all
# nodes in a cluster, check for the location of a resource and so on.
#
# Dependencies:
# - xmltodict - https://pypi.org/project/xmltodict/
#
# Author:
# KeyboardInterrupt - www.keyboardinterrupt.com
#
import xmltodict
import json
import subprocess
xml_config = subprocess.check_output(
["crm", "configure", "show", "xml"]
)
xml_status = subprocess.check_output(
["crm_mon", "--as-xml"]
)
dict_config = xmltodict.parse(xml_config ,attr_prefix="")
dict_status = xmltodict.parse(xml_status ,attr_prefix="")
dict_combined = dict_config.copy()
dict_combined.update(dict_status)
json_combined = json.dumps(dict_combined)
print(json_combined)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment