Skip to content

Commit da3b086

Browse files
authored
Merge pull request #9 from traylenator/inventory
Use inventory endpoint, explode hostgroup query
2 parents a201f75 + bd9d3b0 commit da3b086

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [v3.0.0](https://github.com/cernops/rundeck-puppetdb-nodes/tree/v3.0.0)
6+
- Use PDB /inventory endpoint
7+
- Explode full hostgroup and query with hostgroup elements
8+
59
## [v2.1.0](https://github.com/cernops/rundeck-puppetdb-nodes/tree/v2.1.0)
610

711
- Set custom user-agent for puppetdb requests to "rundeck_puppetdb_nodes"

rundeck-puppetdb-nodes-plugin/contents/rundeck_puppetdb_nodes.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@ def destroy_krb_ticket(self):
2828

2929

3030
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)
3839

3940
headers = {
4041
'Content-Type': 'application/json',
4142
'Accept': 'application/json, version=2',
42-
'User-Agent': 'rundeck_puppetdb_nodes/2.1.0'
43+
'User-Agent': 'rundeck_puppetdb_nodes/3.0.0'
4344
}
4445
payload = {'query': query}
4546

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())
4849

4950
# pylint: disable=no-member
5051
if r.status_code == requests.codes.ok:
@@ -65,17 +66,16 @@ def print_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist):
6566
data = defaultdict(lambda: {})
6667

6768
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-
7169
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'])
7573
print(" "*4 + "username: " + sshuser)
7674
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+
7979
logging.info("Node list printed successfully")
8080

8181
else:
@@ -90,21 +90,18 @@ def store_puppetdb_nodes(self, apiurl, hostgroup, sshuser, factlist, filename):
9090
'''
9191
factlist.extend(["operatingsystem", "operatingsystemrelease", "hostgroup"])
9292
raw_data = self.get_facts_puppetdb(apiurl, factlist, hostgroup)
93-
data = defaultdict(lambda: {})
9493

9594
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-
9995
logging.info("Saving node list in '%s'...", filename)
10096
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')
105101
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')
108105
logging.info('Node list saved successfully')
109106

110107
# trick to avoid Rundeck complain when no output is printed out

rundeck-puppetdb-nodes-plugin/plugin.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#yaml plugin metadata
22

33
name: PuppetDB source
4-
version: 2.1.0
4+
version: 3.0.0
55
rundeckPluginVersion: 1.0
6-
author: Daniel Fernandez, David Moreno Garcia, Philippe Ganz, Nacho Barrientos Arias ([email protected]), Ignacio Coterillo Coz ([email protected])
6+
author: Daniel Fernandez, David Moreno Garcia, Philippe Ganz, Nacho Barrientos Arias ([email protected]), Ignacio Coterillo Coz ([email protected]), Steve Traylen ([email protected])
77
date: 2021/08/04
88
providers:
99
- name: rundeck-puppetdb-nodes

0 commit comments

Comments
 (0)