-
-
Save hari819/64016339282fd3bcd64c36a1ce850b63 to your computer and use it in GitHub Desktop.
Revisions
-
Brandon Catubig revised this gist
Dec 20, 2016 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,11 +37,11 @@ def main(): parser = argparse.ArgumentParser(description='Process ansible playbooks') parser.add_argument('playbooks', metavar='PLAYBOOK', nargs='+', help='The name of an Ansible playbook.') parser.add_argument('-e', '--extra_var', metavar='EXTRA_VAR', nargs="+", help="The key=value extra argument", action="append") args = parser.parse_args() playbooks = args.playbooks extra_vars = args.extra_var extra_vars = list(chain.from_iterable(extra_vars)) ansible_string = build_ansible_string(extra_vars) @@ -50,6 +50,8 @@ def main(): print("Number of playbooks to process: {}".format(num_of_playbooks)) func = partial(execute_playbook, ansible_string) return_codes = pool.map(func, playbooks) pool.close() pool.join() print ('-' * 80) fail_counter = 0 for playbook, rc in return_codes: -
Brandon Catubig revised this gist
Dec 20, 2016 . 1 changed file with 22 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,14 +2,17 @@ import subprocess from multiprocessing import Pool from functools import partial import argparse import sys from itertools import chain def execute_playbook(ansible_string, pb): # Note: If ansible fails, it prints to stdout # only system errors are handled by stderr print("Now executing playbook: {}".format(pb)) ansible_string.append(pb) p = subprocess.Popen(ansible_string, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout_output = None while not (stdout_output == '' and p.poll() is not None): stdout_output = p.stdout.readline() @@ -23,16 +26,30 @@ def execute_playbook(pb): print("[-] ERROR: {}".format(stderr)) return (pb, rc) def build_ansible_string(extra_vars): ansible_string = ['ansible-playbook', '-i', 'localhost,', '-c', 'local'] for var in extra_vars: ansible_string.append('-e') ansible_string.append(var) return ansible_string def main(): parser = argparse.ArgumentParser(description='Process ansible playbooks') parser.add_argument('playbooks', metavar='PLAYBOOK', nargs='+', help='The name of an Ansible playbook.') parser.add_argument('-e', '--extra_vars', metavar='EXTRA_VAR', nargs="+", help="The key=value extra argument", action="append") args = parser.parse_args() playbooks = args.playbooks extra_vars = args.extra_vars extra_vars = list(chain.from_iterable(extra_vars)) ansible_string = build_ansible_string(extra_vars) num_of_playbooks = len(playbooks) pool = Pool() print("Number of playbooks to process: {}".format(num_of_playbooks)) func = partial(execute_playbook, ansible_string) return_codes = pool.map(func, playbooks) print ('-' * 80) fail_counter = 0 for playbook, rc in return_codes: -
Brandon Catubig revised this gist
Dec 20, 2016 . 1 changed file with 27 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,20 +3,24 @@ import subprocess from multiprocessing import Pool import argparse import sys def execute_playbook(pb): # Note: If ansible fails, it prints to stdout only system errors are handled # by stdout print("Now executing playbook: {}".format(pb)) p = subprocess.Popen("ansible-playbook -i localhost, -c local {}".format(pb), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout_output = None while not (stdout_output == '' and p.poll() is not None): stdout_output = p.stdout.readline() if stdout_output: # Stream output to stdout print(pb + ' | ' + stdout_output.strip()) rc = p.poll() stdout, stderr = p.communicate() if stderr: print("[-] ERROR: {}".format(stderr)) return (pb, rc) def main(): @@ -26,11 +30,24 @@ def main(): args = parser.parse_args() playbooks = args.playbooks num_of_playbooks = len(playbooks) pool = Pool() print("Number of playbooks to process: {}".format(num_of_playbooks)) return_codes = pool.map(execute_playbook, playbooks) print ('-' * 80) fail_counter = 0 for playbook, rc in return_codes: if rc != 0: print("[-] {} failed to execute properly".format(playbook)) fail_counter += 1 if fail_counter != 0: print("[-] ERROR: Failed to run all playbooks successfully.") sys.exit(255) for pb, rc in return_codes: print("Playbook executed: {}".format(pb)) print("[+] SUCCESS: All playbooks executed successfully.") sys.exit(0) if __name__ == '__main__': main() -
Brandon Catubig revised this gist
Dec 20, 2016 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,6 @@ def execute_playbook(pb): rc = p.poll() stdout, stderr = p.communicate() return (pb, rc) def main(): @@ -31,6 +29,7 @@ def main(): pool = Pool(num_of_playbooks) print("Number of playbooks to process: {}".format(num_of_playbooks)) return_codes = pool.map(execute_playbook, playbooks) print("Here are the playbooks and return codes") print(return_codes) if __name__ == '__main__': -
Brandon Catubig revised this gist
Dec 20, 2016 . 1 changed file with 17 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,37 @@ #!/usr/bin/env python import subprocess from multiprocessing import Pool import argparse def execute_playbook(pb): print("Now executing playbook: {}".format(pb)) p = subprocess.Popen("ansible-playbook -i localhost, -c local {}".format(pb), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: output = p.stdout.readline() if output == '' and p.poll() is not None: break if output: # Write to a log file, output to stdout print output.strip() rc = p.poll() stdout, stderr = p.communicate() print(stdout) print("-" * 80) return (pb, rc) def main(): parser = argparse.ArgumentParser(description='Process ansible playbooks') parser.add_argument('playbooks', metavar='PLAYBOOK', nargs='+', help='The name of an Ansible playbook.') args = parser.parse_args() playbooks = args.playbooks num_of_playbooks = len(playbooks) pool = Pool(num_of_playbooks) print("Number of playbooks to process: {}".format(num_of_playbooks)) return_codes = pool.map(execute_playbook, playbooks) print(return_codes) if __name__ == '__main__': main() -
Brandon Catubig created this gist
Dec 20, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ #!/usr/bin/env python import subprocess import multiprocessing from multiprocessing import Pool import argparse def execute_playbook(pb): print("Now executing playbook: {}".format(pb)) p = subprocess.Popen("ansible-playbook -i localhost, -c local {}".format(pb), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() return stdout, stderr def main(): parser = argparse.ArgumentParser(description='Process ansible playbooks') parser.add_argument('playbooks', metavar='PLAYBOOK', nargs='+', help='The name of an Ansible playbook.') args = parser.parse_args() playbooks = args.playbooks pool = Pool(len(playbooks)) print(len(playbooks)) print(pool.map(execute_playbook, playbooks)) if __name__ == '__main__': main()