Using module default groups with NetBox and ansible

November 21, 2022

Many ansible collection have a long time been reliant on defining tokens and host URLs for each task. As an example with the NetBox Ansible collection:

netbox.netbox.netbox_device:
  netbox_url: https://demo.netbox.dev
  netbox_token: f4269827cde2eec3ae24b520689f4531d2a7d9b9
  data:
    name: Device One
    device_role: Firewall
    device_type: ISR 4331
    site: Main DC
  state: present

With multiple tasks in your playbook using these modules the amount of code grows steadily. It feels redundant having to specify these parameters on every task. From the Ansible collection 3.8.0 you can now use module default groups. It works like this:

module_defaults:
  group/netbox.netbox.netbox:
    netbox_url: https://demo.netbox.dev
    netbox_token: f4269827cde2eec3ae24b520689f4531d2a7d9b9

tasks:
  netbox.netbox.netbox_device:
    data:
      name: Device One
      device_role: Firewall
      device_type: ISR 4331
      site: Main DC
    state: present

The feature is available from Ansible 2.12, and should save you some time and avoid repetition in your code.

138