ansible.py 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. from __future__ import unicode_literals
  4. def get_inventory_config(*configs):
  5. children = {}
  6. for config in configs:
  7. if not config:
  8. continue
  9. group = config.get_group()
  10. children[group] = get_inventory_child_config(config)
  11. return {
  12. 'all': {
  13. 'children': children,
  14. }
  15. }
  16. def get_inventory_child_config(config):
  17. ret = {
  18. 'hosts': {},
  19. 'vars': config.ansible_vars(),
  20. }
  21. nodes = config.get_nodes()
  22. for n in nodes:
  23. hosts = ret['hosts']
  24. hosts[n.get_host()] = n.ansible_host_vars()
  25. return ret
  26. class AnsibleBastionHost(object):
  27. def __init__(self, host, user='root'):
  28. self.host = host
  29. self.user = user
  30. def to_option(self):
  31. val = '-o "ProxyCommand ssh -o StrictHostKeyChecking=no' \
  32. ' -o UserKnownHostsFile=/dev/null' \
  33. ' -W %h:%p -q {}@{}"'.format(self.user, self.host)
  34. return val