diff --git a/deploy/deploy.py b/deploy/deploy.py new file mode 100755 index 0000000..5a4e506 --- /dev/null +++ b/deploy/deploy.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# PYTHON_ARGCOMPLETE_OK + +import argparse, argcomplete +import subprocess, re + +ANSIBLE="/usr/local/bin/ansible-playbook" +BOOK="main.yml" +HOSTS="hosts" +TAGS=[] +TAGS_LIST=[] + +# +# PREPARE +# +list_tags = str(subprocess.check_output([ANSIBLE, '-i', HOSTS, '--list-tags', BOOK])) +regex = re.compile('\[([a-z, ]+)\]', re.MULTILINE) +find_tags = re.findall(regex, list_tags) +clean_tags = set() +for t in find_tags: + clean_tags.update(t.split(', ')) +TAGS_LIST = list(clean_tags) + +# +# ARGS +# +parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, + description='''Ansible deploy script for Toy Cluser''', + epilog=''' + +Examples: + ./deploy.py + ./deploy.py -t beegfs + ./deploy.py --tags packages ssh +''' +) +parser.add_argument('-t', '--tags', choices=TAGS_LIST, nargs='+') +parser.set_defaults(tags=[]) +argcomplete.autocomplete(parser) +args = parser.parse_args() +TAGS = args.tags + +# +# MAIN +# +command= [ANSIBLE, '--inventory-file', HOSTS] +if len(TAGS) > 0: + command.extend(['--tags', ','.join(TAGS)]) +command.extend([BOOK]) +print (' '.join(command) + '\n') +subprocess.call(command) +