Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render SONiC config_db.json directly without using sonic-cfggen #211

Merged
merged 18 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions partition/roles/sonic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ It depends on the `switch_facts` module from `ansible-common`, so make sure modu
| sonic_mgmtif_gateway | | If using a fixed management IP, the default gateway for the management interface. |
| sonic_ip_masquerade | | Enable ip masquerading on eth0. |
| sonic_breakouts | | The breakout configuration for ports, e.g. `dict('Ethernet0'='4x25G')` |
| sonic_ports | | Special configuration for ports (mtu, fec, have highest precedence) |
| sonic_config_action | | Either `load` or `reload`. In the latter case all services will be restarted. If not given, defaults to `load` |
| sonic_ports | | Configuration for ports (mtu, fec, have highest precedence). These ports will be up by default. |
| sonic_ports.name | | The port name. |
| sonic_ports.speed | | Speed of the port. |
| sonic_ports.mtu | | MTU of the port. |
Expand All @@ -42,7 +43,7 @@ It depends on the `switch_facts` module from `ansible-common`, so make sure modu
| sonic_vlans.ip | | The IP of the SVI of this VLAN. |
| sonic_vlans.dhcp_servers | | DHCP servers to relay to. |
| sonic_vlans.untagged_ports | | Array of untagged ports to bind to this VLAN. |
| sonic_vlans.tagged_ports | | Array of tagged ports to bind to this VLAN. |
| sonic_vlans.tagged_ports | | Array of tagged ports to bind to this VLAN. |
| sonic_vlans.vrf | | The VRF to bind the VLANs SVI to. |
| sonic_vteps | | VTEPs to configure. If defined FRR will automatically advertise all VNIs. |
| sonic_vteps.comment | | Description for the VTEP. |
Expand Down
10 changes: 4 additions & 6 deletions partition/roles/sonic/defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ sonic_ntpservers: []
sonic_nameservers: []
sonic_ip_masquerade: false
sonic_timezone: Europe/Berlin
sonic_config_action: load

## Physical settings
sonic_breakouts: {}
sonic_ports: []
#- name: Ethernet50
# speed: 10000
sonic_ports_default_speed: 10000
sonic_ports_default_mtu: 9216
sonic_ports_default_fec: none
sonic_ports_dict: {}
sonic_ports_default_fec: none

## BGP related settings
sonic_loopback_address:
Expand Down Expand Up @@ -44,4 +42,4 @@ sonic_frr_static_routes_mgmt: []
sonic_interconnects: {}

sonic_interconnects_default_peer_group: EXTERNAL
sonic_interconnects_default_bgp_timers: '1 3'
sonic_interconnects_default_bgp_timers: '1 3'
22 changes: 9 additions & 13 deletions partition/roles/sonic/handlers/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@
systemd:
daemon_reload: yes

- name: write metal conf to db
ansible.builtin.command: sonic-cfggen --yaml /etc/sonic/metal.yaml --write-to-db
notify: config save

- name: config save
ansible.builtin.command: config save -y

- name: restart bgp
systemd:
name: bgp
state: restarted

- name: restart caclmgrd
systemd:
name: caclmgrd
state: restarted

- name: config load
ansible.builtin.command: config load --yes

- name: config reload
ansible.builtin.command: config reload -y
ansible.builtin.command: config reload --yes
async: 120
poll: 0
notify: wait for new connection
Expand All @@ -32,3 +23,8 @@
sleep: 5
delay: 30
timeout: 300

- name: restart bgp
systemd:
name: bgp
state: restarted
63 changes: 56 additions & 7 deletions partition/roles/sonic/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@
- sonic_mgmt_vrf is defined
- sonic_loopback_address is defined
- sonic_asn is defined
- sonic_config_action in ['load', 'reload']
- sonic_ntpservers is defined
- sonic_nameservers is defined
- metal_stack_switch_os_is_sonic

- name: Check mandatory variables on non-empty sonic_ports are set
assert:
fail_msg: "default port configuration is necessary on non-empty sonic_ports"
quiet: yes
that:
- sonic_ports_default_speed
- sonic_ports_default_mtu
when: sonic_ports

