|
| 1 | +# Inline Many-to-Many Associations |
| 2 | + |
| 3 | ++++ 6.2.0 |
| 4 | + |
| 5 | +Many Nautobot models have many-to-many (M2M) relationships with other models. For example, a device can be associated with multiple VRFs, a location can have multiple prefixes, and a secrets group can contain multiple secrets. |
| 6 | + |
| 7 | +Previously, managing these associations required using separate standalone modules (e.g., `vrf_device_assignment`, `prefix_location`, `secrets_groups_association`). Now you can manage them inline on the parent module using the M2M field options. |
| 8 | + |
| 9 | +## Supported Parent Modules and Fields |
| 10 | + |
| 11 | +| Parent Module | M2M Field | Child Object Key | Description | |
| 12 | +|---|---|---|---| |
| 13 | +| `device` | `vrfs` | `vrf` | VRF assignments | |
| 14 | +| `device` | `clusters` | `cluster` | Cluster assignments | |
| 15 | +| `virtual_machine` | `vrfs` | `vrf` | VRF assignments | |
| 16 | +| `virtual_device_context` | `vrfs` | `vrf` | VRF assignments | |
| 17 | +| `device_interface` | `ip_addresses` | `ip_address` | IP address to interface | |
| 18 | +| `vm_interface` | `ip_addresses` | `ip_address` | IP address to VM interface | |
| 19 | +| `location` | `prefixes` | `prefix` | Prefix to location | |
| 20 | +| `location` | `vlans` | `vlan` | VLAN to location | |
| 21 | +| `cloud_network` | `prefixes` | `prefix` | Cloud network prefix assignments | |
| 22 | +| `cloud_service` | `cloud_networks` | `cloud_network` | Cloud service network assignments | |
| 23 | +| `secrets_group` | `secrets` | `secret` | Secrets group associations | |
| 24 | +| `dynamic_group` | `static_group_associations` | `associated_object_type` / `associated_object_id` | Static group memberships | |
| 25 | +| `custom_field` | `custom_field_choices` | `value` | Custom field choices | |
| 26 | +| `metadata_type` | `metadata_choices` | `value` | Metadata type choices | |
| 27 | +| `provider` | `provider_networks` | `name` | Provider networks | |
| 28 | + |
| 29 | +## M2M Field Structure |
| 30 | + |
| 31 | +All M2M fields follow the same structure: |
| 32 | + |
| 33 | +```yaml |
| 34 | +<m2m_field>: |
| 35 | + state: merge # merge (default), replace, or delete |
| 36 | + objects: |
| 37 | + - <child_key>: "value" |
| 38 | + - <child_key>: "another value" |
| 39 | +``` |
| 40 | +
|
| 41 | +### States |
| 42 | +
|
| 43 | +- **merge** (default): Adds the specified associations without removing existing ones. Safe for incremental changes. |
| 44 | +- **replace**: Enforces exactly the listed associations. Any existing associations not in the list are removed. |
| 45 | +- **delete**: Removes only the specified associations. Other existing associations are left intact. |
| 46 | +
|
| 47 | +## Basic Examples |
| 48 | +
|
| 49 | +### Adding VRFs to a Device |
| 50 | +
|
| 51 | +```yaml |
| 52 | +- name: Create a device with VRF associations |
| 53 | + networktocode.nautobot.device: |
| 54 | + url: "{{ nautobot_url }}" |
| 55 | + token: "{{ nautobot_token }}" |
| 56 | + name: "my-router" |
| 57 | + device_type: "Cisco CSR1000v" |
| 58 | + role: "Router" |
| 59 | + location: "Main Site" |
| 60 | + status: "Active" |
| 61 | + vrfs: |
| 62 | + objects: |
| 63 | + - vrf: "Management VRF" |
| 64 | + - vrf: "Production VRF" |
| 65 | + state: present |
| 66 | +``` |
| 67 | +
|
| 68 | +Since no `state` is specified on the `vrfs` field, it defaults to `merge` -- the VRFs are added without affecting any other existing VRF associations. |
| 69 | + |
| 70 | +### Adding IP Addresses to an Interface |
| 71 | + |
| 72 | +The child key accepts either a simple string or a dictionary for more specific lookups: |
| 73 | + |
| 74 | +```yaml |
| 75 | +# Simple string -- looks up the IP address by address |
| 76 | +- name: Associate IP addresses with an interface |
| 77 | + networktocode.nautobot.device_interface: |
| 78 | + url: "{{ nautobot_url }}" |
| 79 | + token: "{{ nautobot_token }}" |
| 80 | + device: "my-router" |
| 81 | + name: "GigabitEthernet0/0" |
| 82 | + ip_addresses: |
| 83 | + objects: |
| 84 | + - ip_address: "10.0.0.1/24" |
| 85 | + state: present |
| 86 | +
|
| 87 | +# Dictionary -- useful when disambiguation is needed (e.g., multiple namespaces) |
| 88 | +- name: Associate IP address with namespace specified |
| 89 | + networktocode.nautobot.device_interface: |
| 90 | + url: "{{ nautobot_url }}" |
| 91 | + token: "{{ nautobot_token }}" |
| 92 | + device: "my-router" |
| 93 | + name: "GigabitEthernet0/0" |
| 94 | + ip_addresses: |
| 95 | + objects: |
| 96 | + - ip_address: |
| 97 | + address: "10.0.0.1/24" |
| 98 | + namespace: "Production" |
| 99 | + state: present |
| 100 | +``` |
| 101 | + |
| 102 | +### Adding Secrets to a Secrets Group |
| 103 | + |
| 104 | +Some M2M associations have extra fields beyond just the child identifier. For secrets group associations, `access_type` and `secret_type` are part of the association itself: |
| 105 | + |
| 106 | +```yaml |
| 107 | +- name: Create secrets group with secret associations |
| 108 | + networktocode.nautobot.secrets_group: |
| 109 | + url: "{{ nautobot_url }}" |
| 110 | + token: "{{ nautobot_token }}" |
| 111 | + name: "Device Credentials" |
| 112 | + secrets: |
| 113 | + objects: |
| 114 | + - secret: "admin-username" |
| 115 | + access_type: "SSH" |
| 116 | + secret_type: "username" |
| 117 | + - secret: "admin-password" |
| 118 | + access_type: "SSH" |
| 119 | + secret_type: "password" |
| 120 | + state: present |
| 121 | +``` |
| 122 | + |
| 123 | +## Managing Association State |
| 124 | + |
| 125 | +### Merge (Default) |
| 126 | + |
| 127 | +Merge adds new associations without removing existing ones. Running the same task twice is idempotent. |
| 128 | + |
| 129 | +```yaml |
| 130 | +# First run: adds VRF-A |
| 131 | +- name: Add first VRF |
| 132 | + networktocode.nautobot.device: |
| 133 | + url: "{{ nautobot_url }}" |
| 134 | + token: "{{ nautobot_token }}" |
| 135 | + name: "my-router" |
| 136 | + vrfs: |
| 137 | + objects: |
| 138 | + - vrf: "VRF-A" |
| 139 | + state: present |
| 140 | +
|
| 141 | +# Second run: adds VRF-B, VRF-A is untouched |
| 142 | +- name: Add second VRF |
| 143 | + networktocode.nautobot.device: |
| 144 | + url: "{{ nautobot_url }}" |
| 145 | + token: "{{ nautobot_token }}" |
| 146 | + name: "my-router" |
| 147 | + vrfs: |
| 148 | + objects: |
| 149 | + - vrf: "VRF-B" |
| 150 | + state: present |
| 151 | +# Result: device has both VRF-A and VRF-B |
| 152 | +``` |
| 153 | + |
| 154 | +### Replace |
| 155 | + |
| 156 | +Replace enforces exactly the listed set of associations. Any existing associations not in the list are removed. |
| 157 | + |
| 158 | +```yaml |
| 159 | +# Device currently has VRF-A and VRF-B |
| 160 | +- name: Replace all VRFs with only VRF-C |
| 161 | + networktocode.nautobot.device: |
| 162 | + url: "{{ nautobot_url }}" |
| 163 | + token: "{{ nautobot_token }}" |
| 164 | + name: "my-router" |
| 165 | + vrfs: |
| 166 | + state: replace |
| 167 | + objects: |
| 168 | + - vrf: "VRF-C" |
| 169 | + state: present |
| 170 | +# Result: device has only VRF-C (VRF-A and VRF-B removed) |
| 171 | +``` |
| 172 | + |
| 173 | +### Delete |
| 174 | + |
| 175 | +Delete removes only the specified associations. Other associations are left intact. |
| 176 | + |
| 177 | +```yaml |
| 178 | +# Device currently has VRF-A and VRF-B |
| 179 | +- name: Remove only VRF-B |
| 180 | + networktocode.nautobot.device: |
| 181 | + url: "{{ nautobot_url }}" |
| 182 | + token: "{{ nautobot_token }}" |
| 183 | + name: "my-router" |
| 184 | + vrfs: |
| 185 | + state: delete |
| 186 | + objects: |
| 187 | + - vrf: "VRF-B" |
| 188 | + state: present |
| 189 | +# Result: device has only VRF-A |
| 190 | +``` |
| 191 | + |
| 192 | +## Diff Output |
| 193 | + |
| 194 | +When M2M fields change, the diff output includes the before and after state as sorted lists of child object UUIDs: |
| 195 | + |
| 196 | +```json |
| 197 | +{ |
| 198 | + "diff": { |
| 199 | + "before": { |
| 200 | + "vrfs": ["<uuid-of-vrf-a>"] |
| 201 | + }, |
| 202 | + "after": { |
| 203 | + "vrfs": ["<uuid-of-vrf-a>", "<uuid-of-vrf-b>"] |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
0 commit comments