ansible-hardening/library/get_users

85 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2016, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Get user facts."""
import pwd
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = """
---
module: get_users
short_description:
- A module for gathering facts about Linux users.
description:
- This module gathers facts about the Linux users and groups that exist
on the system.
author: major@mhtx.net
"""
EXAMPLES = '''
- get_users:
min_uid: 1000
max_uid: 2000
'''
RETURN = '''
users:
description: users matching arguments provided
returned: success
type: list
'''
def main():
"""Ansible calls this function."""
module = AnsibleModule(
argument_spec=dict(
min_uid=dict(default=0, type='int'),
max_uid=dict(default=65535, type='int'),
),
supports_check_mode=True,
)
users = []
# Loop through the users that exist on the system.
for user_record in pwd.getpwall():
# Ensure that the user matches the parameters provided.
if (user_record.pw_uid >= module.params['min_uid'] and
user_record.pw_uid <= module.params['max_uid']):
# Assemble a dictionary of the user information and append it to
# our list.
user_dict = {
'name': user_record.pw_name,
'uid': user_record.pw_uid,
'gid': user_record.pw_gid,
'gecos': user_record.pw_gecos,
'dir': user_record.pw_dir,
'shell': user_record.pw_shell
}
users.append(user_dict)
# Return the user data to the Ansible task.
module.exit_json(
changed=False,
users=users
)
if __name__ == '__main__':
main()