- name: Populate sonic_ports_dict
set_fact:
sonic_ports_dict: "{{ sonic_ports_dict|default({}) | combine( {item.name: item} ) }}"
loop: "{{ sonic_ports }}"

- name: render resolv.conf
template:
src: resolv.conf.j2
Expand Down Expand Up @@ -53,13 +68,47 @@
with_dict: "{{ sonic_breakouts }}"
when: sonic_breakouts is defined

- name: Render metal config for config_db.json
template:
src: metal.yaml.j2
dest: /etc/sonic/metal.yaml
notify:
- write metal conf to db
- config reload
- name: Delete deprecated metal.yaml
ansible.builtin.file:
path: "/etc/sonic/metal.yaml"
state: absent

- name: Get running configuration
ansible.builtin.command: show runningconfiguration all
register: sonic_running_cfg_result
changed_when: false

- name: Parse running configuration
ansible.builtin.set_fact:
sonic_running_cfg: "{{ sonic_running_cfg_result.stdout | from_json }}"

- name: Extract running configuration for breakouts and ports
ansible.builtin.set_fact:
sonic_running_cfg_breakouts: "{{ sonic_running_cfg | community.general.json_query('BREAKOUT_CFG') }}"
sonic_running_cfg_hwsku: "{{ sonic_running_cfg | community.general.json_query('DEVICE_METADATA.localhost.hwsku') }}"
sonic_running_cfg_mac: "{{ sonic_running_cfg | community.general.json_query('DEVICE_METADATA.localhost.mac') }}"
sonic_running_cfg_platform: "{{ sonic_running_cfg | community.general.json_query('DEVICE_METADATA.localhost.platform') }}"
sonic_running_cfg_ports: "{{ sonic_running_cfg | community.general.json_query('PORT') }}"

- name: Fail if running configuration doesn't contain required information
ansible.builtin.assert:
that:
- sonic_running_cfg_breakouts
- sonic_running_cfg_hwsku
- sonic_running_cfg_mac
- sonic_running_cfg_platform
- sonic_running_cfg_ports
fail_msg: The running configuration is incomplete because it does not contain 'BREAKOUT_CFG', 'PORT', or complete 'DEVICE_METADATA'.

- name: Render config_db
set_fact:
config_db: "{{ lookup('template', 'metal.yaml.j2') }}"

- name: Save config_db as JSON file
copy:
content: "{{ config_db | from_yaml | to_nice_json }}"
dest: /etc/sonic/config_db.json
notify: "config {{ sonic_config_action }}"

- name: Set NTP timezone
timezone:
Expand Down
45 changes: 34 additions & 11 deletions partition/roles/sonic/templates/metal.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ DEVICE_METADATA:
localhost:
docker_routing_config_mode: split
hostname: "{{ inventory_hostname }}"
hwsku: "{{ sonic_running_cfg_hwsku }}"
mac: "{{ sonic_running_cfg_mac }}"
platform: "{{ sonic_running_cfg_platform }}"
type: "LeafRouter"
frr_mgmt_framework_config: "true"

Expand Down Expand Up @@ -35,40 +38,60 @@ MGMT_INTERFACE:
{% endif %}

{% endif %}
MGMT_PORT:
eth0:
alias: "eth0"
admin_status: "up"
description: "Management Port"

MGMT_VRF_CONFIG:
vrf_global:
mgmtVrfEnabled: "{{ sonic_mgmt_vrf | lower }}"
{% if sonic_ports|length > 0 %}
{% if sonic_ports_dict|length > 0 %}

INTERFACE:
{% for port in sonic_ports %}
{% if port.name in sonic_bgp_ports|default([]) or port.vrf is defined %}
{{ port.name }}:
{% if port.name in sonic_bgp_ports|default([]) %}
{% for name, port in sonic_ports_dict.items() %}
{% if name in sonic_bgp_ports|default([]) or port.vrf is defined %}
{{ name }}:
{% if name in sonic_bgp_ports|default([]) %}
ipv6_use_link_local_only: enable
{% endif %}
{% if port.vrf is defined %}
vrf_name: "{{ port.vrf }}"
{% endif %}
{% elif port.ips is defined %}
{{ port.name }}: {}
{{ name }}: {}
{% endif %}
{% for ip in port.ips|default([]) %}
{{ port.name }}|{{ ip }}: {}
{{ name }}|{{ ip }}: {}
{% endfor %}
{% endfor %}
{% endif %}
{% if sonic_ports|length > 0 %}

BREAKOUT_CFG:
{% for name, cfg in sonic_running_cfg_breakouts.items() %}
{{ name }}:
brkout_mode: "{{ cfg.brkout_mode }}"
{% endfor %}

PORT:
{% for port in sonic_ports %}
{{ port.name }}:
{% for name, running_cfg in sonic_running_cfg_ports.items() %}
{{ name }}:
alias: {{ running_cfg.alias }}
autoneg: "{{ running_cfg.autoneg|default("off")|string|lower }}"
index: "{{ running_cfg.index }}"
lanes: "{{ running_cfg.lanes }}"
parent_port: {{ running_cfg.parent_port }}
{% if sonic_ports_dict[name] is defined %}
{% set port = sonic_ports_dict[name] %}
admin_status: up
speed: "{{ port.speed|default(sonic_ports_default_speed) }}"
mtu: "{{ port.mtu|default(sonic_ports_default_mtu) }}"
fec: "{{ port.fec|default(sonic_ports_default_fec)|string|lower }}"
{% else %}
speed: "{{ running_cfg.speed }}"
{% endif %}
{% endfor %}
{% endif %}
{% if sonic_vlans is defined and sonic_vlans|length > 0 %}

VLAN:
Expand Down
79 changes: 61 additions & 18 deletions partition/roles/sonic/test/data/exit/input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,68 @@ sonic_loopback_address: 10.0.0.1
sonic_breakouts:
Ethernet0: "4x10G"

sonic_ports:
- name: Ethernet0
speed: 10000
mtu: 1500
vrf: VrfMpls
ips:
- 10.0.0.2/32
# Uplink for pxe VLAN
- name: Ethernet1
speed: 10000
- name: Ethernet2
speed: 10000
- name: Ethernet3
speed: 10000
ips:
- 10.1.2.2/30
sonic_ports_default_mtu: 9216
sonic_ports_default_speed: 100000
sonic_ports_dict:
Ethernet0:
speed: 10000
mtu: 1500
vrf: VrfMpls
ips:
- 10.0.0.2/32
# spine uplinks
- name: Ethernet112
- name: Ethernet116
Ethernet112:
Ethernet116:

sonic_running_cfg_breakouts:
Ethernet0:
brkout_mode: "4x10G"
Ethernet112:
brkout_mode: "1x100G[40G]"
Ethernet116:
brkout_mode: "1x100G[40G]"

sonic_running_cfg_hwsku: Accton-AS7726-32X
sonic_running_cfg_mac: e0:01:a6:e3:29:3c
sonic_running_cfg_platform: x86_64-accton_as7726_32x-r0

sonic_running_cfg_ports:
Ethernet0:
alias: Eth1/1(Port1)
index: "1"
lanes: "1"
parent_port: Ethernet0
speed: "10000"
Ethernet1:
alias: Eth1/2(Port1)
index: "1"
lanes: "2"
parent_port: Ethernet0
speed: "10000"
Ethernet2:
alias: Eth1/3(Port1)
index: "1"
lanes: "3"
parent_port: Ethernet0
speed: "10000"
Ethernet3:
alias: Eth1/4(Port1)
index: "1"
lanes: "4"
parent_port: Ethernet0
speed: "10000"
Ethernet112:
alias: Eth29(Port29)
index: "29"
lanes: "113,114,115,116"
parent_port: Ethernet112
speed: "100000"
Ethernet116:
alias: Eth30(Port30)
index: "30"
lanes: "117,118,119,120"
parent_port: Ethernet116
speed: "100000"

sonic_bgp_ports:
- Ethernet112
Expand Down
Loading