made sensu_check 2.4 friendly
This commit is contained in:
parent
8f31d84300
commit
1659af1541
1 changed files with 10 additions and 4 deletions
|
@ -183,8 +183,8 @@ def sensu_check(module, path, name, state='present', backup=False):
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(path) as stream:
|
stream = open(path, 'r')
|
||||||
config = json.load(stream)
|
config = json.load(stream.read())
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
if e.errno is 2: # File not found, non-fatal
|
if e.errno is 2: # File not found, non-fatal
|
||||||
if state == 'absent':
|
if state == 'absent':
|
||||||
|
@ -196,6 +196,9 @@ def sensu_check(module, path, name, state='present', backup=False):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
msg = '{path} contains invalid JSON'.format(path=path)
|
msg = '{path} contains invalid JSON'.format(path=path)
|
||||||
module.fail_json(msg=msg)
|
module.fail_json(msg=msg)
|
||||||
|
finally:
|
||||||
|
if stream:
|
||||||
|
stream.close()
|
||||||
|
|
||||||
if 'checks' not in config:
|
if 'checks' not in config:
|
||||||
if state == 'absent':
|
if state == 'absent':
|
||||||
|
@ -274,10 +277,13 @@ def sensu_check(module, path, name, state='present', backup=False):
|
||||||
if backup:
|
if backup:
|
||||||
module.backup_local(path)
|
module.backup_local(path)
|
||||||
try:
|
try:
|
||||||
with open(path, 'w') as stream:
|
stream = open(path, 'w')
|
||||||
stream.write(json.dumps(config, indent=2) + '\n')
|
stream.write(json.dumps(config, indent=2) + '\n')
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
finally:
|
||||||
|
if stream:
|
||||||
|
stream.close()
|
||||||
|
|
||||||
return changed, reasons
|
return changed, reasons
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue