Generating config files and configuring devices using Netbox host_vars, the config_context and Jinja2 templates #12033
Replies: 3 comments 2 replies
-
Can't really help without you providing more info. Have you configured Ansible to use Netbox as its inventory? If so, you can use "ansible-inventory" to list all the settings that it is able to read from Netbox. If that doesn't include everything you expect, then you need to show your config you are using for connecting to Netbox as inventory, and the output from ansible-inventory, and what you think is missing. Here's an example inventory config which I used a while ago (and haven't tested recently): store it as "netbox.yaml" and test using
When it's working, to make it the default (so you don't have to supply
|
Beta Was this translation helpful? Give feedback.
-
I have taken screenshots of one of my device showing the interface configs as well as the config context. Take note of the interface descriptions Config_context Here is my inventory plugin: netbox.netbox.nb_inventory
After running this command "ansible-inventory -v --list -i inventory.yml", here is my output. I can see the config context but I cant see interface description { Here is my Jinja2 Template {% for name_server in device.json.results[0].config_context['ip name-servers'] %} So after running my playbook, referencing this template, nothing is being configured.
Here is the output on the routers P2-MVO#sho run | i ntp I hope I have given enough data. I'm sure there something that I am missing or not doing right but I can't figure it out. |
Beta Was this translation helpful? Give feedback.
-
I think if you want interface data you need to set interfaces: true it's expensive to fetch and isn't gathered by default.
https://docs.ansible.com/ansible/latest/collections/netbox/netbox/nb_inventory_inventory.html#parameter-interfaces
You can always use nb_lookup to get interface data for specific hosts in a playbook task/template/set_facts
interfaces: "{{ query('netbox.netbox.nb_lookup', 'interfaces', api_filter='device=' + inventory_hostname, api_endpoint=netbox_api, token=netbox_token) | items2dict(key_name='key', value_name='value') }}"
{% for ntp_server in device.json.results[0].config_context['ntp servers'] %}
and in your template the variable you are trying to access is called config_context[0]["ntp servers"] not device.json.results, that would be a response more appropriate to the uri module and not the nb_inventory module.
{% for ntp_server in config_context[0]["ntp servers"] %}
—
Mark Tinberg ***@***.***>
Division of Information Technology-Network Services
University of Wisconsin-Madison
…________________________________
From: Innocent Mafukidze ***@***.***>
Sent: Wednesday, March 22, 2023 12:58 PM
To: netbox-community/netbox ***@***.***>
Cc: Subscribed ***@***.***>
Subject: Re: [netbox-community/netbox] Generating config files and configuring devices using Netbox host_vars, the config_context and Jinja2 templates (Discussion #12033)
I have taken screenshots of one of my device showing the interface configs as well as the config context. Take note of the interface descriptions
[image]<https://user-images.githubusercontent.com/29435761/226973184-60bc4902-4960-424d-8a8c-406e5efc4e69.png>
Config_context
[image]<https://user-images.githubusercontent.com/29435761/226987540-063a36c6-9f6f-4e8d-9bec-7d8ff8ab241d.png>
Here is my inventory
________________________________
plugin: netbox.netbox.nb_inventory
api_endpoint: http://172.27.161.192:8000
token: 0123456789abcdef0123456789abcdef01234567
validate_certs: False
config_context: True
ansible_connection: network_cli
group_by:
* device_roles
* manufacturers
* device_types
* region
* tags
After running this command "ansible-inventory -v --list -i inventory.yml", here is my output. I can see the config context but I cant see interface description
{
"_meta": {
"hostvars": {
"P2-BYO": {
"ansible_host": "172.18.2.2",
"config_context": [
{
"ip name-servers": [
"8.8.8.8",
"9.9.9.9"
],
"ntp servers": [
"192.168.4.10",
"192.168.4.11"
]
}
],
"custom_fields": {},
"device_roles": [
"mpls_core_router"
],
"device_types": [
"isr-1941"
],
"is_virtual": false,
"local_context_data": [
null
],
"locations": [],
"manufacturers": [
"cisco"
],
"platforms": [
"iosv-router"
],
"primary_ip4": "172.18.2.2",
"regions": [
"bulawayo"
],
"serial": "",
"services": [],
"site_groups": [],
"sites": [
"byo-econet-house"
],
"status": {
"label": "Active",
"value": "active"
},
"tags": []
},
"P2-MVO": {
"ansible_host": "172.18.5.5",
"config_context": [
{
"ip name-servers": [
"8.8.8.8",
"9.9.9.9"
],
"ntp servers": [
"192.168.4.10",
"192.168.4.11"
]
}
],
"custom_fields": {},
"device_roles": [
"mpls_core_router"
],
"device_types": [
"isr-1941"
],
"is_virtual": false,
"local_context_data": [
null
],
"locations": [],
"manufacturers": [
"cisco"
],
"platforms": [
"iosv-router"
],
"primary_ip4": "172.18.5.5",
"regions": [
"masvingo"
],
"serial": "",
"services": [],
"site_groups": [],
"sites": [
"mvo-bsc"
],
"status": {
"label": "Active",
"value": "active"
},
"tags": []
},
"P3-HRE": {
"ansible_host": "172.18.3.3",
"config_context": [
{
"ip name-servers": [
"8.8.8.8",
"9.9.9.9"
],
"ntp servers": [
"192.168.4.10",
"192.168.4.11"
]
}
],
"custom_fields": {},
"device_roles": [
"mpls_core_router"
],
"device_types": [
"isr-1941"
],
"is_virtual": false,
"local_context_data": [
null
],
"locations": [],
"manufacturers": [
"cisco"
],
"platforms": [
"iosv-router"
],
"primary_ip4": "172.18.3.3",
"regions": [
"harare"
],
"serial": "",
"services": [],
"site_groups": [],
"sites": [
"willowvale"
],
"status": {
"label": "Active",
"value": "active"
},
"tags": []
},
"P4-HRE": {
"ansible_host": "172.18.4.4",
"config_context": [
{
"ip name-servers": [
"8.8.8.8",
"9.9.9.9"
],
"ntp servers": [
"192.168.4.10",
"192.168.4.11"
]
}
],
"custom_fields": {},
"device_roles": [
"mpls_core_router"
],
"device_types": [
"isr-1941"
],
"is_virtual": false,
"local_context_data": [
null
],
"locations": [],
"manufacturers": [
"cisco"
],
"platforms": [
"iosv-router"
],
"primary_ip4": "172.18.4.4",
"regions": [
"harare"
],
"serial": "",
"services": [],
"site_groups": [],
"sites": [
"pockets-hill"
],
"status": {
"label": "Active",
"value": "active"
},
"tags": []
}
}
},
"all": {
"children": [
"device_roles_mpls_core_router",
"device_types_isr-1941",
"manufacturers_cisco",
"region_bulawayo",
"region_harare",
"region_masvingo",
"region_midlands",
"ungrouped"
]
},
"device_roles_mpls_core_router": {
"hosts": [
"P2-BYO",
"P2-MVO",
"P3-HRE",
"P4-HRE"
]
},
"device_types_isr-1941": {
"hosts": [
"P2-BYO",
"P2-MVO",
"P3-HRE",
"P4-HRE"
]
},
"manufacturers_cisco": {
"hosts": [
"P2-BYO",
"P2-MVO",
"P3-HRE",
"P4-HRE"
]
},
"region_bulawayo": {
"children": [
"sites_byo-econet-house"
]
},
"region_harare": {
"children": [
"sites_pockets-hill",
"sites_willowvale"
]
},
"region_masvingo": {
"children": [
"sites_mvo-bsc"
]
},
"region_midlands": {
"children": [
"sites_econet-house-gweru"
]
},
"sites_byo-econet-house": {
"hosts": [
"P2-BYO"
]
},
"sites_mvo-bsc": {
"hosts": [
"P2-MVO"
]
},
"sites_pockets-hill": {
"hosts": [
"P4-HRE"
]
},
"sites_willowvale": {
"hosts": [
"P3-HRE"
]
}
}
Here is my Jinja2 Template
{% for name_server in device.json.results[0].config_context['ip name-servers'] %}
ip name-server {{ name_server }}
{% endfor %}
{% for intf in interfaces.json.results %}
interface {{ intf.name }}
{% if intf.description %}
description {{ intf.description }}
{% endif %}
{% endfor %}
{% for ntp_server in device.json.results[0].config_context['ntp servers'] %}
ntp server {{ ntp_server }}
{% endfor %}
!
end
So after running my playbook, referencing this template, nothing is being configured.
* name: 8 - Configure Devices {{ inventory_hostname }}.
ios_config:
config: "templates/iosv-router.j2"
save_when: modified
Here is the output on the routers
P2-MVO#sho run | i ntp
P2-MVO#sho run | i name-server
P2-MVO#sho int description
Interface Status Protocol Description
Gi0/0 up up
Gi0/1 admin down down
Gi0/2 up up
Gi0/3 admin down down
Lo0 up up
P2-MVO#
I hope I have given enough data. I'm sure there something that I am missing or not doing right but I can't figure it out.
—
Reply to this email directly, view it on GitHub<#12033 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAS7UM7LBLPMJQDVZPTZXELW5M4VHANCNFSM6AAAAAAWDVDU2A>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello guys.
I have watched some YouTube videos where someone generated configs files for devices using Netbox host_vars, the config_context and Jinja2 templates. I am trying the same but I seem to be hitting a brick wall. When I run my Ansible playbook, only the static configs in my Jinja2 templates are working and nothing from the host vars e.g interface descriptions from Netbox and ntp-server configs from the config context are configured
Could someone help on how I can leverage on Netbox group/host_vars and the config_context to generate/configure devices.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions