|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# (c) 2015, Dean Wilson <dean.wilson@gmail.com> |
| 5 | +# |
| 6 | +# Ansible is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Ansible is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | + |
| 20 | +import glob |
| 21 | +import re |
| 22 | +import os |
| 23 | + |
| 24 | +DOCUMENTATION = ''' |
| 25 | +--- |
| 26 | +module: yum_plugins |
| 27 | +short_description: Retrieve yum plugin details |
| 28 | +description: |
| 29 | + - Retrieve yum plugin details |
| 30 | +author: Dean Wilson |
| 31 | +options: |
| 32 | + config_dir: |
| 33 | + description: |
| 34 | + - The plugin config directory. Defaults to /etc/yum/pluginconf.d |
| 35 | + required: false |
| 36 | + default: '/etc/yum/pluginconf.d' |
| 37 | + aliases: [] |
| 38 | +''' |
| 39 | + |
| 40 | +EXAMPLES = ''' |
| 41 | + - name: Retrieve yum plugin details |
| 42 | + yum_plugins: |
| 43 | +
|
| 44 | + - debug: var=yum_plugins |
| 45 | +''' |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + module = AnsibleModule( |
| 50 | + argument_spec=dict( |
| 51 | + config_dir=dict(default='/etc/yum/pluginconf.d', required=False) |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + config_dir = module.params['config_dir'] |
| 56 | + regexp = re.compile(r'enabled\s*=\s*1') |
| 57 | + |
| 58 | + plugins = { |
| 59 | + 'plugin': [], |
| 60 | + 'enabled': [], |
| 61 | + 'disabled': [] |
| 62 | + } |
| 63 | + |
| 64 | + files = glob.glob(config_dir + '/*.conf') |
| 65 | + |
| 66 | + for file in files: |
| 67 | + filename = os.path.splitext(os.path.basename(file))[0] |
| 68 | + |
| 69 | + plugins['plugin'].append(filename) |
| 70 | + |
| 71 | + with open(file, 'r') as f: |
| 72 | + content = f.read() |
| 73 | + if regexp.search(content) is not None: |
| 74 | + plugins['enabled'].append(filename) |
| 75 | + else: |
| 76 | + plugins['disabled'].append(filename) |
| 77 | + |
| 78 | + # sort the values |
| 79 | + for key in plugins: |
| 80 | + plugins[key].sort() |
| 81 | + |
| 82 | + results = { |
| 83 | + 'yum_plugins': plugins |
| 84 | + } |
| 85 | + |
| 86 | + module.exit_json(ansible_facts=results) |
| 87 | + |
| 88 | +from ansible.module_utils.basic import * |
| 89 | +main() |
0 commit comments