Greetings Ansible users!
I have a role “nftables” that is assigned to all nodes in my inventory. It has a variable ‘forward_policy’ that has a default value:
$ cat roles/nftables/defaults/main.yaml
forward_policy: drop
I’d like to have another role (applied only to my router system) that can override that variable:
$ cat roles/router/vars/main.yaml
forward_policy: accept
I don’t know how to override the variable in the nftables role from within the router role. Does anyone have any suggestions?
Thanks for any help!
-m
PS. Here is are my current playbooks:
roles
├── nftables
│ ├── defaults
│ │ └── main.yaml
│ ├── files
│ │ └── etc
│ │ ├── nftables.conf
│ │ └── nftables.conf.d
│ │ └── 000-flush-ruleset.nft
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ └── templates
│ └── etc
│ └── nftables.conf.d
│ └── 050-default-chains.nft.j2
└── router
├── files
│ └── etc
│ └── nftables.conf.d
│ └── 070-default-nat-table.nft
├── tasks
│ ├── main.yaml
│ └── nftables_nat_table.yaml
└── vars
└── main.yaml
17 directories, 10 files
And the contents:
───────┬────────────────────────────────────────────────────────────────────────
│ File: hosts.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ ungrouped:
2 │ hosts:
3 │ zed:
4 │
5 │ router:
6 │ hosts:
7 │ zed:
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/nftables/defaults/main.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ forward_policy: drop
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/nftables/files/etc/nftables.conf
───────┼────────────────────────────────────────────────────────────────────────
1 │ #!/usr/sbin/nft -f
2 │
3 │ include “/etc/nftables.conf.d/*.nft”
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/nftables/files/etc/nftables.conf.d/000-flush-ruleset.nft
───────┼────────────────────────────────────────────────────────────────────────
1 │ flush ruleset
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/nftables/handlers/main.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ -
2 │ name: restart nftables
3 │ service:
4 │ name: nftables
5 │ state: restarted
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/nftables/tasks/main.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ —
2 │ # This playbook contains plays to configure nftables.
3 │
4 │ -
5 │ name: enable service nftables
6 │ service:
7 │ name: nftables
8 │ enabled: yes
9 │
10 │ -
11 │ name: configure nftables.conf
12 │ copy:
13 │ src: files/etc/nftables.conf
14 │ dest: /etc/nftables.conf
15 │ tags:
16 │ - nftables
17 │ notify: restart nftables
18 │
19 │ -
20 │ name: configure nftables.conf.d
21 │ copy:
22 │ src: files/etc/nftables.conf.d
23 │ dest: /etc
24 │ tags:
25 │ - nftables
26 │ notify: restart nftables
27 │
28 │ -
29 │ name: configure nftables.conf.d/050-default-chains.nft
30 │ template:
31 │ src: etc/nftables.conf.d/050-default-chains.nft.j2
32 │ dest: /etc/nftables.conf.d/050-default-chains.nft
33 │ tags:
34 │ - nftables
35 │ notify: restart nftables
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/nftables/templates/etc/nftables.conf.d/050-default-chains.nft.j2
───────┼────────────────────────────────────────────────────────────────────────
1 │ table inet filter {
2 │ chain input {
3 │ type filter hook input
4 │ priority 0;
5 │ policy accept;
6 │ }
7 │ chain forward {
8 │ type filter hook forward
9 │ priority 0;
10 │ policy {{ forward_policy }};
11 │ }
12 │ chain output {
13 │ type filter hook output
14 │ priority 0;
15 │ policy accept;
16 │ }
17 │ }
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/router/files/etc/nftables.conf.d/070-default-nat-table.nft
───────┼────────────────────────────────────────────────────────────────────────
1 │ table inet nat {
2 │ chain postrouting {
3 │ type nat hook postrouting
4 │ priority srcnat;
5 │ }
6 │
7 │ chain prerouting {
8 │ type nat hook prerouting
9 │ priority dstnat;
10 │ }
11 │ }
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/router/tasks/main.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ —
2 │ # This playbook contains plays to configure the router.
3 │
4 │ -
5 │ include_tasks: nftables_nat_table.yaml
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/router/tasks/nftables_nat_table.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ —
2 │ # This playbook contains plays to add nat table for nftables.
3 │
4 │ -
5 │ name: configure nftables nat table
6 │ copy:
7 │ src: files/etc/nftables.conf.d/070-default-nat-table.nft
8 │ dest: /etc/nftables.conf.d/070-default-nat-table.nft
9 │ tags:
10 │ - router
11 │ notify: restart nftables
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: roles/router/vars/main.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ forward_policy: accept
───────┴────────────────────────────────────────────────────────────────────────
───────┬────────────────────────────────────────────────────────────────────────
│ File: site.yaml
───────┼────────────────────────────────────────────────────────────────────────
1 │ —
2 │ -
3 │ name: apply common configuration to all nodes
4 │ hosts: all
5 │ roles:
6 │ - nftables
7 │
8 │ -
9 │ name: apply router configurations
10 │ hosts: router
11 │ roles:
12 │ - router
───────┴────────────────────────────────────────────────────────────────────────