summaryrefslogtreecommitdiffstats
path: root/roles/lib_utils/library/swapoff.py
blob: 925eeb17d087784927437f2e528c60c1ef7b48c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# pylint: disable=missing-docstring
#
# Copyright 2017 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import subprocess

from ansible.module_utils.basic import AnsibleModule


DOCUMENTATION = '''
---
module: swapoff

short_description: Disable swap and comment from /etc/fstab

version_added: "2.4"

description:
    - This module disables swap and comments entries from /etc/fstab

author:
    - "Michael Gugino <mgugino@redhat.com>"
'''

EXAMPLES = '''
# Pass in a message
- name: Disable Swap
  swapoff: {}
'''


def check_swap_in_fstab(module):
    '''Check for uncommented swap entries in fstab'''
    res = subprocess.call(['grep', '^[^#].*swap', '/etc/fstab'])

    if res == 2:
        # rc 2 == cannot open file.
        result = {'failed': True,
                  'changed': False,
                  'msg': 'unable to read /etc/fstab',
                  'state': 'unknown'}
        module.fail_json(**result)
    elif res == 1:
        # No grep match, fstab looks good.
        return False
    elif res == 0:
        # There is an uncommented entry for fstab.
        return True
    else:
        # Some other grep error code, we shouldn't get here.
        result = {'failed': True,
                  'changed': False,
                  'msg': 'unknow problem with grep "^[^#].*swap" /etc/fstab ',
                  'state': 'unknown'}
        module.fail_json(**result)


def check_swapon_status(module):
    '''Check if swap is actually in use.'''
    try:
        res = subprocess.check_output(['swapon', '--show'])
    except subprocess.CalledProcessError:
        # Some other grep error code, we shouldn't get here.
        result = {'failed': True,
                  'changed': False,
                  'msg': 'unable to execute swapon --show',
                  'state': 'unknown'}
        module.fail_json(**result)
    return 'NAME' in str(res)


def comment_swap_fstab(module):
    '''Comment out swap lines in /etc/fstab'''
    res = subprocess.call(['sed', '-i.bak', 's/^[^#].*swap.*/#&/', '/etc/fstab'])
    if res:
        result = {'failed': True,
                  'changed': False,
                  'msg': 'sed failed to comment swap in /etc/fstab',
                  'state': 'unknown'}
        module.fail_json(**result)


def run_swapoff(module, changed):
    '''Run swapoff command'''
    res = subprocess.call(['swapoff', '--all'])
    if res:
        result = {'failed': True,
                  'changed': changed,
                  'msg': 'swapoff --all returned {}'.format(str(res)),
                  'state': 'unknown'}
        module.fail_json(**result)


def run_module():
    '''Run this module'''
    module = AnsibleModule(
        supports_check_mode=False,
        argument_spec={}
    )
    changed = False

    swap_fstab_res = check_swap_in_fstab(module)
    swap_is_inuse_res = check_swapon_status(module)

    if swap_fstab_res:
        comment_swap_fstab(module)
        changed = True

    if swap_is_inuse_res:
        run_swapoff(module, changed)
        changed = True

    result = {'changed': changed}

    module.exit_json(**result)


def main():
    run_module()


if __name__ == '__main__':
    main()