@@ -28,23 +28,24 @@ def destroy_krb_ticket(self):
28
28
29
29
30
30
def get_facts_puppetdb (self , apiurl , facts , hostgroup ):
31
- url = '%s/facts' % apiurl
32
- if 'v3' in apiurl :
33
- query_base = '["and",["or",%s],["in", "certname", ["extract", "certname", ["select-facts", ["and", ["=", "name", "hostgroup"], ["~", "value", "%s"]]]]]]'
34
- else :
35
- query_base = '["and",["or",%s],["in", "certname", ["extract", "certname", ["select_facts", ["and", ["=", "name", "hostgroup"], ["~", "value", "%s"]]]]]]'
36
- query_facts = ',' .join (['["=","name","%s"]' % fact for fact in facts ])
37
- query = query_base % (query_facts , hostgroup )
31
+
32
+ query_facts = ',' .join (['facts.%s' % fact for fact in facts ])
33
+
34
+ # Strip / to cover cases of where hostgroup has been set to "top/"
35
+ hostgroup_parts = hostgroup .rstrip ('/' ).split ('/' )
36
+ hostgroup_filter = ' and ' .join ([f'facts.hostgroup_{ i } = "{ part } "' for i , part in enumerate (hostgroup_parts )])
37
+
38
+ query = 'inventory[certname,%s]{%s}' % (query_facts , hostgroup_filter )
38
39
39
40
headers = {
40
41
'Content-Type' : 'application/json' ,
41
42
'Accept' : 'application/json, version=2' ,
42
- 'User-Agent' : 'rundeck_puppetdb_nodes/2.1 .0'
43
+ 'User-Agent' : 'rundeck_puppetdb_nodes/3.0 .0'
43
44
}
44
45
payload = {'query' : query }
45
46
46
- logging .info ("Getting facts from '%s', query: '%s'" , url , query )
47
- r = requests .get (url , params = payload , headers = headers , auth = HTTPKerberosAuth ())
47
+ logging .info ("Getting facts from '%s', query: '%s'" , apiurl , query )
48
+ r = requests .get (apiurl , params = payload , headers = headers , auth = HTTPKerberosAuth ())
48
49
49
50
# pylint: disable=no-member
50
51
if r .status_code == requests .codes .ok :
@@ -65,17 +66,16 @@ def print_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist):
65
66
data = defaultdict (lambda : {})
66
67
67
68
if raw_data != None :
68
- for entry in raw_data :
69
- data [entry ['certname' ]] = dict (list (data [entry ['certname' ]].items ()) + [(entry ['name' ], entry ['value' ])])
70
-
71
69
logging .info ("Printing node list using standard output..." )
72
- for node in data . keys () :
73
- print ('%s:' % node )
74
- print (" " * 4 + "hostname: " + node )
70
+ for entry in raw_data :
71
+ print ('%s:' % entry [ 'certname' ] )
72
+ print (" " * 4 + "hostname: " + entry [ 'certname' ] )
75
73
print (" " * 4 + "username: " + sshuser )
76
74
for fact in factlist :
77
- if fact in data [node ]:
78
- print (" " * 4 + fact + ": " + str (data [node ][fact ]) )
75
+ factkey = "facts.%s" % fact
76
+ if factkey in entry :
77
+ print (" " * 4 + factkey + ": " + str (entry [factkey ]))
78
+
79
79
logging .info ("Node list printed successfully" )
80
80
81
81
else :
@@ -90,21 +90,18 @@ def store_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist, filename):
90
90
'''
91
91
factlist .extend (["operatingsystem" , "operatingsystemrelease" , "hostgroup" ])
92
92
raw_data = self .get_facts_puppetdb (apiurl , factlist , hostgroup )
93
- data = defaultdict (lambda : {})
94
93
95
94
if raw_data != None :
96
- for entry in raw_data :
97
- data [entry ['certname' ]] = dict (list (data [entry ['certname' ]].items ()) + [(entry ['name' ], entry ['value' ])])
98
-
99
95
logging .info ("Saving node list in '%s'..." , filename )
100
96
with open (filename , 'w' ) as file :
101
- for node in data . keys () :
102
- file .write ('%s:\n ' % node )
103
- file .write (" " * 4 + "hostname: " + node + '\n ' )
104
- file .write (" " * 4 + "username: " + sshuser + '\n ' )
97
+ for entry in raw_data :
98
+ file .write ('%s:' % entry [ 'certname' ] + ' \n ' )
99
+ file .write (" " * 4 + "hostname: " + entry [ 'certname' ] + '\n ' )
100
+ file .write (" " * 4 + "username: " + sshuser + '\n ' )
105
101
for fact in factlist :
106
- if fact in data [node ]:
107
- file .write (" " * 4 + fact + ": " + str (data [node ][fact ]) + '\n ' )
102
+ factkey = "facts.%s" % fact
103
+ if factkey in entry :
104
+ file .write (" " * 4 + factkey + ": " + str (entry [factkey ]) + '\n ' )
108
105
logging .info ('Node list saved successfully' )
109
106
110
107
# trick to avoid Rundeck complain when no output is printed out
0 commit comments