2017-08-07 12:09:13 +02:00
|
|
|
---
|
2019-04-16 13:48:48 +02:00
|
|
|
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
|
|
|
|
#
|
2017-08-07 12:09:13 +02:00
|
|
|
# tasks file for nftables
|
2020-11-11 15:27:08 +01:00
|
|
|
|
2020-12-01 16:17:01 +01:00
|
|
|
- name: Check for group files
|
|
|
|
become: no
|
|
|
|
delegate_to: localhost
|
|
|
|
stat:
|
2020-12-30 17:12:50 +01:00
|
|
|
path: "{{ nft_merged_groups_dir ~ groupname }}"
|
|
|
|
register: nftables_group_rules
|
2020-11-10 21:17:11 +01:00
|
|
|
loop: "{{ group_names }}"
|
|
|
|
loop_control:
|
|
|
|
loop_var: groupname
|
|
|
|
|
2021-08-07 13:18:09 +02:00
|
|
|
- name: Debug nftables_group_rules
|
|
|
|
debug: var=nftables_group_rules
|
2020-12-30 17:12:50 +01:00
|
|
|
when: nft_debug
|
2020-12-01 16:17:01 +01:00
|
|
|
|
2020-12-30 17:12:50 +01:00
|
|
|
- name: Import nftables-variables if nft_merged_groups is set
|
|
|
|
when: nft_merged_groups and varfile.stat.exists
|
2020-12-01 16:17:01 +01:00
|
|
|
include_vars:
|
2020-12-30 17:12:50 +01:00
|
|
|
file: "{{ nft_merged_groups_dir ~ varfile.groupname }}"
|
2020-12-01 16:17:01 +01:00
|
|
|
name: "{{ varfile.groupname }}"
|
2020-12-30 17:12:50 +01:00
|
|
|
loop: "{{ nftables_group_rules.results }}"
|
2020-12-01 16:17:01 +01:00
|
|
|
loop_control:
|
|
|
|
loop_var: varfile
|
|
|
|
|
2020-12-30 17:12:50 +01:00
|
|
|
- name: Combine Rules when nft_merged_groups is set
|
2021-01-05 15:58:43 +01:00
|
|
|
when: nft_merged_groups and
|
|
|
|
((hostvars[inventory_hostname][varfile.groupname] is defined) and
|
|
|
|
(hostvars[inventory_hostname][varfile.groupname]|length > 0)) and
|
|
|
|
varfile.stat.exists
|
2020-11-10 21:17:11 +01:00
|
|
|
set_fact:
|
2020-12-01 16:17:01 +01:00
|
|
|
nft_combined_rules: "{{ nft_combined_rules | default({}) | combine ( hostvars[inventory_hostname][varfile.groupname], recursive=True ) }}"
|
2020-12-30 17:12:50 +01:00
|
|
|
loop: "{{ nftables_group_rules.results }}"
|
2020-11-10 21:17:11 +01:00
|
|
|
loop_control:
|
2020-12-01 16:17:01 +01:00
|
|
|
loop_var: varfile
|
2017-08-07 12:09:13 +02:00
|
|
|
|
2021-08-07 13:18:09 +02:00
|
|
|
- name: Debug nft_combined_rules
|
|
|
|
debug: var=nft_combined_rules
|
2020-12-30 17:12:50 +01:00
|
|
|
when: nft_debug
|
|
|
|
|
2021-08-07 14:32:43 +02:00
|
|
|
- name: Debug ansible_os_family
|
|
|
|
debug: var=ansible_os_family
|
|
|
|
when: nft_debug
|
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
- name: Load specific OS vars for nftables
|
2020-11-10 21:17:11 +01:00
|
|
|
include_vars: "{{ osname }}"
|
2017-08-07 12:09:13 +02:00
|
|
|
with_first_found:
|
|
|
|
- "{{ ansible_distribution|lower }}-{{ ansible_distribution_version }}.yml"
|
|
|
|
- "{{ ansible_distribution|lower }}.yml"
|
|
|
|
- "{{ ansible_os_family|lower }}.yml"
|
2020-11-10 21:17:11 +01:00
|
|
|
loop_control:
|
|
|
|
loop_var: osname
|
2017-08-07 12:09:13 +02:00
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
# Manage packages [[[1
|
2020-11-10 21:17:11 +01:00
|
|
|
- name: Ensure Nftables packages are in their desired state
|
2017-08-07 12:09:13 +02:00
|
|
|
package:
|
2019-05-07 00:00:48 +02:00
|
|
|
name: '{{ nft_pkg_list | list }}'
|
2017-08-07 12:09:13 +02:00
|
|
|
state: '{{ nft_pkg_state }}'
|
2021-08-07 12:47:32 +02:00
|
|
|
update_cache: true
|
2019-02-27 13:31:25 +01:00
|
|
|
register: pkg_install_result
|
|
|
|
until: pkg_install_result is success
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-07 12:09:13 +02:00
|
|
|
|
2020-11-10 21:17:11 +01:00
|
|
|
- name: Ensure old Iptables packages are in their desired state
|
2020-11-11 15:27:08 +01:00
|
|
|
package:
|
2019-05-07 00:00:48 +02:00
|
|
|
name: '{{ nft_old_pkg_list | list }}'
|
2017-08-18 09:25:28 +02:00
|
|
|
state: '{{ nft_old_pkg_state }}'
|
2019-02-27 13:31:25 +01:00
|
|
|
register: pkg_remove_result
|
|
|
|
until: pkg_remove_result is success
|
2018-05-16 14:38:33 +02:00
|
|
|
when: (nft_enabled|bool and
|
|
|
|
nft_old_pkg_manage|bool)
|
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
# Common configuration [[[1
|
|
|
|
- name: Ensure to create nftables.d directory
|
2017-08-18 09:18:43 +02:00
|
|
|
file:
|
|
|
|
path: "{{ nft_conf_dir_path }}"
|
|
|
|
state: directory
|
|
|
|
mode: 0755
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-18 09:18:43 +02:00
|
|
|
|
|
|
|
- name: CONFIG generate main conf file
|
2017-08-07 13:48:54 +02:00
|
|
|
template:
|
|
|
|
src: "{{ nft_main_conf_content }}"
|
|
|
|
dest: "{{ nft_main_conf_path }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-07 17:37:41 +02:00
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
- name: CONFIG generate vars definition file
|
2017-08-07 17:37:41 +02:00
|
|
|
template:
|
2019-04-16 13:48:48 +02:00
|
|
|
src: "{{ nft_define_conf_content }}"
|
|
|
|
dest: "{{ nft_define_conf_path }}"
|
2017-08-07 17:37:41 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-08 12:11:58 +02:00
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
- name: CONFIG generate sets file
|
2017-08-08 15:35:05 +02:00
|
|
|
template:
|
2019-04-16 13:48:48 +02:00
|
|
|
src: "{{ nft_set_conf_content }}"
|
|
|
|
dest: "{{ nft_set_conf_path }}"
|
2017-08-08 15:35:05 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-08 15:35:05 +02:00
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
# Filter table content [[[1
|
|
|
|
- name: Filter table - generate input rules file
|
2017-08-08 12:11:58 +02:00
|
|
|
template:
|
2019-04-16 13:48:48 +02:00
|
|
|
src: "{{ nft_input_conf_content }}"
|
|
|
|
dest: "{{ nft_input_conf_path }}"
|
2017-08-08 12:11:58 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-08 14:32:59 +02:00
|
|
|
|
2019-04-16 13:48:48 +02:00
|
|
|
- name: Filter table - generate output rules file
|
2017-08-08 14:32:59 +02:00
|
|
|
template:
|
2019-04-16 13:48:48 +02:00
|
|
|
src: "{{ nft_output_conf_content }}"
|
|
|
|
dest: "{{ nft_output_conf_path }}"
|
2017-08-08 14:32:59 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2018-05-16 14:38:33 +02:00
|
|
|
when: nft_enabled|bool
|
2017-08-09 14:27:07 +02:00
|
|
|
|
2021-03-03 10:47:02 +01:00
|
|
|
- name: Filter table - generate forward rules file
|
|
|
|
template:
|
|
|
|
src: "{{ nft_forward_conf_content }}"
|
|
|
|
dest: "{{ nft_forward_conf_path }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
|
|
|
backup: yes
|
|
|
|
notify: ['Reload nftables service']
|
|
|
|
when: (nft_enabled|bool and
|
|
|
|
nft__forward_table_manage|bool)
|
|
|
|
|
2019-04-16 15:48:30 +02:00
|
|
|
# Nat table content [[[1
|
|
|
|
- name: Nat table - generate prerouting rules file
|
|
|
|
template:
|
|
|
|
src: "{{ nft__nat_prerouting_conf_content }}"
|
|
|
|
dest: "{{ nft__nat_prerouting_conf_path }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2019-04-16 15:48:30 +02:00
|
|
|
when: (nft_enabled|bool and
|
|
|
|
nft__nat_table_manage|bool)
|
|
|
|
|
|
|
|
- name: Nat table - generate postrouting rules file
|
|
|
|
template:
|
|
|
|
src: "{{ nft__nat_postrouting_conf_content }}"
|
|
|
|
dest: "{{ nft__nat_postrouting_conf_path }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0755
|
2021-03-12 09:28:45 +01:00
|
|
|
backup: "{{ nft_backup_conf }}"
|
2020-04-21 09:53:57 +02:00
|
|
|
notify: ['Reload nftables service']
|
2019-04-16 15:48:30 +02:00
|
|
|
when: (nft_enabled|bool and
|
|
|
|
nft__nat_table_manage|bool)
|
|
|
|
|
2021-07-30 12:20:27 +02:00
|
|
|
# Manage nftables service [[[1
|
|
|
|
- name: Install nftables Debian systemd service unit
|
2018-02-06 16:58:18 +01:00
|
|
|
template:
|
|
|
|
src: '{{ nft_service_unit_content }}'
|
|
|
|
dest: '{{ nft_service_unit_path }}'
|
|
|
|
owner: 'root'
|
|
|
|
group: 'root'
|
|
|
|
mode: '0644'
|
|
|
|
register: nftables__register_systemd_service
|
2018-05-16 14:38:33 +02:00
|
|
|
when: (nft_enabled|bool and
|
|
|
|
nft_service_manage|bool)
|
2021-07-30 09:34:38 +02:00
|
|
|
notify: ['Restart nftables service']
|
|
|
|
|
2021-07-30 12:20:27 +02:00
|
|
|
- name: Ensure to remove nftables systemd service from old target
|
|
|
|
file:
|
|
|
|
path: '/etc/systemd/system/multi-user.target.wants/nftables.service'
|
|
|
|
state: absent
|
|
|
|
register: nftables__register_fix_systemd_target
|
|
|
|
when: (nft_enabled|bool and
|
|
|
|
nft_service_manage|bool)
|
|
|
|
notify: ['Restart nftables service']
|
|
|
|
|
2021-07-30 13:05:34 +02:00
|
|
|
# Manage custom nftables service [[[1
|
|
|
|
- name: Create Nftables custom directory for systemd service
|
|
|
|
file:
|
|
|
|
path: "{{ nft__service_override_path | dirname }}"
|
|
|
|
state: directory
|
2021-07-31 11:39:28 +02:00
|
|
|
mode: '0755'
|
2021-07-30 13:05:34 +02:00
|
|
|
when:
|
|
|
|
- nft_enabled|bool
|
|
|
|
- nft_service_manage|bool
|
|
|
|
- not nft__service_protect|bool
|
|
|
|
|
|
|
|
- name: Add Nftables systemd custom configuration
|
|
|
|
template:
|
|
|
|
src: '{{ nft__service_override_content }}'
|
|
|
|
dest: '{{ nft__service_override_path }}'
|
|
|
|
owner: 'root'
|
|
|
|
group: 'root'
|
|
|
|
mode: '0644'
|
|
|
|
register: nftables__register_systemd_custom
|
|
|
|
when:
|
|
|
|
- nft_enabled|bool
|
|
|
|
- nft_service_manage|bool
|
|
|
|
- not nft__service_protect|bool
|
|
|
|
notify: ['Restart nftables service']
|
|
|
|
|
2021-07-30 09:34:38 +02:00
|
|
|
# Manage custom fail2ban service [[[1
|
|
|
|
- name: Create Fail2Ban custom directory for systemd service
|
|
|
|
file:
|
|
|
|
path: "{{ nft__fail2ban_service_unit_path | dirname }}"
|
|
|
|
state: directory
|
2021-07-31 11:39:28 +02:00
|
|
|
mode: '0755'
|
2021-07-30 09:34:38 +02:00
|
|
|
when:
|
|
|
|
- nft_enabled|bool
|
|
|
|
- nft_service_manage|bool
|
|
|
|
|
|
|
|
- name: Install Debian Fail2Ban custom service
|
|
|
|
template:
|
|
|
|
src: '{{ nft__fail2ban_service_unit_content }}'
|
|
|
|
dest: '{{ nft__fail2ban_service_unit_path }}'
|
|
|
|
owner: 'root'
|
|
|
|
group: 'root'
|
|
|
|
mode: '0644'
|
|
|
|
register: nftables__register_fail2ban_service
|
|
|
|
when:
|
|
|
|
- nft_enabled|bool
|
|
|
|
- nft_service_manage|bool
|
|
|
|
notify: ['Restart nftables service']
|