blacklist 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python2
  2. #coding: utf-8 -*-
  3. # This module is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This software is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this software. If not, see <http://www.gnu.org/licenses/>.
  15. import os
  16. import re
  17. DOCUMENTATION = '''
  18. ---
  19. module: blacklist
  20. short_description: Blacklist kernel modules
  21. requirements: []
  22. version_added: 1.4
  23. description:
  24. - Add or remove kernel modules from blacklist.
  25. options:
  26. name:
  27. required: true
  28. description:
  29. - Name of kernel module to black- or whitelist.
  30. state:
  31. required: false
  32. default: "present"
  33. choices: [ present, absent ]
  34. description:
  35. - Whether the module should be present in the blacklist or absent.
  36. blacklist_file:
  37. required: false
  38. description:
  39. - If specified, use this blacklist file instead of
  40. C(/etc/modprobe.d/blacklist-ansible.conf).
  41. default: null
  42. '''
  43. EXAMPLES = '''
  44. # Blacklist the nouveau driver module
  45. - blacklist: name=nouveau state=present
  46. '''
  47. class Blacklist(object):
  48. def __init__(self, module, filename):
  49. if not os.path.exists(filename):
  50. open(filename, 'a').close()
  51. self.filename = filename
  52. self.module = module
  53. def get_pattern(self):
  54. return '^blacklist\s*' + self.module + '$'
  55. def readlines(self):
  56. f = open(self.filename, 'r')
  57. lines = f.readlines()
  58. f.close()
  59. return lines
  60. def module_listed(self):
  61. lines = self.readlines()
  62. pattern = self.get_pattern()
  63. for line in lines:
  64. stripped = line.strip()
  65. if stripped.startswith('#'):
  66. continue
  67. if re.match(pattern, stripped):
  68. return True
  69. return False
  70. def remove_module(self):
  71. lines = self.readlines()
  72. pattern = self.get_pattern()
  73. f = open(self.filename, 'w')
  74. for line in lines:
  75. if not re.match(pattern, line.strip()):
  76. f.write(line)
  77. f.close()
  78. def add_module(self):
  79. f = open(self.filename, 'a')
  80. f.write('blacklist %s\n' % self.module)
  81. def main():
  82. module = AnsibleModule(
  83. argument_spec=dict(
  84. name=dict(required=True),
  85. state=dict(required=False, choices=['present', 'absent'],
  86. default='present'),
  87. blacklist_file=dict(required=False, default=None)
  88. ),
  89. supports_check_mode=False,
  90. )
  91. args = dict(changed=False, failed=False,
  92. name=module.params['name'], state=module.params['state'])
  93. filename = '/etc/modprobe.d/blacklist-ansible.conf'
  94. if module.params['blacklist_file']:
  95. filename = module.params['blacklist_file']
  96. blacklist = Blacklist(args['name'], filename)
  97. if blacklist.module_listed():
  98. if args['state'] == 'absent':
  99. blacklist.remove_module()
  100. args['changed'] = True
  101. else:
  102. if args['state'] == 'present':
  103. blacklist.add_module()
  104. args['changed'] = True
  105. module.exit_json(**args)
  106. # this is magic, see lib/ansible/module_common.py
  107. #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
  108. main()