ansible2json.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2017-2020 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. import sys
  27. import re
  28. import subprocess
  29. import os
  30. import json
  31. import argparse
  32. def main():
  33. parser = argparse.ArgumentParser(prog=sys.argv[0], description="Convert Ansible hosts file to JSON")
  34. parser.add_argument("--output-file", "-o", metavar="<OUTPUT_FILE_PATH>", help="Path to the file to store the JSON", required=True)
  35. args = parser.parse_args()
  36. os.environ["ANSIBLE_FORCE_COLOR"] = "True"
  37. os.environ["ANSIBLE_HOST_KEY_CHECKING"] = "False"
  38. os.environ["ANSIBLE_PERSISTENT_COMMAND_TIMEOUT"] = "300"
  39. command = [
  40. "ansible-playbook",
  41. "-i",
  42. "inventory/hosts",
  43. "--list-hosts",
  44. "-e",
  45. "ansible_python_interpreter={}".format(sys.executable),
  46. "add-to-librenms-playbook.yml",
  47. ]
  48. p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  49. hosts = ""
  50. for c in iter(lambda: p.stdout.read(1), b""):
  51. hosts += c.decode("utf-8")
  52. p.poll()
  53. hlist = []
  54. reading_hosts = False
  55. for h in hosts.split("\n"):
  56. if not reading_hosts and re.search(r"hosts \(\d+\):", h):
  57. reading_hosts = True
  58. continue
  59. if not reading_hosts:
  60. continue
  61. h = h.strip()
  62. if h == "":
  63. continue
  64. hlist.append(h.strip())
  65. with open(args.output_file, "w") as fd:
  66. json.dump(hlist, fd, indent=4)
  67. if __name__ == "__main__":
  68. main()