Created
March 3, 2020 10:00
-
-
Save asquelt/38b58065835134544fff57a877c86ec1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Copyright: (c) 2019, Andrew J. Huffman <ahuffman@redhat.com> | |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
ANSIBLE_METADATA = {'metadata_version': '1.1', | |
'status': ['preview'], | |
'supported_by': 'community'} | |
DOCUMENTATION = ''' | |
--- | |
module: scan_sysctl | |
short_description: Collects currently set sysctl parameters on a system | |
version_added: "2.9" | |
description: | |
- "Collects the currently set sysctl parameters on a system at the time the module is run." | |
- "This module presents the currently set values as ansible_facts" | |
author: | |
- konrad rzentarzewski (@asquelt) | |
''' | |
EXAMPLES = ''' | |
# Collect sysctls and output parsed data | |
- name: "Collect sysctls" | |
scan_sysctl: | |
''' | |
RETURN = ''' | |
ansible_facts.sysctl: | |
''' | |
from ansible.module_utils.basic import AnsibleModule | |
import os, re, subprocess | |
from os.path import isfile, isdir, join | |
def main(): | |
module_args = dict() | |
result = dict( | |
changed=False, | |
original_message='', | |
message='' | |
) | |
module = AnsibleModule( | |
argument_spec=module_args, | |
supports_check_mode=True | |
) | |
params = module.params | |
def get_sysctl(): | |
re_disabled = re.compile(r'=\s*$') | |
bools = list() | |
try: | |
out = subprocess.check_output(["/sbin/sysctl -a"], universal_newlines=True, shell=True) | |
except: | |
out = '' | |
for l in out.split('\n'): | |
if len(l) > 0 and re_disabled.search(l) is None: | |
kv = l.split('=') | |
bools.append({ 'key': kv[0].strip(), 'value': kv[1].strip().replace('\t',' ') }) | |
return bools | |
# Do work | |
data = get_sysctl() | |
# Build output | |
result = {'ansible_facts': {'sysctl': data}} | |
module.exit_json(**result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment