3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
26 from collections import namedtuple
31 def ansibleSysCall (inventory_path, playbook_path, nodelist, mandatory,
32 envparameters, localparameters, lcm, timeout):
34 print "***> in AnsibleModule.ansibleSysCall"
35 print " EnvParameters: ", envparameters
36 print " LocalParameters:", localparameters
37 print " Inventory: ", inventory_path
38 print " Playbook: ", playbook_path
39 print " NodeList: ", nodelist
40 print " Mandatory: ", mandatory
41 print " Timeout: ", timeout
46 if not envparameters == {}:
47 for key in envparameters:
48 if str_parameters == '':
49 str_parameters = '"' + str(key) + '=\'' + str(envparameters[key]) + '\''
51 str_parameters += ' ' + str(key) + '=\'' + str(envparameters[key]) + '\''
54 if len(str_parameters) > 0:
55 cmd = 'timeout --signal=KILL ' + str(timeout) + \
56 ' ansible-playbook -v --extra-vars ' + str_parameters + ' -i ' + \
57 inventory_path + ' ' + playbook_path
59 cmd = 'timeout --signal=KILL ' + str(timeout) + \
60 ' ansible-playbook -v -i ' + inventory_path + ' ' + playbook_path
64 print "\n =================ANSIBLE STDOUT BEGIN============================================\n"
65 p = subprocess.Popen(cmd, shell=True,
66 stdout=subprocess.PIPE,
67 stderr=subprocess.STDOUT)
69 (stdout_value, err) = p.communicate()
71 stdout_value_cleanup = ''
72 for line in stdout_value:
73 stdout_value_cleanup += line.replace(' ', ' ')
74 stdout_value = stdout_value_cleanup.splitlines()
78 returncode = p.returncode
82 print " ansible-playbook system call timed out"
83 # ansible-playbook system call timed out
84 for line in stdout_value: # p.stdout.readlines():
90 targetnode = envparameters['TargetNode'].split(' ')
92 for line in stdout_value: # p.stdout.readlines():
94 if "PLAY RECAP" in line:
96 if ParseFlag and len(line.strip())>0:
97 str_json += line.strip()
98 if "TASK [debug]" in line:
105 out_json =eval(str_json.split('=>')[1].replace('true','True').replace('false','False'))
107 if 'ping.stdout_lines' in out_json:
108 for node in targetnode:
112 unreachable_flag = '0'
114 for rec in out_json['ping.stdout_lines']:
115 if node in rec and "is alive" in rec:
118 unreachable_flag = '0'
120 for rec in out_json['ping.stdout_lines']:
121 if node in rec and "address not found" in rec:
124 unreachable_flag = '1'
126 retval[ip_address]=[ok_flag, changed_flag, unreachable_flag,
130 for line in stdout_value: # p.stdout.readlines():
132 if ParseFlag and len(line.strip())>0:
133 ip_address = line.split(':')[0].strip()
134 ok_flag = line.split(':')[1].strip().split('=')[1].split('changed')[0].strip()
135 changed_flag = line.split(':')[1].strip().split('=')[2].split('unreachable')[0].strip()
136 unreachable_flag = line.split(':')[1].strip().split('=')[3].split('failed')[0].strip()
137 failed_flag = line.split(':')[1].strip().split('=')[4].strip()
138 retval[ip_address]=[ok_flag, changed_flag, unreachable_flag, failed_flag]
139 if "PLAY RECAP" in line:
143 # retval['p'] = p.wait()
145 print " =================ANSIBLE STDOUT END==============================================\n"
147 return retval, log, returncode
149 if __name__ == '__main__':
151 from multiprocessing import Process, Value, Array, Manager
156 playbook_file = 'ansible_sleep@0.00.yml'
161 p = Process(nodelist=ansible_call, args=('ansible_module_config', playbook_file, nodelist,d, ))
164 print "Process running"