diff --git a/README.md b/README.md index f499b8b..355fcf0 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,12 @@ Release notes for the project can be found at: static_routes: - Gateway: "10.1.0.1" Destination: "10.1.0.0/24" + - Gateway: "192.168.0.1" + Table: 100 + routing_rules: + - IncomingInterface: bond1 + Priority: 900 + Table: 100 config_overrides: Network: ConfigureWithoutCarrier: true diff --git a/defaults/main.yml b/defaults/main.yml index 423f7f6..ac54276 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -96,6 +96,8 @@ systemd_netdevs: [] # `static_routes` -- (optional) list of routes to use for the network. This # option requires a gateway and cidr to be set # within the list item. +# `routing_rules` -- (optional) list of routing rule policies which will be +# applied for the interface. # `ipforward` -- (optional) When set to true forwarding will be enabled for # interface. This setting will enable # net.ipv4.ip_forward, net.ipv4.conf.all.forwarding @@ -112,6 +114,14 @@ systemd_netdevs: [] # - interface: "bond0" # bridge: "br-dummy" # mtu: 9000 +# static_routes: +# - gateway: 10.0.0.1 +# destination: 0.0.0.0/0 +# table: 100 +# routing_rules: +# - From: 192.168.0.0/24 +# Priority: 900 +# Table: 100 # - interface: "br-dummy" # bridge: "br-dummy" # address: "10.0.0.100" diff --git a/releasenotes/notes/routing_policy_rules-188c8ff5e8c5450f.yaml b/releasenotes/notes/routing_policy_rules-188c8ff5e8c5450f.yaml new file mode 100644 index 0000000..6f9680c --- /dev/null +++ b/releasenotes/notes/routing_policy_rules-188c8ff5e8c5450f.yaml @@ -0,0 +1,9 @@ +--- +features: + - | + Added `routing_rules` to manage routing policy rules for the network + interface. + It is a list of mappings, where each mapping accept keys applicable + for section RoutingPolicyRule of systemd.network. + You can check documentation for `systemd.network `_ + for more details. diff --git a/tasks/main.yml b/tasks/main.yml index 148e32e..481f67c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -194,6 +194,17 @@ state: directory loop: "{{ _systemd_networks_named | selectattr('static_routes', 'defined') | map(attribute='filename') }}" +- name: Place systemd-networkd routing policy rules + ansible.builtin.template: + src: systemd-network-rules.j2 + dest: "/etc/systemd/network/{{ item['filename'] }}.network.d/rules.conf" + owner: "root" + group: "root" + mode: "0644" + loop: "{{ _systemd_networks_named | selectattr('routing_rules', 'defined') }}" + notify: + - Restart systemd-networkd + - name: Place systemd-networkd network routes ansible.builtin.template: src: systemd-network-routes.j2 diff --git a/templates/systemd-network-rules.j2 b/templates/systemd-network-rules.j2 new file mode 100644 index 0000000..3901a61 --- /dev/null +++ b/templates/systemd-network-rules.j2 @@ -0,0 +1,9 @@ +# {{ ansible_managed }} + +{% for rule in item['routing_rules'] %} +[RoutingPolicyRule] +{% for key, val in rule.items() %} +{{ key }}={{ val }} +{% endfor %} + +{% endfor %} diff --git a/tests/test.yml b/tests/test.yml index dad8836..8320484 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -76,6 +76,12 @@ static_routes: - Gateway: "10.1.0.1" cidr: "10.1.0.0/24" + - Gateway: "10.1.0.100" + Table: 100 + routing_rules: + - From: 192.168.0.0/24 + Priority: 900 + Table: 100 config_overrides: Network: ConfigureWithoutCarrier: true