swapoff.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python
  2. # pylint: disable=missing-docstring
  3. #
  4. # Copyright 2017 Red Hat, Inc. and/or its affiliates
  5. # and other contributors as indicated by the @author tags.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. import subprocess
  19. from ansible.module_utils.basic import AnsibleModule
  20. DOCUMENTATION = '''
  21. ---
  22. module: swapoff
  23. short_description: Disable swap and comment from /etc/fstab
  24. version_added: "2.4"
  25. description:
  26. - This module disables swap and comments entries from /etc/fstab
  27. author:
  28. - "Michael Gugino <mgugino@redhat.com>"
  29. '''
  30. EXAMPLES = '''
  31. # Pass in a message
  32. - name: Disable Swap
  33. swapoff: {}
  34. '''
  35. def check_swap_in_fstab(module):
  36. '''Check for uncommented swap entries in fstab'''
  37. res = subprocess.call(['grep', '^[^#].*swap', '/etc/fstab'])
  38. if res == 2:
  39. # rc 2 == cannot open file.
  40. result = {'failed': True,
  41. 'changed': False,
  42. 'msg': 'unable to read /etc/fstab',
  43. 'state': 'unknown'}
  44. module.fail_json(**result)
  45. elif res == 1:
  46. # No grep match, fstab looks good.
  47. check_result = False
  48. elif res == 0:
  49. # There is an uncommented entry for fstab.
  50. check_result = True
  51. else:
  52. # Some other grep error code, we shouldn't get here.
  53. result = {'failed': True,
  54. 'changed': False,
  55. 'msg': 'unknown problem with grep "^[^#].*swap" /etc/fstab ',
  56. 'state': 'unknown'}
  57. module.fail_json(**result)
  58. return check_result
  59. def check_swapon_status(module):
  60. '''Check if swap is actually in use.'''
  61. try:
  62. res = subprocess.check_output(['swapon', '--show'])
  63. except subprocess.CalledProcessError:
  64. # Some other grep error code, we shouldn't get here.
  65. result = {'failed': True,
  66. 'changed': False,
  67. 'msg': 'unable to execute swapon --show',
  68. 'state': 'unknown'}
  69. module.fail_json(**result)
  70. return 'NAME' in str(res)
  71. def comment_swap_fstab(module):
  72. '''Comment out swap lines in /etc/fstab'''
  73. res = subprocess.call(['sed', '-i.bak', 's/^[^#].*swap.*/#&/', '/etc/fstab'])
  74. if res:
  75. result = {'failed': True,
  76. 'changed': False,
  77. 'msg': 'sed failed to comment swap in /etc/fstab',
  78. 'state': 'unknown'}
  79. module.fail_json(**result)
  80. def run_swapoff(module, changed):
  81. '''Run swapoff command'''
  82. res = subprocess.call(['swapoff', '--all'])
  83. if res:
  84. result = {'failed': True,
  85. 'changed': changed,
  86. 'msg': 'swapoff --all returned {}'.format(str(res)),
  87. 'state': 'unknown'}
  88. module.fail_json(**result)
  89. def run_module():
  90. '''Run this module'''
  91. module = AnsibleModule(
  92. supports_check_mode=False,
  93. argument_spec={}
  94. )
  95. changed = False
  96. swap_fstab_res = check_swap_in_fstab(module)
  97. swap_is_inuse_res = check_swapon_status(module)
  98. if swap_fstab_res:
  99. comment_swap_fstab(module)
  100. changed = True
  101. if swap_is_inuse_res:
  102. run_swapoff(module, changed)
  103. changed = True
  104. result = {'changed': changed}
  105. module.exit_json(**result)
  106. def main():
  107. run_module()
  108. if __name__ == '__main__':
  109. main()