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 * ============LICENSE_END=========================================================
28 from os import listdir
29 from os.path import isfile, join
33 def __init__(self, myhost, myuser, mypasswd, mydb):
34 self.db = pymysql.connect(host=myhost,
38 self.cur = self.db.cursor()
40 def Query (self, myquery, val = None):
45 self.cur.execute(myquery, val)
47 self.cur.execute(myquery)
49 results = self.cur.fetchall()
50 except Exception as e:
57 def loadPlaybook (value, version, ext = '.yml'):
62 # Test if primary key already defined
63 query = "SELECT name FROM playbook WHERE name='" + value +"'"
64 results, error = sqlintf.Query (query)
66 # print "Primary key already defined: Updating playbook"
69 # print "Primary key not defined: Insert new playbook"
70 query = "INSERT INTO playbook (name) VALUES ('" + value + "')"
71 results, error = sqlintf.Query (query)
77 file = open(playbook_path + value + ext, 'r')
78 load_file = file.read()
83 sql = "UPDATE playbook SET value=%s, version=%s, type=%s WHERE name=%s"
85 results, error = sqlintf.Query(sql, (load_file, version, ext, value))
88 # Error loading playbook
92 return errorCode, diag
94 def loadCredentials (hostgroup, hostname, cred):
100 query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'"
101 results = sqlintf.Query (query)
103 print('==>', results)
105 if hostname in str(results):
107 results_hostgroups = results[0][0][1]
109 # print "Record already defined: Updating inventory"
110 if hostgroup in results_hostgroups.split(','):
111 query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
113 "' WHERE hostname='" + hostname + "'"
116 results_hostgroups = results_hostgroups + ',' + hostgroup
118 query = "UPDATE inventory SET hostname='" + hostname + "',credentials='" +\
119 cred + "',hostgroup='" + results_hostgroups + \
120 "' WHERE hostname='" + hostname + "'"
122 results, error = sqlintf.Query (query)
126 query = "INSERT INTO inventory (hostgroup, hostname, credentials) VALUES ('" + \
127 hostgroup + "','" + hostname + "','" + cred + "')"
128 results, error = sqlintf.Query (query)
131 # Error loading credentials
135 return errorCode, diag
138 if __name__ == '__main__':
140 ################################################################
142 ################################################################
143 host="localhost" # your host, usually localhost
144 user="mysql_user_id" # your username
145 passwd="password_4_mysql_user_id" # your password
146 db="ansible" # name of the data base
148 playbook_path = "/home/ubuntu/RestServerOpenSource/"
149 inventory = "/home/ubuntu/RestServerOpenSource/Ansible_inventory"
150 ################################################################
152 onlyfiles = [f for f in listdir(playbook_path)
153 if isfile(join(playbook_path, f))]
155 sqlintf = mySql (host, user, passwd, db)
158 print("Loading playbooks")
159 for file in onlyfiles:
161 name = file.split (".yml")[0]
162 print(" Loading:", name)
163 version = name.split("@")[1]
164 errorCode, diag = loadPlaybook (name, version)
166 print(" Results: Failed - ", diag)
168 print(" Results: Success")
170 name = file.split (".tar.gz")[0]
171 print(" Loading:", name)
172 version = name.split("@")[1]
173 errorCode, diag = loadPlaybook (name, version, ".tar.gz")
175 print("\nLoading inventory")
180 file = open(inventory, 'r')
184 if '[' in line and ']' in line:
185 hostgroup = line.strip().replace('[','').replace(']','')
187 elif hostgroup and len(line.strip())>0:
188 host = line.strip().split(" ")[0]
189 credentials = line.replace(host,"")
190 inv[hostgroup][host] = credentials
194 for hostgroup in inv:
195 print(" Loading:", hostgroup)
198 for hostname in inv[hostgroup]:
199 cred = inv[hostgroup][hostname]
200 errorCode, diag = loadCredentials (hostgroup, hostname, cred)
202 print(" Results: Failed - ", diag)
204 print(" Results: Success")