How to Inject Host and Group Vars from Ansible into NetBox Config Templates

How to Inject Host and Group Vars from Ansible into NetBox Config Templates

Ansible and NetBox Tip:
Pass Ansible group_vars and host_vars into NetBox Config Templates by sending your Ansible vars, making them available directly within your Config Templates.

Here is an example:

# Ansible Playbook
- name: Trigger NetBox to return rendered configuration
  ansible.builtin.uri:
    url: "{{ NETBOX_ENDPOINT }}/api/dcim/devices/{{ device_id }}/render-config/"
    method: POST
    headers:
      Authorization: "Token {{ NETBOX_TOKEN }}"
      Content-Type: "application/json"
      Accept: "text/plain"
    body:
      format: txt
      local_context_data: "{{ vars }}"  # <---- Send Ansible vars to NetBox
    body_format: json
    return_content: yes
    status_code: 200
  delegate_to: localhost
  register: response
  tags:
    - build

# Jinja Template Usage (NetBox Config Template)
{% for dns_server in local_context_data.dns_servers %}
ip name-server {{ dns_server }}
{% endfor %}

Subscribe to our newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!