2014-03-22 02:10:47 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2014, Brian Coca <brian.coca+dev@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: getent
|
|
|
|
short_description: a wrapper to the unix getent utility
|
|
|
|
description:
|
2014-03-23 14:03:17 +00:00
|
|
|
- Runs getent against one of it's various databases and returns information into
|
|
|
|
the host's facts
|
2014-08-08 15:49:41 +00:00
|
|
|
version_added: "1.8"
|
2014-03-22 02:10:47 +00:00
|
|
|
options:
|
|
|
|
database:
|
|
|
|
required: True
|
|
|
|
description:
|
2014-03-23 14:03:17 +00:00
|
|
|
- the name of a getent database supported by the target system (passwd, group,
|
|
|
|
hosts, etc).
|
2014-03-22 02:10:47 +00:00
|
|
|
key:
|
|
|
|
required: False
|
|
|
|
default: ''
|
|
|
|
description:
|
2014-03-23 14:03:17 +00:00
|
|
|
- key from which to return values from the specified database, otherwise the
|
|
|
|
full contents are returned.
|
2014-03-22 02:10:47 +00:00
|
|
|
split:
|
|
|
|
required: False
|
|
|
|
default: None
|
|
|
|
description:
|
2014-03-23 14:03:17 +00:00
|
|
|
- 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
|
|
|
|
|
2014-08-08 15:49:41 +00:00
|
|
|
notes:
|
|
|
|
- "Not all databases support enumeration, check system documentation for details"
|
2014-03-22 02:10:47 +00:00
|
|
|
requirements: [ ]
|
|
|
|
author: Brian Coca
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# get root user info
|
2014-03-23 14:03:17 +00:00
|
|
|
- getent: database=passwd key=root
|
2014-03-22 02:10:47 +00:00
|
|
|
|
|
|
|
# get all groups
|
|
|
|
- getent: database=group split=':'
|
|
|
|
|
|
|
|
# get all hosts, split by tab
|
|
|
|
- getent: database=hosts
|
|
|
|
|
2014-03-23 14:03:17 +00:00
|
|
|
# get http service info, no error if missing
|
|
|
|
- getent: database=services key=http fail_key=False
|
2014-03-22 02:10:47 +00:00
|
|
|
|
|
|
|
# get user password hash (requires sudo/root)
|
|
|
|
- getent: database=shadow key=www-data split=:
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
database = dict(required=True),
|
|
|
|
key = dict(required=False, default=None),
|
|
|
|
split = dict(required=False, default=None),
|
2014-03-23 14:03:17 +00:00
|
|
|
fail_key = dict(required=False, default=True),
|
2014-03-22 02:10:47 +00:00
|
|
|
),
|
|
|
|
supports_check_mode = True,
|
|
|
|
)
|
|
|
|
|
2014-03-23 14:03:17 +00:00
|
|
|
colon = [ 'passwd', 'shadow', 'group', 'gshadow' ]
|
|
|
|
|
2014-03-22 02:10:47 +00:00
|
|
|
database = module.params['database']
|
|
|
|
key = module.params.get('key')
|
|
|
|
split = module.params.get('split')
|
2014-03-23 14:03:17 +00:00
|
|
|
fail_key = module.params.get('fail_key')
|
2014-03-22 02:10:47 +00:00
|
|
|
|
|
|
|
getent_bin = module.get_bin_path('getent', True)
|
|
|
|
|
|
|
|
if key is not None:
|
|
|
|
cmd = [ getent_bin, database, key ]
|
|
|
|
else:
|
|
|
|
cmd = [ getent_bin, database ]
|
|
|
|
|
2014-03-23 14:03:17 +00:00
|
|
|
if split is None and database in colon:
|
|
|
|
split = ':'
|
|
|
|
|
2014-03-22 02:10:47 +00:00
|
|
|
try:
|
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
msg = "Unexpected failure!"
|
|
|
|
dbtree = 'getent_%s' % database
|
|
|
|
results = { dbtree: {} }
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
for line in out.splitlines():
|
|
|
|
record = line.split(split)
|
|
|
|
results[dbtree][record[0]] = record[1:]
|
|
|
|
|
|
|
|
module.exit_json(ansible_facts=results)
|
|
|
|
|
|
|
|
elif rc == 1:
|
|
|
|
msg = "Missing arguments, or database unknown."
|
|
|
|
elif rc == 2:
|
2014-03-23 14:03:17 +00:00
|
|
|
msg = "One or more supplied key could not be found in the database."
|
|
|
|
if not fail_key:
|
|
|
|
results[dbtree][key] = None
|
|
|
|
module.exit_json(ansible_facts=results, msg=msg)
|
2014-03-22 02:10:47 +00:00
|
|
|
elif rc == 3:
|
|
|
|
msg = "Enumeration not supported on this database."
|
|
|
|
|
|
|
|
module.fail_json(msg=msg)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
main()
|
|
|
|
|