Skip to content

Commit 3a86537

Browse files
committed
Fix: 'adjust_inventory_host' should generate template variables
The 'adjust_inventory_host' is used to add group vars & co to node data. However, it's also used to prep the node data for config template generation, and in that case we need variables that are usually computed in Ansible playbooks. The new 'template_vars' parameter triggers generation of those variables (inventory_hostname, netlab_interfaces...)
1 parent 9c90f5b commit 3a86537

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

netsim/cli/initial/templates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def template_node_data(n_data: Box, topology: Box) -> dict:
3636
node_data = adjust_inventory_host( # Add group variables to node data
3737
node=n_data,
3838
defaults=topology.defaults,
39-
group_vars=True).to_dict()
39+
group_vars=True,
40+
template_vars=True).to_dict()
4041
shared_data = template_shared_data(topology)
4142
for k,v in shared_data.items(): # ...copy shared data
4243
node_data[k] = v

netsim/outputs/common.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ def adjust_inventory_host(
5555
defaults: Box,
5656
translate: typing.Optional[dict] = None,
5757
ignore: typing.Optional[list] = None,
58-
group_vars: typing.Optional[bool] = False) -> Box:
58+
group_vars: typing.Optional[bool] = False,
59+
template_vars: typing.Optional[bool] = False) -> Box:
5960
host = get_empty_box()
6061

62+
n_provider = devices.get_provider(node,defaults)
6163
if group_vars: # Add group variables before doing netlab-to-ansible
6264
g_vars = add_group_vars(host,node,defaults) # ... attribute conversion because we need 'ansible_connection'
6365
else:
@@ -66,14 +68,20 @@ def adjust_inventory_host(
6668
# different ansible_connection
6769
#
6870
g_vars = devices.get_device_attribute(node,'group_vars',defaults)
69-
n_provider = devices.get_provider(node,defaults)
7071
if n_provider != defaults.provider:
7172
add_device_provider_group_vars(host,node,defaults)
7273

7374
translate = translate or topo_to_host
7475
if ignore is None:
7576
ignore = topo_to_host_skip
7677

78+
if template_vars:
79+
node.inventory_hostname = node.name
80+
node.netlab_device_type = node.get('netlab_device_type',node.get('ansible_network_os','none'))
81+
node.node_provider = n_provider
82+
node.netlab_interfaces = ([ node.get('loopback')] if 'loopback' in node else []) + \
83+
node.get('interfaces',[])
84+
7785
# For Docker nodes, do not use mgmt.X attributes (to set ansible_host)
7886
# Everywhere else, 'mgmt.ipv4' will overwrite 'hostname' (set by clab)
7987
# resulting in an IPv4 address, not a hostname, for nodes using SSH/HTTPx

0 commit comments

Comments
 (0)