Commit 105c24a0 authored by nanahira's avatar nanahira

update for raspi

parent cbdd7063
...@@ -16,6 +16,43 @@ ip addr add {{address}} dev {{br.name}} ...@@ -16,6 +16,43 @@ ip addr add {{address}} dev {{br.name}}
_change_mtu -A "$BRIDGE_NAME" _change_mtu -A "$BRIDGE_NAME"
replace_ip() {
local BRIDGE_IP="$1"
local INPUT="$2"
# 检查 $INPUT 是否包含点
if [[ "$INPUT" == *.* ]]; then
IFS='.' read -r -a INPUT_PARTS <<< "$INPUT"
PART_COUNT=${#INPUT_PARTS[@]}
# 如果有 4 个或以上的部分,那么这是一个完整的 IP 地址,直接返回
if [[ $PART_COUNT -ge 4 ]]; then
echo "$INPUT"
else
# 根据 PART_COUNT,补全 IP 地址
AWK_SCRIPT='{print '
for ((i = 0; i < $((4 - PART_COUNT)); i++)); do
AWK_SCRIPT="${AWK_SCRIPT}\$$((i + 1))"
if [[ $i -lt 3 ]]; then
AWK_SCRIPT="${AWK_SCRIPT}\".\""
fi
done
AWK_SCRIPT="$AWK_SCRIPT}"
IP_PREFIX=$(echo $BRIDGE_IP | awk -F'.' "$AWK_SCRIPT")
RESULT_IP="${IP_PREFIX}${INPUT}"
echo "$RESULT_IP"
fi
elif [[ $INPUT =~ ^[0-9]+$ ]]; then
# 如果 $INPUT 但是是纯数字,则替换最后一个数字
IP_PREFIX=$(echo $BRIDGE_IP | awk -F'.' '{print $1"."$2"."$3"."}')
RESULT_IP="${IP_PREFIX}${INPUT}"
echo "$RESULT_IP"
else
# 如果 $INPUT 输入非法,直接返回输入内容
echo "$INPUT"
fi
}
handle_gateway() { handle_gateway() {
GATEWAY_ID=$1 GATEWAY_ID=$1
GATEWAY_ADDRESS=$2 GATEWAY_ADDRESS=$2
...@@ -25,11 +62,13 @@ handle_gateway() { ...@@ -25,11 +62,13 @@ handle_gateway() {
BRIDGE_IP=$(ip -4 addr show dev "$BRIDGE_NAME" | grep 'inet ' | awk '{print $2}') BRIDGE_IP=$(ip -4 addr show dev "$BRIDGE_NAME" | grep 'inet ' | awk '{print $2}')
if [[ "$GATEWAY_ADDRESS" == "_use_first" ]]; then if [[ "$GATEWAY_ADDRESS" == "_use_first" ]]; then
GATEWAY_ADDRESS=$(subnetcalc "$BRIDGE_IP" -n | grep "Host Range" | awk '{print $5}') GATEWAY_ADDRESS=$(subnetcalc "$BRIDGE_IP" -n | grep "Host Range" | awk '{print $5}')
fi elif [[ "$GATEWAY_ADDRESS" == "_use_last" ]]; then
if [[ "$GATEWAY_ADDRESS" == "_use_last" ]]; then
GATEWAY_ADDRESS=$(subnetcalc "$BRIDGE_IP" -n | grep "Host Range" | awk '{print $7}') GATEWAY_ADDRESS=$(subnetcalc "$BRIDGE_IP" -n | grep "Host Range" | awk '{print $7}')
else
GATEWAY_ADDRESS=$(replace_ip "$BRIDGE_IP" "$GATEWAY_ADDRESS")
fi fi
ip route replace default via "$GATEWAY_ADDRESS" dev "$BRIDGE_NAME" table "$GATEWAY_NEXT_HOP_MARK" ip route replace default via "$GATEWAY_ADDRESS" dev "$BRIDGE_NAME" table "$GATEWAY_NEXT_HOP_MARK"
{% if br.type != "dhcp" %} {% if br.type != "dhcp" %}
ip route add default via "$GATEWAY_ADDRESS" dev "$BRIDGE_NAME" metric "$GATEWAY_NEXT_HOP_MARK" ip route add default via "$GATEWAY_ADDRESS" dev "$BRIDGE_NAME" metric "$GATEWAY_NEXT_HOP_MARK"
......
...@@ -4,9 +4,12 @@ ...@@ -4,9 +4,12 @@
become: true become: true
vars: vars:
kernel_modules: kernel_modules:
- macvlan - name: macvlan
- bonding path: drivers/net/macvlan.ko
- 8021q - name: bonding
path: drivers/net/bonding/bonding.ko
- name: 8021q
path: net/8021q/8021q.ko
tasks: tasks:
- name: apt - name: apt
apt: apt:
...@@ -24,11 +27,22 @@ ...@@ -24,11 +27,22 @@
apt: apt:
name: vlan name: vlan
when: not ansible_kernel.endswith("-pve") when: not ansible_kernel.endswith("-pve")
# check file exists at /lib/modules/{{ansible_kernel}}/kernel/{{item.path}}, and if not, remove it from kernel_modules
- name: filter existing kernel modules
stat:
path: '/lib/modules/{{ansible_kernel}}/kernel/{{item.path}}'
register: 'kernel_module_stat'
with_items: '{{kernel_modules}}'
- name: remove non-existing kernel modules
set_fact:
kernel_modules: '{{kernel_modules | rejectattr("name", "equalto", item.item.name) | list}}'
when: not item.stat.exists
with_items: '{{kernel_module_stat.results}}'
- name: modules file - name: modules file
copy: copy:
content: | content: |
{% for module in kernel_modules %} {% for module in kernel_modules %}
{{module}} {{module.name}}
{% endfor %} {% endfor %}
dest: /etc/modules-load.d/mycard-router-nextgen.conf dest: /etc/modules-load.d/mycard-router-nextgen.conf
notify: load_modules notify: load_modules
...@@ -50,5 +64,5 @@ ...@@ -50,5 +64,5 @@
- cturra/ntp - cturra/ntp
handlers: handlers:
- name: load_modules - name: load_modules
shell: 'modprobe {{item}}' shell: 'modprobe {{item.name}}'
with_items: '{{kernel_modules}}' with_items: '{{kernel_modules}}'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment