CloudRouterDemo

This commit is contained in:
ayurishchev committed 2025-11-17 14:19:38 +03:00
commit 62c86b96ff
23 files changed
+1382

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
---
- name: save iptables
command: netfilter-persistent save
+69
View File
@@ -0,0 +1,69 @@
---
- name: Update packages
apt:
upgrade: dist
update_cache: yes
cache_valid_time: 3600
- name: Install System Utils Packages
apt:
name:
- net-tools
- nmon
- htop
- bmon
- mtr
state: present
- name: Enable IP forwarding
sysctl:
name: net.ipv4.ip_forward
value: '1'
state: present
reload: yes
- name: Load GRE module
modprobe:
name: ip_gre
state: present
- name: Persist GRE module
lineinfile:
path: /etc/modules
line: ip_gre
create: yes
- name: Pre-seed iptables-persistent IPv4
debconf:
name: iptables-persistent
question: iptables-persistent/autosave_v4
value: "true"
vtype: boolean
- name: Pre-seed iptables-persistent IPv6
debconf:
name: iptables-persistent
question: iptables-persistent/autosave_v6
value: "false"
vtype: boolean
- name: Install iptables-persistent
apt:
name: iptables-persistent
state: present
- name: NAT masquerade for LAN
iptables:
table: nat
chain: POSTROUTING
jump: MASQUERADE
source: "{{ lan_cidr }}"
out_interface: "eth0"
comment: "LAN to Internet"
notify: save iptables
- name: Allow forwarding
iptables:
chain: FORWARD
policy: ACCEPT
notify: save iptables
+26
View File
@@ -0,0 +1,26 @@
---
- name: Disable IPv6 via sysctl
sysctl:
name: "{{ item }}"
value: '1'
sysctl_set: yes
state: present
reload: yes
loop:
- net.ipv6.conf.all.disable_ipv6
- net.ipv6.conf.default.disable_ipv6
- net.ipv6.conf.lo.disable_ipv6
- name: Disable IPv6 in GRUB
lineinfile:
path: /etc/default/grub
regexp: '^GRUB_CMDLINE_LINUX='
line: 'GRUB_CMDLINE_LINUX="ipv6.disable=1"'
- name: Update GRUB
command: update-grub
when: ansible_distribution == "Ubuntu"
- name: Update initramfs
command: update-initramfs -u
when: ansible_distribution == "Ubuntu"
@@ -0,0 +1,5 @@
- name: Restart FRR
systemd:
name: frr
state: restarted
daemon_reload: yes
+62
View File
@@ -0,0 +1,62 @@
---
- name: Update package cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Install FRR and required packages
apt:
name:
- frr
- frr-pythontools
state: present
- name: Create FRR configuration directory
file:
path: /etc/frr
state: directory
owner: frr
group: frr
mode: '0755'
- name: Enable FRR daemons
lineinfile:
path: /etc/frr/daemons
regexp: '^{{ item.daemon }}='
line: '{{ item.daemon }}={{ item.state }}'
backup: yes
loop:
- { daemon: 'bgpd', state: 'yes' }
- { daemon: 'zebra', state: 'yes' }
- { daemon: 'staticd', state: 'yes' }
- name: Configure FRR startup options
lineinfile:
path: /etc/frr/daemons
regexp: '^{{ item.option }}='
line: '{{ item.option }}={{ item.value }}'
backup: yes
loop:
- { option: 'frr_options', value: '"-A 127.0.0.1"' }
- name: Configure FRR
template:
src: bgpd_router.conf.j2
dest: /etc/frr/frr.conf
owner: frr
group: frr
mode: '0640'
notify: Restart FRR
- name: Restart FRR
systemd:
name: frr
state: restarted
daemon_reload: yes
# - name: Wait for FRR to be fully started
# wait_for:
# path: /run/frr/bgpd.vty
# state: present
# timeout: 30
# when: ansible_service_mgr == "systemd"
@@ -0,0 +1,58 @@
!
frr version
frr defaults traditional
hostname {{ inventory_hostname }}
log syslog informational
!
router bgp {{ local_asn }}
bgp router-id {{ lan_ip }}
!
network {{ lan_cidr }}
network {{ external_cidr }}
!
neighbor {{ hostvars['router1'].lan_ip if inventory_hostname != 'router1' else hostvars['router2'].lan_ip }} remote-as {{ local_asn }}
neighbor {{ hostvars['router1'].lan_ip if inventory_hostname != 'router1' else hostvars['router2'].lan_ip }} update-source eth1
!
neighbor {{ gre_main_ip | ipmath(1) }} remote-as {{ remote_asn }}
neighbor {{ gre_main_ip | ipmath(1) }} update-source gre-main
neighbor {{ gre_main_ip | ipmath(1) }} timers 10 30
!
neighbor {{ gre_backup_ip | ipmath(1) }} remote-as {{ remote_asn }}
neighbor {{ gre_backup_ip | ipmath(1) }} update-source gre-backup
neighbor {{ gre_backup_ip | ipmath(1) }} timers 10 30
!
address-family ipv4 unicast
neighbor {{ hostvars['router1'].lan_ip if inventory_hostname != 'router1' else hostvars['router2'].lan_ip }} activate
neighbor {{ hostvars['router1'].lan_ip if inventory_hostname != 'router1' else hostvars['router2'].lan_ip }} next-hop-self
neighbor {{ gre_main_ip | ipmath(1) }} activate
neighbor {{ gre_backup_ip | ipmath(1) }} activate
neighbor {{ gre_main_ip | ipmath(1) }} route-map SET-PREF-{{ local_pref_main }} in
neighbor {{ gre_backup_ip | ipmath(1) }} route-map SET-PREF-{{ local_pref_backup }} in
neighbor {{ hostvars['router1'].lan_ip if inventory_hostname != 'router1' else hostvars['router2'].lan_ip }} route-map OUT-LAN out
neighbor {{ gre_main_ip | ipmath(1) }} route-map OUT-EXTERNAL out
neighbor {{ gre_backup_ip | ipmath(1) }} route-map OUT-EXTERNAL out
exit-address-family
!
route-map SET-PREF-400 permit 10
set local-preference 400
!
route-map SET-PREF-300 permit 10
set local-preference 300
!
route-map SET-PREF-200 permit 10
set local-preference 200
!
route-map SET-PREF-100 permit 10
set local-preference 100
!
route-map OUT-LAN permit 10
match ip address prefix-list LAN-ONLY
!
route-map OUT-EXTERNAL permit 10
match ip address prefix-list EXTERNAL-ONLY
!
ip prefix-list LAN-ONLY seq 5 permit {{ lan_cidr }}
ip prefix-list EXTERNAL-ONLY seq 5 permit {{ external_cidr }}
!
line vty
!
@@ -0,0 +1,104 @@
!
! FRR BGP Configuration Template (Managed by Ansible)
!
frr version
frr defaults traditional
hostname {{ inventory_hostname }}
log syslog informational
!
! Static route for originating a network in BGP if it's not directly connected
!
! Interface configurations
interface eth1
ip address {{ lan_ip }}/{{ lan_network_prefix }}
!
interface gre-main
ip address {{ gre_main_ip }}/30
!
interface gre-backup
ip address {{ gre_backup_ip }}/30
!
! BGP Configuration
router bgp {{ local_asn }}
bgp router-id {{ lan_ip }}
!
! Networks to originate from this router
network {{ lan_cidr }}
!
! iBGP neighbor configuration
neighbor {{ remote_ibgp_peer }} remote-as {{ local_asn }}
neighbor {{ remote_ibgp_peer }} update-source eth1
!
! eBGP neighbors configuration
neighbor {{ gre_main_ip | ipmath(1) }} remote-as {{ remote_asn }}
neighbor {{ gre_main_ip | ipmath(1) }} ebgp-multihop 255
neighbor {{ gre_main_ip | ipmath(1) }} update-source gre-main
neighbor {{ gre_main_ip | ipmath(1) }} timers 10 30
!
neighbor {{ gre_backup_ip | ipmath(1) }} remote-as {{ remote_asn }}
neighbor {{ gre_backup_ip | ipmath(1) }} ebgp-multihop 255
neighbor {{ gre_backup_ip | ipmath(1) }} update-source gre-backup
neighbor {{ gre_backup_ip | ipmath(1) }} timers 10 30
!
address-family ipv4 unicast
! Activate neighbors
neighbor {{ remote_ibgp_peer }} activate
neighbor {{ gre_main_ip | ipmath(1) }} activate
neighbor {{ gre_backup_ip | ipmath(1) }} activate
! Policy configurations for neighbors
neighbor {{ remote_ibgp_peer }} next-hop-self
neighbor {{ remote_ibgp_peer }} route-map ALLOW-RFC1918-OUT out
neighbor {{ gre_main_ip | ipmath(1) }} route-map FROM-MAIN-PEER in
neighbor {{ gre_main_ip | ipmath(1) }} route-map TO-MAIN-PEER out
neighbor {{ gre_backup_ip | ipmath(1) }} route-map FROM-BACKUP-PEER in
neighbor {{ gre_backup_ip | ipmath(1) }} route-map TO-BACKUP-PEER out
exit-address-family
!
!
! Route Maps for BGP Policy
!
! Inbound policy from the main external peer
route-map FROM-MAIN-PEER permit 10
match ip address prefix-list RFC1918-NETS
set local-preference {{ local_pref_main | default(400) }}
!
! Inbound policy from the backup external peer
route-map FROM-BACKUP-PEER permit 10
match ip address prefix-list RFC1918-NETS
set local-preference {{ local_pref_backup | default(300) }}
!
! Outbound policy for the main external peer
route-map TO-MAIN-PEER permit 10
match ip address prefix-list RFC1918-NETS
set metric {{ bgp_med_main }}
!
! Outbound policy for the main external peer
route-map TO-BACKUP-PEER permit 10
match ip address prefix-list RFC1918-NETS
set metric {{ bgp_med_backup }}
!
!
! Prefix List for RFC1918 networks and default route filtering
!
! Rule 1: Explicitly deny the default route
ip prefix-list RFC1918-NETS seq 5 deny 0.0.0.0/0
! Rule 2: Permit 10.0.0.0/8 and all its subnets
ip prefix-list RFC1918-NETS seq 10 permit 10.0.0.0/8 le 32
! Rule 3: Permit 172.16.0.0/12 and all its subnets
ip prefix-list RFC1918-NETS seq 15 permit 172.16.0.0/12 le 32
! Rule 4: Permit 192.168.0.0/16 and all its subnets
ip prefix-list RFC1918-NETS seq 20 permit 192.168.0.0/16 le 32
!
! VTY lines for management access
line vty
!
+3
View File
@@ -0,0 +1,3 @@
---
- name: save iptables
command: netfilter-persistent save
+60
View File
@@ -0,0 +1,60 @@
---
- name: Create GRE tunnels via Netplan
template:
src: gre-netplan.yaml.j2
dest: "/etc/netplan/10-gre-{{ item }}.yaml"
mode: '0600'
loop:
- main
- backup
- name: Apply Netplan
command: netplan apply
- name: Refresh interface facts
ansible.builtin.setup:
gather_subset:
- 'interfaces'
- name: Discover GRE interfaces
ansible.builtin.set_fact:
gre_interfaces: "{{ ansible_interfaces | select('match', '^gre-(m|b).*') | list }}"
- name: Debug GRE interfaces
ansible.builtin.debug:
msg: "Found GRE interfaces: {{ gre_interfaces }}"
- name: Validate that GRE interfaces exist
ansible.builtin.assert:
that:
- gre_interfaces | length > 0
fail_msg: "No GRE interfaces found"
success_msg: "GRE interfaces found: {{ gre_interfaces }}"
- name: Apply MSS clamping rule only if not already exists
ansible.builtin.shell: |
if ! iptables -t mangle -C FORWARD -d {{ lan_cidr }} -i {{ item }} -p tcp --tcp-flags SYN,RST SYN -m length --length 1321:65535 -j TCPMSS --set-mss 1320 -m comment --comment 'Clamp MSS to 1320 for GRE tunnel {{ item }}' 2>/dev/null; then
iptables -t mangle -I FORWARD -d {{ lan_cidr }} -i {{ item }} -p tcp --tcp-flags SYN,RST SYN -m length --length 1321:65535 -j TCPMSS --set-mss 1320 -m comment --comment 'Clamp MSS to 1320 for GRE tunnel {{ item }}'
echo "rule_added"
else
echo "rule_already_exists"
fi
loop: "{{ gre_interfaces }}"
register: iptables_shell
changed_when:
- iptables_shell.stdout == "rule_added"
notify: save iptables
- name: Apply MSS clamping rule only if not already exists
ansible.builtin.shell: |
if ! iptables -t mangle -C FORWARD -s {{ lan_cidr }} -o {{ item }} -p tcp --tcp-flags SYN,RST SYN -m length --length 1321:65535 -j TCPMSS --set-mss 1320 -m comment --comment 'Clamp MSS to 1320 for GRE tunnel {{ item }}' 2>/dev/null; then
iptables -t mangle -I FORWARD -s {{ lan_cidr }} -o {{ item }} -p tcp --tcp-flags SYN,RST SYN -m length --length 1321:65535 -j TCPMSS --set-mss 1320 -m comment --comment 'Clamp MSS to 1320 for GRE tunnel {{ item }}'
echo "rule_added"
else
echo "rule_already_exists"
fi
loop: "{{ gre_interfaces }}"
register: iptables_shell
changed_when:
- iptables_shell.stdout == "rule_added"
notify: save iptables
@@ -0,0 +1,19 @@
{% if item == "main" %}
{% set gre_ip = gre_main_ip %}
{% set remote_wan = remote_main_isp_ip %}
{% elif item == "backup" %}
{% set gre_ip = gre_backup_ip %}
{% set remote_wan = remote_backup_isp_ip %}
{% endif %}
network:
version: 2
tunnels:
gre-{{ item }}:
mode: gre
local: {{ wan_ip }}
remote: {{ remote_wan }}
addresses:
- {{ gre_ip }}/30
mtu: 1360
dhcp4: no
dhcp6: no
+17
View File
@@ -0,0 +1,17 @@
---
- name: Install Keepalived
apt:
name: keepalived
state: present
- name: Configure Keepalived
template:
src: keepalived.conf.j2
dest: /etc/keepalived/keepalived.conf
mode: '0644'
- name: Enable and start Keepalived
systemd:
name: keepalived
enabled: yes
state: restarted
@@ -0,0 +1,19 @@
vrrp_instance VI_1 {
interface eth1
state BACKUP
# set priority
priority {% if inventory_hostname == 'router1' %}101{% else %}100{% endif %}
# set VRRP Router ID
virtual_router_id 51
advert_int 1
authentication {
auth_type PASS
auth_pass secret123
}
virtual_ipaddress {
{{ vrrp_vip }}/{{ lan_network_prefix }}
}
}
+17
View File
@@ -0,0 +1,17 @@
---
- name: Update packages
apt:
upgrade: dist
update_cache: yes
cache_valid_time: 3600
- name: Install utilities
apt:
name:
- net-tools
- traceroute
- iproute2
- mtr
- bmon
- htop
state: present
+32
View File
@@ -0,0 +1,32 @@
---
- name: Install StrongSwan including extra plugins
apt:
name: [strongswan, strongswan-swanctl, libstrongswan-extra-plugins]
state: present
- name: Configure swanctl.conf
template:
src: swanctl.conf.j2
dest: /etc/swanctl/conf.d/deployment.conf
mode: '0644'
- name: Configure StrongSwan charon.conf to load all tunnels on startup
block:
- name: Check if swanctl startup command already exists in charon.conf
command: grep -q "swanctl = /usr/sbin/swanctl --load-all" /etc/strongswan.d/charon.conf
register: swanctl_exists
failed_when: false
changed_when: false
- name: Update charon.conf to include swanctl startup command
shell: |
sed -i '/start-scripts\s*{/,/}/{
/}/i\ swanctl = /usr/sbin/swanctl --load-all
}' /etc/strongswan.d/charon.conf
when: swanctl_exists.rc != 0
- name: Restart StrongSwan (using strongswan-starter)
systemd:
name: strongswan-starter
state: restarted
enabled: yes
@@ -0,0 +1,58 @@
connections {
gre-main {
local_addrs = {{ wan_ip }}
remote_addrs = {{ remote_main_isp_ip }}
local { auth = psk }
remote { auth = psk }
version = 2
proposals = {{ ike_proposal }}
rekey_time = {{ ike_lifetime }}s
children {
gre-main {
local_ts = dynamic[gre]
remote_ts = dynamic[gre]
esp_proposals = {{ esp_proposal }}
rekey_time = {{ esp_lifetime }}s
mode = transport
dpd_action = restart
close_action = restart
}
}
dpd_timeout = {{ dpd_timeout }}s
}
gre-backup {
local_addrs = {{ wan_ip }}
remote_addrs = {{ remote_backup_isp_ip }}
local { auth = psk }
remote { auth = psk }
version = 2
proposals = {{ ike_proposal }}
rekey_time = {{ ike_lifetime }}s
children {
gre-backup {
mode = transport
local_ts = dynamic[gre]
remote_ts = dynamic[gre]
esp_proposals = {{ esp_proposal }}
rekey_time = {{ esp_lifetime }}s
mode = transport
dpd_action = restart
close_action = restart
}
}
dpd_timeout = {{ dpd_timeout }}s
}
}
secrets {
ike {
secret = "{{ ipsec_psk }}"
}
}