added control to fail or not on missing key

made split 'smarter' but still overridable
This commit is contained in:
Brian Coca 2014-03-23 10:03:17 -04:00 committed by Michael DeHaan
parent 391f8ec600
commit 5aa2ca762a

View file

@ -25,23 +25,33 @@ DOCUMENTATION = '''
module: getent module: getent
short_description: a wrapper to the unix getent utility short_description: a wrapper to the unix getent utility
description: description:
- Runs getent against one of it's various databases and returns information into the host's facts - Runs getent against one of it's various databases and returns information into
the host's facts
version_added: "1.6" version_added: "1.6"
options: options:
database: database:
required: True required: True
description: description:
- the name of a getent database supported by the target system (passwd, group, hosts, etc). - the name of a getent database supported by the target system (passwd, group,
hosts, etc).
key: key:
required: False required: False
default: '' default: ''
description: description:
- key from which to return values from the specified database, otherwise the full contents are returned. - key from which to return values from the specified database, otherwise the
full contents are returned.
split: split:
required: False required: False
default: None default: None
description: description:
- character used to override the split the database values into lists/arrays, i.e ':' or '\t'. - character used to override the split the database values into lists/arrays,
i.e ':' or '\t'. Otherwise split tries to be 'smart' depending on DB.
fail_key:
required: False
default: True
description:
- If a supplied key is missing this will make the task fail if True
notes: [ Not all databases support enumeration, check system documentation for details ] notes: [ Not all databases support enumeration, check system documentation for details ]
requirements: [ ] requirements: [ ]
author: Brian Coca author: Brian Coca
@ -49,7 +59,7 @@ author: Brian Coca
EXAMPLES = ''' EXAMPLES = '''
# get root user info # get root user info
- getent: database=passwd key=root split=':' - getent: database=passwd key=root
# get all groups # get all groups
- getent: database=group split=':' - getent: database=group split=':'
@ -57,8 +67,8 @@ EXAMPLES = '''
# get all hosts, split by tab # get all hosts, split by tab
- getent: database=hosts - getent: database=hosts
# get http service info # get http service info, no error if missing
- getent: database=services key=http - getent: database=services key=http fail_key=False
# get user password hash (requires sudo/root) # get user password hash (requires sudo/root)
- getent: database=shadow key=www-data split=: - getent: database=shadow key=www-data split=:
@ -71,13 +81,17 @@ def main():
database = dict(required=True), database = dict(required=True),
key = dict(required=False, default=None), key = dict(required=False, default=None),
split = dict(required=False, default=None), split = dict(required=False, default=None),
fail_key = dict(required=False, default=True),
), ),
supports_check_mode = True, supports_check_mode = True,
) )
colon = [ 'passwd', 'shadow', 'group', 'gshadow' ]
database = module.params['database'] database = module.params['database']
key = module.params.get('key') key = module.params.get('key')
split = module.params.get('split') split = module.params.get('split')
fail_key = module.params.get('fail_key')
getent_bin = module.get_bin_path('getent', True) getent_bin = module.get_bin_path('getent', True)
@ -86,6 +100,9 @@ def main():
else: else:
cmd = [ getent_bin, database ] cmd = [ getent_bin, database ]
if split is None and database in colon:
split = ':'
try: try:
rc, out, err = module.run_command(cmd) rc, out, err = module.run_command(cmd)
except Exception, e: except Exception, e:
@ -105,8 +122,10 @@ def main():
elif rc == 1: elif rc == 1:
msg = "Missing arguments, or database unknown." msg = "Missing arguments, or database unknown."
elif rc == 2: elif rc == 2:
msg = "One or more supplied key could not be found in the database."
if not fail_key:
results[dbtree][key] = None results[dbtree][key] = None
module.exit_json(ansible_facts=results, msg="One or more supplied key could not be found in the database.") module.exit_json(ansible_facts=results, msg=msg)
elif rc == 3: elif rc == 3:
msg = "Enumeration not supported on this database." msg = "Enumeration not supported on this database."