-
-
Save hari819/64016339282fd3bcd64c36a1ce850b63 to your computer and use it in GitHub Desktop.
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 characters
| #!/usr/bin/env python | |
| 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(): | |
| 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() | |
| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment