ansible2json.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. from yaml import load, dump
  33. try:
  34. from yaml import CLoader as Loader, CDumper as Dumper
  35. except ImportError:
  36. from yaml import Loader, Dumper
  37. def main():
  38. parser = argparse.ArgumentParser(prog=sys.argv[0], description="Convert Ansible hosts file to JSON")
  39. parser.add_argument("--output-file", "-o", metavar="<OUTPUT_FILE_PATH>", help="Path to the file to store the JSON", required=True)
  40. parser.add_argument(
  41. "--playbook",
  42. "-p",
  43. metavar="<PLAYBOOK_NAME>",
  44. help="Name of playbook to use to generate file (default: add-to-librenms-playbook.yml)",
  45. default="add-to-librenms-playbook.yml",
  46. )
  47. parser.add_argument(
  48. "--format", "-f", metavar="<JSON|YAML>", help="Output format (default: JSON)", choices=["YAML", "JSON"], default="JSON"
  49. )
  50. parser.add_argument("--limit", metavar="<LIMIT_STRING>", help="Optional set of hosts or groups to limit")
  51. args = parser.parse_args()
  52. os.environ["ANSIBLE_FORCE_COLOR"] = "True"
  53. os.environ["ANSIBLE_HOST_KEY_CHECKING"] = "False"
  54. os.environ["ANSIBLE_PERSISTENT_COMMAND_TIMEOUT"] = "300"
  55. command = [
  56. "ansible-playbook",
  57. "-i",
  58. "inventory/hosts",
  59. "--list-hosts",
  60. "-e",
  61. "ansible_python_interpreter={}".format(sys.executable),
  62. args.playbook,
  63. ]
  64. if args.limit:
  65. command += ["--limit", args.limit]
  66. p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  67. hosts = ""
  68. for c in iter(lambda: p.stdout.read(1), b""):
  69. hosts += c.decode("utf-8")
  70. p.poll()
  71. hlist = []
  72. reading_hosts = False
  73. for h in hosts.split("\n"):
  74. if not reading_hosts and re.search(r"hosts \(\d+\):", h):
  75. reading_hosts = True
  76. continue
  77. if not reading_hosts:
  78. continue
  79. h = h.strip()
  80. if h == "":
  81. continue
  82. hlist.append(h.strip())
  83. fd = None
  84. if args.output_file == "-":
  85. fd = sys.stdout
  86. else:
  87. fd = open(args.output_file, "w")
  88. if args.format == "JSON":
  89. json.dump(hlist, fd, indent=4)
  90. else:
  91. dump(hlist, fd, Dumper=Dumper)
  92. fd.write("\n")
  93. if args.output_file == "-":
  94. fd.flush()
  95. else:
  96. fd.close()
  97. if __name__ == "__main__":
  98. main()