From f8c70cb1f2bfe7a2e0e3686742a8655db4a0cede Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Wed, 25 Nov 2015 08:31:03 -0500 Subject: [PATCH] Don't post facts if there are none Also, post non-facter facts - we have a hybrid system, why not report all of our facts to puppetdb. Change-Id: I246c5fd31739921d072edf8614c748dfaa611927 --- library/puppet_post_puppetdb | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/library/puppet_post_puppetdb b/library/puppet_post_puppetdb index 9b79cb1..5373e0f 100644 --- a/library/puppet_post_puppetdb +++ b/library/puppet_post_puppetdb @@ -92,21 +92,29 @@ def main(): for k, v in p['hostvars'].items(): if k.startswith('facter_'): facts[k[7:]] = v - payload = { - "command": "replace facts", - "version": 3, - "payload": { - "name": fqdn, - "environment": p['environment'], - "producer-timestamp": timestamp, - "values": facts }} + for k, v in p['hostvars'].items(): + if not k.startswith('facter_'): + # Go ahead and set the non-facter facts that ansible has gathered + # too - but let facter facts with the same name win + facts.setdefault(k, v) - r = requests.post(endpoint, json=payload, **requests_kwargs) - if r.status_code != 200: - module.fail_json( - rc=r.status_code, - msg="Failed to post facts to {puppetdb}".format( - puppetdb=p['puppetdb'])) + if facts: + # Don't post facts update if we don't have facts + payload = { + "command": "replace facts", + "version": 3, + "payload": { + "name": fqdn, + "environment": p['environment'], + "producer-timestamp": timestamp, + "values": facts }} + + r = requests.post(endpoint, json=payload, **requests_kwargs) + if r.status_code != 200: + module.fail_json( + rc=r.status_code, + msg="Failed to post facts to {puppetdb}".format( + puppetdb=p['puppetdb'])) log_data = json.load(open(p['logfile'], 'r')) r = requests.post(endpoint, json=log_data, **requests_kwargs)