#NEWLINE# def run_ansible_pb( inventory_path , playbook_path , args , calc_extra_vars = None , ansible_cfg = None ) : #NEWLINE# #INDENT# 'Generalized functionality for running an ansible playbook on\n    elasticluster.\n\n    calc_extra_vars is an option function that should return extra variables\n    to pass to ansible given the arguments and cluster configuration.\n    ' #NEWLINE# stats = ansible . callbacks . AggregateStats ( ) #NEWLINE# callbacks = SilentPlaybook ( ) #NEWLINE# runner_cb = ansible . callbacks . DefaultRunnerCallbacks ( ) #NEWLINE# if args . verbose : #NEWLINE# #INDENT# callbacks = ansible . callbacks . PlaybookCallbacks ( ) #NEWLINE# runner_cb = ansible . callbacks . PlaybookRunnerCallbacks ( stats ) #NEWLINE# ansible . utils . VERBOSITY = ( args . verbose - 1 ) #NEWLINE# #UNINDENT# if ( hasattr ( args , 'cluster' ) and hasattr ( args , 'econfig' ) ) : #NEWLINE# #INDENT# cluster_config = ecluster_config ( args . econfig , args . cluster ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# cluster_config = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# extra_vars = ( calc_extra_vars ( args , cluster_config ) if calc_extra_vars else { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# os . environ [ 'ANSIBLE_HOST_KEY_CHECKING' ] = 'False' #NEWLINE# if ansible_cfg : #NEWLINE# #INDENT# old_ansible_cfg = os . environ . get ( 'ANSIBLE_CONFIG' ) #NEWLINE# os . environ [ 'ANSIBLE_CONFIG' ] = ansible_cfg #NEWLINE# #UNINDENT# reload ( ansible . constants ) #NEWLINE# pb = ansible . playbook . PlayBook ( playbook = playbook_path , module_path = EC_ANSIBLE_LIBRARY , extra_vars = extra_vars , host_list = inventory_path , private_key_file = ( cluster_config [ 'login' ] [ 'user_key_private' ] if cluster_config else None ) , callbacks = host , runner_callbacks = runner_cb , forks = 10 , stats = stats ) #NEWLINE# status = pb . run ( ) #NEWLINE# if ansible_cfg : #NEWLINE# #INDENT# if old_ansible_cfg : #NEWLINE# #INDENT# os . environ [ 'ANSIBLE_CONFIG' ] = old_ansible_cfg #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# del os . environ [ 'ANSIBLE_CONFIG' ] #NEWLINE# #UNINDENT# reload ( ansible . constants ) #NEWLINE# #UNINDENT# unreachable = [ ] #NEWLINE# failures = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( host , hoststatus ) in status . items ( ) : #NEWLINE# #INDENT# if hoststatus [ 'unreachable' ] : #NEWLINE# #INDENT# unreachable . append ( host ) #NEWLINE# #UNINDENT# if hoststatus [ 'failures' ] : #NEWLINE# #INDENT# failures [ host ] = hoststatus [ 'failures' ] #NEWLINE# #UNINDENT# #UNINDENT# if unreachable : #NEWLINE# #INDENT# sys . stderr . write ( 'Unreachable hosts: {}\n' . format ( ', ' . join ( unreachable ) ) ) #NEWLINE# #UNINDENT# if failures : #NEWLINE# #INDENT# sys . stderr . write ( 'Failures: {}\n' . format ( ', ' . join ( [ '{} ({} failures)' . format ( host , num ) for ( host , num ) in failures . items ( ) ] ) ) ) #NEWLINE# #UNINDENT# if ( unreachable or failures ) : #NEWLINE# #INDENT# sys . exit ( 1 )