3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Copyright (C) 2017 Amdocs
9 * =============================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 * ============LICENSE_END=========================================================
29 from collections import namedtuple
34 def ansibleSysCall (inventory_path, playbook_path, nodelist, mandatory,
35 envparameters, localparameters, lcm, timeout):
37 print "***> in AnsibleModule.ansibleSysCall"
38 print " EnvParameters: ", envparameters
39 print " LocalParameters:", localparameters
40 print " Inventory: ", inventory_path
41 print " Playbook: ", playbook_path
42 print " NodeList: ", nodelist
43 print " Mandatory: ", mandatory
44 print " Timeout: ", timeout
49 if not envparameters == {}:
50 for key in envparameters:
51 if str_parameters == '':
52 str_parameters = '"' + str(key) + '=\'' + str(envparameters[key]) + '\''
54 str_parameters += ' ' + str(key) + '=\'' + str(envparameters[key]) + '\''
57 if len(str_parameters) > 0:
58 cmd = 'timeout --signal=KILL ' + str(timeout) + \
59 ' ansible-playbook -v --extra-vars ' + str_parameters + ' -i ' + \
60 inventory_path + ' ' + playbook_path
62 cmd = 'timeout --signal=KILL ' + str(timeout) + \
63 ' ansible-playbook -v -i ' + inventory_path + ' ' + playbook_path
67 print "\n =================ANSIBLE STDOUT BEGIN============================================\n"
68 p = subprocess.Popen(cmd, shell=True,
69 stdout=subprocess.PIPE,
70 stderr=subprocess.STDOUT)
72 (stdout_value, err) = p.communicate()
74 stdout_value_cleanup = ''
75 for line in stdout_value:
76 stdout_value_cleanup += line.replace(' ', ' ')
77 stdout_value = stdout_value_cleanup.splitlines()
81 returncode = p.returncode
85 print " ansible-playbook system call timed out"
86 # ansible-playbook system call timed out
87 for line in stdout_value: # p.stdout.readlines():
93 targetnode = envparameters['TargetNode'].split(' ')
95 for line in stdout_value: # p.stdout.readlines():
97 if "PLAY RECAP" in line:
99 if ParseFlag and len(line.strip())>0:
100 str_json += line.strip()
101 if "TASK [debug]" in line:
108 out_json =eval(str_json.split('=>')[1].replace('true','True').replace('false','False'))
110 if 'ping.stdout_lines' in out_json:
111 for node in targetnode:
115 unreachable_flag = '0'
117 for rec in out_json['ping.stdout_lines']:
118 if node in rec and "is alive" in rec:
121 unreachable_flag = '0'
123 for rec in out_json['ping.stdout_lines']:
124 if node in rec and "address not found" in rec:
127 unreachable_flag = '1'
129 retval[ip_address]=[ok_flag, changed_flag, unreachable_flag,
133 for line in stdout_value: # p.stdout.readlines():
135 if ParseFlag and len(line.strip())>0:
136 ip_address = line.split(':')[0].strip()
137 ok_flag = line.split(':')[1].strip().split('=')[1].split('changed')[0].strip()
138 changed_flag = line.split(':')[1].strip().split('=')[2].split('unreachable')[0].strip()
139 unreachable_flag = line.split(':')[1].strip().split('=')[3].split('failed')[0].strip()
140 failed_flag = line.split(':')[1].strip().split('=')[4].strip()
141 retval[ip_address]=[ok_flag, changed_flag, unreachable_flag, failed_flag]
142 if "PLAY RECAP" in line:
146 # retval['p'] = p.wait()
148 print " =================ANSIBLE STDOUT END==============================================\n"
150 return retval, log, returncode
152 if __name__ == '__main__':
154 from multiprocessing import Process, Value, Array, Manager
159 playbook_file = 'ansible_sleep@0.00.yml'
164 p = Process(nodelist=ansible_call, args=('ansible_module_config', playbook_file, nodelist,d, ))
167 print "Process running"