Skip to content

Commit

Permalink
ENH: implement dynamic selection of ams net id at runtime, with backc…
Browse files Browse the repository at this point in the history
…ompat for the vms
  • Loading branch information
ZLLentz committed Mar 16, 2024
1 parent 9519739 commit 5b50531
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 17 deletions.
5 changes: 5 additions & 0 deletions group_vars/tcbsd_plcs/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ enable_freebsd_packages: false
use_psproxy: true
use_psntp: true

# Dynamic AMS net id = set AMS net id to ip addr .1.1
# Static AMS net id = set AMS net id to the value of tc_ams_net_id
dynamic_ams: true
# tc_ams_net_id: 0.0.0.0.1.1

# set static IP on x000 (mac id 2)
x000_set_static_ip: true
x000_static_ip: 192.168.1.10
Expand Down
4 changes: 4 additions & 0 deletions group_vars/tcbsd_vms/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ enable_freebsd_packages: false
use_psproxy: false
use_psntp: false

# Dynamic AMS net id = set AMS net id to ip addr .1.1
# Static AMS net id = set AMS net id to the value of tc_ams_net_id
dynamic_ams: false

# set static IP on x000 (mac id 2)
x000_set_static_ip: false
x000_static_ip: 192.168.1.10
Expand Down
6 changes: 5 additions & 1 deletion host_vars/plc-tmo-tmp-vac/vars.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
ansible_host: plc-tmo-tmp-vac
tc_ams_net_id: 172.21.132.78.1.1

# Uncomment any setting below and change it to override a default setting.
#ansible_user: Administrator
Expand All @@ -15,6 +14,11 @@ tc_ams_net_id: 172.21.132.78.1.1
#use_psproxy: true
#use_psntp: true
#
## Dynamic AMS net id = set AMS net id to ip addr .1.1
## Static AMS net id = set AMS net id to the value of tc_ams_net_id
#dynamic_ams: true
## tc_ams_net_id: 0.0.0.0.1.1
#
## set static IP on x000 (mac id 2)
#x000_set_static_ip: true
#x000_static_ip: 192.168.1.10
Expand Down
6 changes: 5 additions & 1 deletion host_vars/plc-tst-bsd1/vars.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
ansible_host: plc-tst-bsd1
tc_ams_net_id: 172.21.148.81.1.1

# Uncomment any setting below and change it to override a default setting.
#ansible_user: Administrator
Expand All @@ -15,6 +14,11 @@ tc_ams_net_id: 172.21.148.81.1.1
#use_psproxy: true
#use_psntp: true
#
## Dynamic AMS net id = set AMS net id to ip addr .1.1
## Static AMS net id = set AMS net id to the value of tc_ams_net_id
#dynamic_ams: true
## tc_ams_net_id: 0.0.0.0.1.1
#
## set static IP on x000 (mac id 2)
#x000_set_static_ip: true
#x000_static_ip: 192.168.1.10
Expand Down
6 changes: 5 additions & 1 deletion host_vars/plc-tst-bsd2/vars.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
ansible_host: plc-tst-bsd2
tc_ams_net_id: 172.21.148.94.1.1

# Uncomment any setting below and change it to override a default setting.
#ansible_user: Administrator
Expand All @@ -15,6 +14,11 @@ tc_ams_net_id: 172.21.148.94.1.1
#use_psproxy: true
#use_psntp: true
#
## Dynamic AMS net id = set AMS net id to ip addr .1.1
## Static AMS net id = set AMS net id to the value of tc_ams_net_id
#dynamic_ams: true
## tc_ams_net_id: 0.0.0.0.1.1
#
## set static IP on x000 (mac id 2)
#x000_set_static_ip: true
#x000_static_ip: 192.168.1.10
Expand Down
2 changes: 1 addition & 1 deletion ip_macros.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{%- macro ip_to_ams_net_id(hostname) -%}
{%- set ipaddr = lookup('dig', hostname) -%}
{%- set ipaddr = lookup('community.general.dig', hostname, fail_on_error=True) -%}
{{ ipaddr }}.1.1
{%- endmacro -%}

Expand Down
36 changes: 24 additions & 12 deletions scripts/make_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import socket
from typing import Iterator
from pathlib import Path
from string import Template

from ruamel.yaml import YAML

Expand Down Expand Up @@ -65,26 +64,36 @@ def get_netid(hostname: str) -> str:
return ipaddr + ".1.1"


def tcbsd_vms_extra_vars(
hostname: str,
) -> dict[str, str]:
"""
Assign a starting ams net id for the tcbsd vms.
The IPs for these can be less stable than for CDS PLCs,
so don't dynamically update it every run.
"""
return {"tc_ams_net_id": get_netid(hostname)}


def write_host_vars(
hostname: str,
host_vars_path: str | Path,
group_vars_path: str | Path,
template_path: str | Path,
extra_vars: dict[str, str] | None = None,
) -> None:
"""Write the vars.yml file given the necessary information."""
# Load the template, sub in the values
with Path(template_path).open("r") as fd:
template = Template(fd.read())
host_vars_text = template.substitute(
PLC_IP=hostname,
PLC_NET_ID=get_netid(hostname=hostname),
)
if extra_vars is None:
extra_vars = {}
# Load the group vars, prepend with comment
with Path(group_vars_path).open("r") as fd:
group_vars_lines = ["#" + line for line in fd.read().splitlines()[1:]]
# Write the new file
with Path(host_vars_path).open("w") as fd:
fd.write(host_vars_text)
fd.write("---\n")
fd.write(f"ansible_host: {hostname}\n")
for key, value in extra_vars.items():
fd.write(f"{key}: {value}")
fd.write(
"\n"
"# Uncomment any setting below and change it "
Expand All @@ -104,15 +113,18 @@ def main(hostname: str) -> int:
inventory_path=inventory_path,
groups_path=groups_path,
)
if group == "tcbsd_vms":
extra_vars = tcbsd_vms_extra_vars(hostname)
else:
extra_vars = {}
group_vars_path = groups_path / group / "vars.yml"
template_path = repo_root / "tcbsd-plc.yaml.template"
host_vars_path = repo_root / "host_vars" / hostname / "vars.yml"
host_vars_path.parent.mkdir(exist_ok=True)
write_host_vars(
hostname=hostname,
host_vars_path=host_vars_path,
group_vars_path=group_vars_path,
template_path=template_path,
extra_vars=extra_vars,
)
print(
f"Created {host_vars_path}, "
Expand Down
7 changes: 6 additions & 1 deletion tcbsd-provision-playbook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@
community.general.xml:
path: /usr/local/etc/TwinCAT/3.1/TcRegistry.xml
xpath: /TcRegistry/Key[@Name='HKLM']/Key[@Name='Software']/Key[@Name='Beckhoff']/Key[@Name='TwinCAT3']/Key[@Name='System']/Value[@Name='AmsNetId']
value: "{% import 'ip_macros.j2' as ip_macros %}{{ ip_macros.ams_net_id_to_binary(tc_ams_net_id) }}"
value: >-
{%- import 'ip_macros.j2' as ip_macros -%}
{%- if dynamic_ams -%}
{%- set tc_ams_net_id = ip_macros.ip_to_ams_net_id(ansible_host) -%}
{%- endif -%}
{{ ip_macros.ams_net_id_to_binary(tc_ams_net_id) }}
register: ams_net_id

- name: Adjust the locked memory size
Expand Down

0 comments on commit 5b50531

Please sign in to comment.