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 os import listdir
30 from os.path import isfile, join
34 def __init__(self, myhost, myuser, mypasswd, mydb):
35 self.db = pymysql.connect(host=myhost,
39 self.cur = self.db.cursor()
41 def Query (self, myquery, val = None):
46 self.cur.execute(myquery, val)
48 self.cur.execute(myquery)
50 results = self.cur.fetchall()
58 def loadPlaybook (value, version, ext = '.yml'):
63 # Test if primary key already defined
64 query = "SELECT name FROM playbook WHERE name='" + value +"'"
65 results, error = sqlintf.Query (query)
67 # print "Primary key already defined: Updating playbook"
70 # print "Primary key not defined: Insert new playbook"
71 query = "INSERT INTO playbook (name) VALUES ('" + value + "')"
72 results, error = sqlintf.Query (query)
78 file = open(playbook_path + value + ext, 'r')
79 load_file = file.read()
84 sql = "UPDATE playbook SET value=%s, version=%s, type=%s WHERE name=%s"
86 results, error = sqlintf.Query(sql, (load_file, version, ext, value))
89 # Error loading playbook
93 return errorCode, diag
95 def loadCredentials (hostgroup, hostname, cred):
101 query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'"
102 results = sqlintf.Query (query)
106 if hostname in str(results):
108 results_hostgroups = results[0][0][1]
110 # print "Record already defined: Updating inventory"
111 if hostgroup in results_hostgroups.split(','):
112 query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
114 "' WHERE hostname='" + hostname + "'"
117 results_hostgroups = results_hostgroups + ',' + hostgroup
119 query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
120 cred + "',hostgroup='" + results_hostgroups + \
121 "' WHERE hostname='" + hostname + "'"
123 results, error = sqlintf.Query (query)
127 query = "INSERT INTO inventory (hostgroup, hostname, credentials) VALUES ('" + \
128 hostgroup + "','" + hostname + "','" + cred + "')"
129 results, error = sqlintf.Query (query)
132 # Error loading credentials
136 return errorCode, diag
139 if __name__ == '__main__':
141 ################################################################
143 ################################################################
144 host="localhost" # your host, usually localhost
145 user="mysql_user_id" # your username
146 passwd="password_4_mysql_user_id" # your password
147 db="ansible" # name of the data base
149 playbook_path = "/home/ubuntu/RestServerOpenSource/"
150 inventory = "/home/ubuntu/RestServerOpenSource/Ansible_inventory"
151 ################################################################
153 onlyfiles = [f for f in listdir(playbook_path)
154 if isfile(join(playbook_path, f))]
156 sqlintf = mySql (host, user, passwd, db)
159 print "Loading playbooks"
160 for file in onlyfiles:
162 name = file.split (".yml")[0]
163 print " Loading:", name
164 version = name.split("@")[1]
165 errorCode, diag = loadPlaybook (name, version)
167 print " Results: Failed - ", diag
169 print " Results: Success"
171 name = file.split (".tar.gz")[0]
172 print " Loading:", name
173 version = name.split("@")[1]
174 errorCode, diag = loadPlaybook (name, version, ".tar.gz")
176 print "\nLoading inventory"
181 file = open(inventory, 'r')
185 if '[' in line and ']' in line:
186 hostgroup = line.strip().replace('[','').replace(']','')
188 elif hostgroup and len(line.strip())>0:
189 host = line.strip().split(" ")[0]
190 credentials = line.replace(host,"")
191 inv[hostgroup][host] = credentials
195 for hostgroup in inv:
196 print " Loading:", hostgroup
199 for hostname in inv[hostgroup]:
200 cred = inv[hostgroup][hostname]
201 errorCode, diag = loadCredentials (hostgroup, hostname, cred)
203 print " Results: Failed - ", diag
205 print " Results: Success"