Skip to content

Commit

Permalink
* Replace Ubuntu initialization with Debian logic
Browse files Browse the repository at this point in the history
* Use the Vagrant generated private key to access boxes (update Ansible inventory)
  • Loading branch information
jbemmel committed Dec 8, 2024
1 parent 64ec9e3 commit 1c6134c
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 7 deletions.
140 changes: 140 additions & 0 deletions netsim/ansible/templates/initial/linux/debian.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
echo -n 'Starting initial config ' && date

# (Overwrite resolved config to remove DNS stuff)
# cat <<SCRIPT > /etc/systemd/resolved.conf
# [Resolve]
# DNS=
# FallbackDNS=
# Domains=
# DNSOverTLS=no
# Cache=yes
# DNSStubListener=yes
# SCRIPT
# systemctl restart systemd-resolved
# Set persistent hostname
hostnamectl set-hostname {{ inventory_hostname }}
# Update APT and install netplan
apt-get update -qq
apt-get install -qq nplan
{% if netlab_net_tools|default(False) %}
#
# Install net-tools (arp, route...)
#
if which arp; then
echo "net-tools already installed"
else
apt-get install -qq net-tools
fi
if which traceroute; then
echo "traceroute already installed"
else
apt-get install -qq traceroute
fi
{% endif %}
{% if netlab_lldp_enable|default(False) %}
#
# Enable LLDP
#
if systemctl is-active --quiet lldpd.service; then
echo "LLDP already installed"
else
apt-get install -qq lldpd
fi
cat <<CONFIG >/etc/lldpd.d/system.conf
configure lldp tx-interval 30
configure lldp tx-hold 3
configure system interface pattern *,!eth0,eth*
CONFIG
systemctl enable lldpd
systemctl restart lldpd
{% endif %}
# Sysctl settings: IPv4/IPv6 forwarding, IPv6 LLA
#
{% set pkt_fwd = "1" if role|default("host") == "router" else "0" %}
cat <<SCRIPT > /etc/sysctl.d/10-netsim.conf
net.ipv4.ip_forward={{ pkt_fwd }}
net.ipv6.conf.all.forwarding={{ pkt_fwd }}
{% if loopback.ipv6 is defined %}
net.ipv6.conf.lo.disable_ipv6=0
{% endif %}
{% for l in interfaces|default([]) %}
{% if l.ipv6 is defined %}
net.ipv6.conf.{{ l.ifname }}.disable_ipv6=0
{% endif %}
{% endfor %}
SCRIPT
sysctl -p /etc/sysctl.d/10-netsim.conf
# Loopback addressing, JvB commented out
{% if loopback.ipv4 is defined or loopback.ipv6 is defined %}
cat <<SCRIPT > /etc/netplan/02-loopback.yaml
network:
version: 2
renderer: networkd
ethernets:
lo:
addresses:
{% if 'ipv4' in loopback %}
- {{ loopback.ipv4 }}
{% endif %}
{% if 'ipv6' in loopback %}
- {{ loopback.ipv6 }}
{% endif %}
SCRIPT
{% endif %}
# Interface addressing
{% for l in interfaces|default([]) if (l.ipv4 is defined or l.ipv6 is defined or l.dhcp is defined)%}
cat <<SCRIPT > /etc/netplan/03-eth-{{ l.ifname }}.yaml
network:
version: 2
renderer: networkd
ethernets:
{{ l.ifname }}:
{% if l.dhcp.client.ipv4|default(False) %}
dhcp4: true
{% endif %}
{% if l.dhcp.client.ipv6|default(False) %}
dhcp6: true
{% endif %}
{% for af in ('ipv4','ipv6') if af in l %}
{% if loop.first %}
addresses:
{% endif %}
- {{ l[af] }}
{% endfor %}
{% if l.mtu is defined %}
mtu: {{ l.mtu }}
{% endif %}
SCRIPT
{% endfor %}
# Add routes to IPv4 address pools pointing to the first neighbor on the first link
{% for ifdata in interfaces|default([]) if ifdata.gateway is defined %}
cat <<SCRIPT > /etc/netplan/04-routes-{{ ifdata.ifname }}.yaml
network:
version: 2
renderer: networkd
ethernets:
{{ ifdata.ifname }}:
routes:
{% for name,pool in pools.items()|default({}) %}
{% for af,pfx in pool.items() if af == 'ipv4' and name != 'mgmt' and name != 'router_id' %}
- to: {{ pfx }}
via: {{ ifdata.gateway.ipv4|ipaddr('address') }}
{% endfor %}
{% endfor %}
SCRIPT
{% endfor %}
echo -n 'Starting netplan generate ' && date
netplan generate
echo -n 'Starting netplan apply ' && date
nohup netplan apply &
4 changes: 3 additions & 1 deletion netsim/cli/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def ssh_connect(
if data.netlab_ssh_args:
c_args.extend(data.netlab_ssh_args.split(' '))

if data.ansible_ssh_pass:
if data.ansible_ssh_private_key_file:
c_args.extend(['-i', strings.eval_format(data.ansible_ssh_private_key_file,{'name': data.host})])
elif data.ansible_ssh_pass:
c_args = ['sshpass','-p',data.ansible_ssh_pass ] + c_args

if data.ansible_port:
Expand Down
5 changes: 2 additions & 3 deletions netsim/devices/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ features:
libvirt:
image: debian/bookworm64 # generic/ubuntu2004
group_vars:
netlab_linux_distro: ubuntu
netlab_linux_distro: debian
virtualbox:
image: debian/bookworm64 # generic/ubuntu2004
group_vars:
netlab_linux_distro: ubuntu
netlab_linux_distro: debian
group_vars:
ansible_network_os: linux
ansible_connection: paramiko
ansible_user: vagrant
ansible_ssh_pass: vagrant
docker_shell: sh -il
ansible_python_interpreter: auto_silent
netlab_lldp_enable: False
Expand Down
6 changes: 3 additions & 3 deletions netsim/outputs/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

forwarded_port_name = { 'ssh': 'ansible_port', }

def copy_provider_inventory(host: Box, p_data: Box) -> None:
def copy_provider_inventory(host: Box, p_data: Box, node: Box) -> None:
if 'inventory' in p_data:
for k,v in p_data.inventory.items():
host[k] = v
host[k] = strings.eval_format(v,node)

if 'inventory_port_map' in p_data and 'forwarded' in p_data:
for k,v in p_data.inventory_port_map.items():
Expand All @@ -42,7 +42,7 @@ def provider_inventory_settings(host: Box, node: Box, topology: Box) -> None:
node_provider = devices.get_provider(node,topology)
p_data = defaults.providers[node_provider]
if p_data:
copy_provider_inventory(host,p_data)
copy_provider_inventory(host,p_data,node)

if 'provider' in node: # Is the node using a secondary provider?
copy_device_provider_group_vars(host,node,topology)
Expand Down
3 changes: 3 additions & 0 deletions netsim/providers/libvirt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ attributes:
uplink: str
global:
providers:

inventory:
ansible_ssh_private_key_file: .vagrant/machines/{ name }/libvirt/private_key
1 change: 1 addition & 0 deletions netsim/templates/provider/libvirt/Vagrantfile.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
VAGRANT_COMMAND = ARGV[0]

Vagrant.configure("2") do |config|
config.ssh.insert_key = true
config.vm.provider :libvirt do |libvirt|
{% if addressing.mgmt._network|default(False) %}
libvirt.management_network_name = "{{ addressing.mgmt._network }}"
Expand Down

0 comments on commit 1c6134c

Please sign in to comment.