Last active
July 15, 2021 12:26
-
-
Save baiyongzhen/54b281a5249ded97ca4aacd49f210b1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
#!/usr/bin/env python | |
import argparse | |
import json | |
import sys | |
def parse_args(): | |
parser = argparse.ArgumentParser(description="Vagrant inventory script") | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('--list', action='store_true') | |
group.add_argument('--host') | |
return parser.parse_args() | |
def list_running_hosts(): | |
# Example Service Discovery(CoreDNS)부터 서버 정보를 얻어와서 | |
# Host IP 정보를 추출함. | |
hosts = ['192.168.58.3', '192.168.58.4'] | |
return hosts | |
def get_host_details(host): | |
# list_running_hosts 함수에서 전달한 Host IP 정보를 인자로 전달 받음. | |
# host 정보를 기반으로 서버의 상세 정보를 추출해서 json 정보로 리턴합니다. | |
return {'ansible_host': host, | |
'ansible_port': 22, | |
'ansible_user': 'vagrant', | |
'ansible_private_key_file': 'private_id'} | |
def main(): | |
args = parse_args() | |
if args.list: | |
hosts = list_running_hosts() | |
json.dump({'vagrant': hosts}, sys.stdout) | |
else: | |
details = get_host_details(args.host) | |
json.dump(details, sys.stdout) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment