modprobe 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. DOCUMENTATION = '''
  16. ---
  17. module: modprobe
  18. short_description: Add or remove kernel modules
  19. requirements: []
  20. version_added: 1.4
  21. description:
  22. - Add or remove kernel modules.
  23. options:
  24. name:
  25. required: true
  26. description:
  27. - Name of kernel module to manage.
  28. state:
  29. required: false
  30. default: "present"
  31. choices: [ present, absent ]
  32. description:
  33. - Whether the module should be present or absent.
  34. '''
  35. EXAMPLES = '''
  36. # Add the 802.1q module
  37. - modprobe: name=8021q state=present
  38. '''
  39. def main():
  40. module = AnsibleModule(
  41. argument_spec={
  42. 'name': {'required': True},
  43. 'state': {'default': 'present', 'choices': ['present', 'absent']},
  44. },
  45. supports_check_mode=True,
  46. )
  47. args = {
  48. 'changed': False,
  49. 'failed': False,
  50. 'name': module.params['name'],
  51. 'state': module.params['state'],
  52. }
  53. # Check if module is present
  54. try:
  55. modules = open('/proc/modules')
  56. present = False
  57. for line in modules:
  58. if line.startswith(args['name'] + ' '):
  59. present = True
  60. break
  61. modules.close()
  62. except IOError, e:
  63. module.fail_json(msg=str(e), **args)
  64. # Check only; don't modify
  65. if module.check_mode:
  66. if args['state'] == 'present' and not present:
  67. changed = True
  68. elif args['state'] == 'absent' and present:
  69. changed = True
  70. else:
  71. changed = False
  72. module.exit_json(changed=changed)
  73. # Add/remove module as needed
  74. if args['state'] == 'present':
  75. if not present:
  76. rc, _, err = module.run_command(['modprobe', args['name']])
  77. if rc != 0:
  78. module.fail_json(msg=err, **args)
  79. args['changed'] = True
  80. elif args['state'] == 'absent':
  81. if present:
  82. rc, _, err = module.run_command(['rmmod', args['name']])
  83. if rc != 0:
  84. module.fail_json(msg=err, **args)
  85. args['changed'] = True
  86. module.exit_json(**args)
  87. # this is magic, see lib/ansible/module_common.py
  88. #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
  89. main()