1
- #! /usr/bin/env python
1
+ #!/usr/bin/env python
2
2
# Copyright 2019, RackN
3
3
#
4
4
# Licensed under the Apache License, Version 2.0 (the "License");
14
14
# limitations under the License.
15
15
16
16
# pip install requests
17
- import requests , argparse , json , urllib3 , os
17
+ import argparse
18
+ import json
19
+ import logging
20
+ import os
21
+
22
+ try :
23
+ import requests
24
+ import urllib3
25
+ except ImportError :
26
+ print ("Missing requests. pip install requests" )
27
+ raise SystemExit
18
28
19
29
'''
20
- Usage: https://github.com/digitalrebar/provision/v4/ tree/master/integration /ansible
30
+ Usage: https://github.com/digitalrebar/provision/tree/v4/integrations /ansible
21
31
22
32
example: ansible -i drpmachines.py all -a "uname -a"
23
33
'''
24
34
25
- def main ():
35
+ urllib3 .disable_warnings ()
36
+
37
+
38
+ def setup_logging ():
39
+ fmat = '%(asctime)-15s %(name)s %(message)s'
40
+ logging .basicConfig (format = fmat , level = logging .DEBUG )
41
+
42
+
43
+ def setup_parser ():
44
+ parser = argparse .ArgumentParser (
45
+ description = "Ansible dynamic inventory via DigitalRebar"
46
+ )
47
+ parser .add_argument (
48
+ "--list" ,
49
+ help = "Ansible inventory of all of the deployments" ,
50
+ action = "store_true" ,
51
+ dest = "list_inventory"
52
+ )
53
+ parser .add_argument (
54
+ "--host" ,
55
+ help = "Ansible inventory of a particular host" ,
56
+ action = "store" ,
57
+ dest = "ansible_host" ,
58
+ type = str
59
+ )
60
+ parser .add_argument (
61
+ "--debug" ,
62
+ action = "store_true" ,
63
+ help = "Enable debug logging."
64
+ )
65
+ return parser .parse_args ()
26
66
27
- inventory = { "_meta" : { "hostvars" : {} } }
28
67
68
+ def main ():
69
+ cli_args = setup_parser ()
70
+ if cli_args .debug :
71
+ setup_logging ()
72
+ inventory = {"_meta" : {"hostvars" : {}}}
29
73
# change these values to match your DigitalRebar installation
30
74
addr = os .getenv ('RS_ENDPOINT' , "https://127.0.0.1:8092" )
75
+ logging .debug ("RS_ENDPOINT: {0}" .format (addr ))
31
76
ups = os .getenv ('RS_KEY' , "rocketskates:r0cketsk8ts" )
77
+ logging .debug ("RS_KEY: {0}" .format (ups ))
32
78
profile = os .getenv ('RS_ANSIBLE' , "all_machines" )
79
+ logging .debug ("RS_ANSIBLE: {0}" .format (profile ))
33
80
host_address = os .getenv ('RS_HOST_ADDRESS' , "internal" )
81
+ logging .debug ("RS_HOST_ADDRESS: {0}" .format (host_address ))
34
82
ansible_user = os .getenv ('RS_ANSIBLE_USER' , "root" )
83
+ logging .debug ("RS_ANSIBLE_USER: {0}" .format (ansible_user ))
35
84
parent_key = os .getenv ('RS_ANSIBLE_PARENT' , "ansible/children" )
36
- arr = ups .split (":" )
37
- user = arr [0 ]
38
- password = arr [1 ]
39
-
40
- # Argument parsing
41
- parser = argparse .ArgumentParser (description = "Ansible dynamic inventory via DigitalRebar" )
42
- parser .add_argument ("--list" , help = "Ansible inventory of all of the deployments" ,
43
- action = "store_true" , dest = "list_inventory" )
44
- parser .add_argument ("--host" ,
45
- help = "Ansible inventory of a particular host" , action = "store" ,
46
- dest = "ansible_host" , type = str )
47
-
48
- cli_args = parser .parse_args ()
85
+ logging .debug ("RS_ANSIBLE_PARENT: {0}" .format (parent_key ))
86
+ user , password = ups .split (":" )
87
+
49
88
list_inventory = cli_args .list_inventory
50
89
ansible_host = cli_args .ansible_host
51
-
52
- Headers = {'content-type' : 'application/json' }
53
- urllib3 .disable_warnings ()
54
- inventory = {'all' : { 'hosts' : [] }, '_meta' : { 'hostvars' : {}} }
90
+ headers = {'content-type' : 'application/json' }
91
+ inventory = {'all' : {'hosts' : []}, '_meta' : {'hostvars' : {}}}
55
92
inventory ["_meta" ]["rebar_url" ] = addr
56
93
inventory ["_meta" ]["rebar_user" ] = user
57
94
inventory ["_meta" ]["rebar_profile" ] = profile
@@ -61,21 +98,26 @@ def main():
61
98
profiles_vars = {}
62
99
hostvars = {}
63
100
64
- URL = addr + "/api/v3/machines"
101
+ url = addr + "/api/v3/machines"
65
102
if list_inventory :
66
103
if profile != "all_machines" :
67
- URL += "?ansible=Eq(" + profile + ")"
104
+ url += "?ansible=Eq({0})" . format ( profile )
68
105
else :
69
106
if ansible_host :
70
- URL += "?Name=" + ansible_host
107
+ url += "?Name={0}" . format ( ansible_host )
71
108
else :
72
109
if profile != "all_machines" :
73
- URL += "?ansible=Eq(" + profile + ")"
110
+ url += "?ansible=Eq({0})" . format ( profile )
74
111
75
- raw = requests .get (URL ,headers = Headers ,auth = (user ,password ),verify = False )
112
+ raw = requests .get (
113
+ url ,
114
+ headers = headers ,
115
+ auth = (user , password ),
116
+ verify = False
117
+ )
76
118
77
- IGNORE_PARAMS = ["gohai-inventory" ,"inventory/data" ,"change-stage/map" ]
78
- if raw .status_code == 200 :
119
+ ignore_params = ["gohai-inventory" , "inventory/data" , "change-stage/map" ]
120
+ if raw .status_code == 200 :
79
121
for machine in raw .json ():
80
122
name = machine [u'Name' ]
81
123
inventory ["all" ]["hosts" ].extend ([name ])
@@ -87,21 +129,27 @@ def main():
87
129
myvars ["ansible_user" ] = ansible_user
88
130
myvars ["rebar_uuid" ] = machine [u"Uuid" ]
89
131
for k in machine [u'Params' ]:
90
- if k not in IGNORE_PARAMS :
132
+ if k not in ignore_params :
91
133
myvars [k ] = machine [u'Params' ][k ]
92
134
inventory ["_meta" ]["hostvars" ][name ] = myvars
93
135
else :
94
136
raise IOError (raw .text )
95
137
96
138
if ansible_host is None :
97
- groups = requests .get (addr + "/api/v3/profiles" ,headers = Headers ,auth = (user ,password ),verify = False )
139
+ groups = requests .get (
140
+ addr + "/api/v3/profiles" ,
141
+ headers = headers ,
142
+ auth = (user , password ),
143
+ verify = False
144
+ )
98
145
if groups .status_code == 200 :
99
146
for group in groups .json ():
100
147
name = group [u'Name' ]
101
148
if name != "global" and name != "rackn-license" :
102
- inventory [name ] = { "hosts" : [], "vars" : [] }
149
+ inventory [name ] = {"hosts" : [], "vars" : []}
103
150
gvars = hostvars .copy ()
104
- if 'Profiles' in group .keys () and len (group [u'Profiles' ])> 0 :
151
+ if 'Profiles' in group .keys () \
152
+ and len (group [u'Profiles' ]) > 0 :
105
153
inventory [name ]["children" ] = group [u'Profiles' ]
106
154
for k in group [u'Params' ]:
107
155
v = group [u'Params' ][k ]
@@ -110,7 +158,14 @@ def main():
110
158
else :
111
159
gvars [k ] = v
112
160
inventory [name ]["vars" ] = gvars
113
- hosts = requests .get (addr + "/api/v3/machines?slim=Params&Profiles=In(" + name + ")" ,headers = Headers ,auth = (user ,password ),verify = False )
161
+ hosts = requests .get (
162
+ "{0}/api/v3/machines?slim=Params&Profiles=In({1})" .format (
163
+ addr , name
164
+ ),
165
+ headers = headers ,
166
+ auth = (user , password ),
167
+ verify = False
168
+ )
114
169
if hosts .status_code == 200 :
115
170
inventory [name ]["hosts" ] = []
116
171
for host in hosts .json ():
@@ -121,5 +176,6 @@ def main():
121
176
122
177
print (json .dumps (inventory ))
123
178
179
+
124
180
if __name__ == "__main__" :
125
181
main ()
0 commit comments