changed to parse yaml for network

This commit is contained in:
Jonas Forsberg 2021-09-22 12:30:23 +02:00
parent a0bc0eecf3
commit 68248df359
4 changed files with 34 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1 @@
network.env
salt/pillars/local.sls salt/pillars/local.sls

View File

@ -1,3 +0,0 @@
INTERNAL_IF="eth0"
INTERNAL_ADDRESS="192.168.0.1/24"
EXTERNAL_IF="eth0.1000"

View File

@ -1,7 +1,8 @@
network: network:
domain: suse.lan domain: suse.lan
address: 192.168.0.0 netaddress: 192.168.0.0
netmask: 24 netmask: 24
ip: 192.168.0.1
interface: interface:
internal: eth0 internal: eth0

View File

@ -40,6 +40,24 @@ function log(){
printf "%s\n" "$*" printf "%s\n" "$*"
} }
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
function check_prerequisites(){ function check_prerequisites(){
set +e set +e
local answer local answer
@ -70,17 +88,20 @@ function configure_network(){
if asktobreak; then if asktobreak; then
return return
fi fi
source network.env eval $(parse_yaml salt/pillars/network.sls "SALT_")
sudo nmcli connection show if [[ -f salt/pillars/local.sls ]];then
sudo nmcli connection modify "$INTERNAL_IF" ipv4.addresses "$INTERNAL_ADDRESS" eval $(parse_yaml salt/pillars/local.sls "SALT_")
sudo nmcli connection modify "$INTERNAL_IF" ipv4.dns "127.0.0.1, 1.1.1.1, 1.1.1.2" fi
sudo nmcli connection modify "$INTERNAL_IF" ipv4.method manual sudo nmcli connection modify "$SALT_network_interface_internal" ipv4.addresses "$SALT_network_ip"
sudo nmcli connection down "$INTERNAL_IF" sudo nmcli connection modify "$SALT_network_interface_internal" ipv4.dns "127.0.0.1, 1.1.1.1, 1.1.1.2"
sudo nmcli connection up "$INTERNAL_IF" sudo nmcli connection modify "$SALT_network_interface_internal" ipv4.method manual
sudo nmcli connection down "$SALT_network_interface_internal"
sudo nmcli connection add type vlan con-name "$EXTERNAL_IF" ifname "$EXTERNAL_IF" dev "$INTERNAL_IF" id "${EXTERNAL_IF#*.}" sudo nmcli connection up "$SALT_network_interface_internal"
sudo nmcli connection modify "$EXTERNAL_IF" ipv4.method auto if [[ $(nmcli connection show | grep -i $SALT_network_interface_external | wc -l) == 0 ]]; then
sudo nmcli connection up "$EXTERNAL_IF" sudo nmcli connection add type vlan con-name "$SALT_network_interface_external" ifname "$SALT_network_interface_external" dev "$SALT_network_interface_internal" id "${SALT_network_interface_external#*.}"
fi
sudo nmcli connection modify "$SALT_network_interface_external" ipv4.method auto
sudo nmcli connection up "$SALT_network_interface_external"
} }