|
| 1 | +# Ansible Role: Firewall (iptables) |
| 2 | + |
| 3 | +[](https://travis-ci.org/geerlingguy/ansible-role-firewall) |
| 4 | + |
| 5 | +Installs an iptables-based firewall for Linux. Supports both IPv4 (`iptables`) and IPv6 (`ip6tables`). |
| 6 | + |
| 7 | +This firewall aims for simplicity over complexity, and only opens a few specific ports for incoming traffic (configurable through Ansible variables). If you have a rudimentary knowledge of `iptables` and/or firewalls in general, this role should be a good starting point for a secure system firewall. |
| 8 | + |
| 9 | +After the role is run, a `firewall` init service will be available on the server. You can use `service firewall [start|stop|restart|status]` to control the firewall. |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +None. |
| 14 | + |
| 15 | +## Role Variables |
| 16 | + |
| 17 | +Available variables are listed below, along with default values (see `defaults/main.yml`): |
| 18 | + |
| 19 | + firewall_state: started |
| 20 | + firewall_enabled_at_boot: true |
| 21 | + |
| 22 | +Controls the state of the firewall service; whether it should be running (`firewall_state`) and/or enabled on system boot (`firewall_enabled_at_boot`). |
| 23 | + |
| 24 | + firewall_allowed_tcp_ports: |
| 25 | + - "22" |
| 26 | + - "80" |
| 27 | + ... |
| 28 | + firewall_allowed_udp_ports: [] |
| 29 | + |
| 30 | +A list of TCP or UDP ports (respectively) to open to incoming traffic. |
| 31 | + |
| 32 | + firewall_forwarded_tcp_ports: |
| 33 | + - { src: "22", dest: "2222" } |
| 34 | + - { src: "80", dest: "8080" } |
| 35 | + firewall_forwarded_udp_ports: [] |
| 36 | + |
| 37 | +Forward `src` port to `dest` port, either TCP or UDP (respectively). |
| 38 | + |
| 39 | + firewall_additional_rules: [] |
| 40 | + firewall_ip6_additional_rules: [] |
| 41 | + |
| 42 | +Any additional (custom) rules to be added to the firewall (in the same format you would add them via command line, e.g. `iptables [rule]`/`ip6tables [rule]`). A few examples of how this could be used: |
| 43 | + |
| 44 | + # Allow only the IP 167.89.89.18 to access port 4949 (Munin). |
| 45 | + firewall_additional_rules: |
| 46 | + - "iptables -A INPUT -p tcp --dport 4949 -s 167.89.89.18 -j ACCEPT" |
| 47 | + |
| 48 | + # Allow only the IP 214.192.48.21 to access port 3306 (MySQL). |
| 49 | + firewall_additional_rules: |
| 50 | + - "iptables -A INPUT -p tcp --dport 3306 -s 214.192.48.21 -j ACCEPT" |
| 51 | + |
| 52 | +See [Iptables Essentials: Common Firewall Rules and Commands](https://www.digitalocean.com/community/tutorials/iptables-essentials-common-firewall-rules-and-commands) for more examples. |
| 53 | + |
| 54 | + firewall_log_dropped_packets: true |
| 55 | + |
| 56 | +Whether to log dropped packets to syslog (messages will be prefixed with "Dropped by firewall: "). |
| 57 | + |
| 58 | + firewall_disable_firewalld: false |
| 59 | + firewall_disable_ufw: false |
| 60 | + |
| 61 | +Set to `true` to disable firewalld (installed by default on RHEL/CentOS) or ufw (installed by default on Ubuntu), respectively. |
| 62 | + |
| 63 | +## Dependencies |
| 64 | + |
| 65 | +None. |
| 66 | + |
| 67 | +## Example Playbook |
| 68 | + |
| 69 | + - hosts: server |
| 70 | + vars_files: |
| 71 | + - vars/main.yml |
| 72 | + roles: |
| 73 | + - { role: geerlingguy.firewall } |
| 74 | + |
| 75 | +*Inside `vars/main.yml`*: |
| 76 | + |
| 77 | + firewall_allowed_tcp_ports: |
| 78 | + - "22" |
| 79 | + - "25" |
| 80 | + - "80" |
| 81 | + |
| 82 | +## TODO |
| 83 | + |
| 84 | + - Make outgoing ports more configurable. |
| 85 | + - Make other firewall features (like logging) configurable. |
| 86 | + |
| 87 | +## License |
| 88 | + |
| 89 | +MIT / BSD |
| 90 | + |
| 91 | +## Author Information |
| 92 | + |
| 93 | +This role was created in 2014 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). |
0 commit comments