| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Ted Trask <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: awall |
| 11 |
short_description: Manage awall policies |
| 12 |
author: Ted Trask (@tdtrask) <[email protected]> |
| 13 |
description: |
| 14 |
- This modules allows for enable/disable/activate of C(awall) policies. |
| 15 |
- Alpine Wall (C(awall)) generates a firewall configuration from the enabled policy files and activates the configuration |
| 16 |
on the system. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- One or more policy names. |
| 28 |
type: list |
| 29 |
elements: str |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Whether the policies should be enabled or disabled. |
| 33 |
type: str |
| 34 |
choices: [disabled, enabled] |
| 35 |
default: enabled |
| 36 |
activate: |
| 37 |
description: |
| 38 |
- Activate the new firewall rules. |
| 39 |
- Can be run with other steps or on its own. |
| 40 |
- Idempotency is affected if O(activate=true), as the module always reports a changed state. |
| 41 |
type: bool |
| 42 |
default: false |
| 43 |
notes: |
| 44 |
- At least one of O(name) and O(activate) is required. |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = r""" |
| 48 |
- name: Enable "foo" and "bar" policy |
| 49 |
community.general.awall: |
| 50 |
name: [foo bar] |
| 51 |
state: enabled |
| 52 |
|
| 53 |
- name: Disable "foo" and "bar" policy and activate new rules |
| 54 |
community.general.awall: |
| 55 |
name: |
| 56 |
- foo |
| 57 |
- bar |
| 58 |
state: disabled |
| 59 |
activate: false |
| 60 |
|
| 61 |
- name: Activate currently enabled firewall rules |
| 62 |
community.general.awall: |
| 63 |
activate: true |
| 64 |
""" |
| 65 |
|
| 66 |
RETURN = """ # """ |
| 67 |
|
| 68 |
import re |
| 69 |
|
| 70 |
from ansible.module_utils.basic import AnsibleModule |
| 71 |
|
| 72 |
|
| 73 |
def activate(module): |
| 74 |
cmd = f"{AWALL_PATH} activate --force" |
| 75 |
rc, stdout, stderr = module.run_command(cmd) |
| 76 |
if rc == 0: |
| 77 |
return True |
| 78 |
else: |
| 79 |
module.fail_json(msg="could not activate new rules", stdout=stdout, stderr=stderr) |
| 80 |
|
| 81 |
|
| 82 |
def is_policy_enabled(module, name): |
| 83 |
cmd = f"{AWALL_PATH} list" |
| 84 |
rc, stdout, stderr = module.run_command(cmd) |
| 85 |
return bool(re.search(rf"^{name}\s+enabled", stdout, re.MULTILINE)) |
| 86 |
|
| 87 |
|
| 88 |
def enable_policy(module, names, act): |
| 89 |
policies = [] |
| 90 |
for name in names: |
| 91 |
if not is_policy_enabled(module, name): |
| 92 |
policies.append(name) |
| 93 |
if not policies: |
| 94 |
module.exit_json(changed=False, msg="policy(ies) already enabled") |
| 95 |
names = " ".join(policies) |
| 96 |
if module.check_mode: |
| 97 |
cmd = f"{AWALL_PATH} list" |
| 98 |
else: |
| 99 |
cmd = f"{AWALL_PATH} enable {names}" |
| 100 |
rc, stdout, stderr = module.run_command(cmd) |
| 101 |
if rc != 0: |
| 102 |
module.fail_json(msg=f"failed to enable {names}", stdout=stdout, stderr=stderr) |
| 103 |
if act and not module.check_mode: |
| 104 |
activate(module) |
| 105 |
module.exit_json(changed=True, msg=f"enabled awall policy(ies): {names}") |
| 106 |
|
| 107 |
|
| 108 |
def disable_policy(module, names, act): |
| 109 |
policies = [] |
| 110 |
for name in names: |
| 111 |
if is_policy_enabled(module, name): |
| 112 |
policies.append(name) |
| 113 |
if not policies: |
| 114 |
module.exit_json(changed=False, msg="policy(ies) already disabled") |
| 115 |
names = " ".join(policies) |
| 116 |
if module.check_mode: |
| 117 |
cmd = f"{AWALL_PATH} list" |
| 118 |
else: |
| 119 |
cmd = f"{AWALL_PATH} disable {names}" |
| 120 |
rc, stdout, stderr = module.run_command(cmd) |
| 121 |
if rc != 0: |
| 122 |
module.fail_json(msg=f"failed to disable {names}", stdout=stdout, stderr=stderr) |
| 123 |
if act and not module.check_mode: |
| 124 |
activate(module) |
| 125 |
module.exit_json(changed=True, msg=f"disabled awall policy(ies): {names}") |
| 126 |
|
| 127 |
|
| 128 |
def main(): |
| 129 |
module = AnsibleModule( |
| 130 |
argument_spec=dict( |
| 131 |
state=dict(type="str", default="enabled", choices=["disabled", "enabled"]), |
| 132 |
name=dict(type="list", elements="str"), |
| 133 |
activate=dict(type="bool", default=False), |
| 134 |
), |
| 135 |
required_one_of=[["name", "activate"]], |
| 136 |
supports_check_mode=True, |
| 137 |
) |
| 138 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 139 |
|
| 140 |
global AWALL_PATH |
| 141 |
AWALL_PATH = module.get_bin_path("awall", required=True) |
| 142 |
|
| 143 |
p = module.params |
| 144 |
|
| 145 |
if p["name"]: |
| 146 |
if p["state"] == "enabled": |
| 147 |
enable_policy(module, p["name"], p["activate"]) |
| 148 |
elif p["state"] == "disabled": |
| 149 |
disable_policy(module, p["name"], p["activate"]) |
| 150 |
|
| 151 |
if p["activate"]: |
| 152 |
if not module.check_mode: |
| 153 |
activate(module) |
| 154 |
module.exit_json(changed=True, msg="activated awall rules") |
| 155 |
|
| 156 |
module.fail_json(msg="no action defined") |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == "__main__": |
| 160 |
main() |