Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netbox ansible #97

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ansible/inventory/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
all:
children:
homelab:
hosts:
proxmox:
ansible_host: 10.0.0.180
device_type: "ThinkCentre M93P"
platform: "Proxmox"
site: "HomeLab"
role: "Hypervisor"
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved

Check failure on line 12 in ansible/inventory/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/inventory/main.yaml#L12

Trailing spaces. (trailing-spaces)
truenas:
ansible_host: 10.0.0.66
device_type: "F4-423"
platform: "TrueNAS Scale"
site: "HomeLab"
role: "NAS"

Check failure on line 19 in ansible/inventory/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/inventory/main.yaml#L19

Trailing spaces. (trailing-spaces)
apollo:
ansible_host: 10.0.0.36
device_type: "B2 S Mini PC N4020C"
platform: "Ubuntu"
site: "HomeLab"
role: "Dev Server"
27 changes: 27 additions & 0 deletions ansible/roles/netbox_ansible/tasks/device_tasks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
- name: "TASK 100: NETBOX >> ADD DEVICE TO NETBOX"
netbox.netbox.netbox_device:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
name: "{{ inventory_hostname }}"
device_type: "{{ hostvars[inventory_hostname]['device_type'] }}"
platform: "{{ hostvars[inventory_hostname]['platform'] }}"
serial: "{{ ansible_facts['net_serialnum'] | default('N/A') }}"
status: Active
device_role: "{{ hostvars[inventory_hostname]['role'] }}"
site: "{{ hostvars[inventory_hostname]['site'] }}"
custom_fields:
code_version: "{{ ansible_facts['net_version'] | default('N/A') }}"

Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
- name: "TASK 110: NETBOX >> ADD INTERFACES TO NETBOX"
netbox.netbox.netbox_device_interface:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
device: "{{ inventory_hostname }}"
name: "{{ item.key }}"
form_factor: "{{ item.key | get_interface_type }}" # Define types
mac_address: "{{ item.value.macaddress | default('00:00:00:00:00:00') }}" # Provide default MAC
state: present
with_dict: "{{ ansible_facts['net_interfaces'] | default({}) }}"
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions ansible/roles/netbox_ansible/tasks/ip_tasks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: "TASK 200: NETBOX >> Add temporary interface"
netbox.netbox.netbox_device_interface:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
device: "{{ inventory_hostname }}"
name: Temporary_Interface
form_factor: Virtual
state: present

Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
- name: "TASK 210: NETBOX >> ADD IP ADDRESS OF ANSIBLE HOST"
netbox.netbox.netbox_ip_address:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
family: 4
address: "{{ ansible_host }}/{{ subnet_mask | default(24) }}"
status: active
interface:
name: Temporary_Interface
device: "{{ inventory_hostname }}"

- name: "TASK 220: NETBOX >> ASSOCIATE IP ADDRESS TO DEVICE"
netbox.netbox.netbox_device:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
name: "{{ inventory_hostname }}"
device_type: "{{ hostvars[inventory_hostname]['device_type'] }}"
platform: "{{ hostvars[inventory_hostname]['platform'] | default('Unknown') }}"
serial: "{{ ansible_facts['net_serialnum'] | default('N/A') }}"
status: Active
primary_ip4: "{{ ansible_host }}/{{ subnet_mask | default(24) }}"
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions ansible/roles/netbox_ansible/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Populate NetBox
hosts: []
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
gather_facts: no

Check warning on line 4 in ansible/roles/netbox_ansible/tasks/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/tasks/main.yaml#L4

Truthy value should be one of [false, true] (truthy)
vars:
netbox_url: "{{ lookup('ENV', 'NETBOX_URL') }}"
netbox_token: "{{ lookup('ENV', 'NETBOX_API_KEY') }}"
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved

tasks:
- name: "Include setup tasks"
ansible_builtin.include_tasks: setup_tasks.yaml

- name: "Include device tasks"
ansible_builtin.include_tasks: device_tasks.yaml

- name: "Include IP tasks"
ansible_builtin.include_tasks: ip_tasks.yaml
44 changes: 44 additions & 0 deletions ansible/roles/netbox_ansible/tasks/setup_tasks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add pre-task validation for required variables

Add validation before the tasks to ensure all required variables are defined.

Add this before the first task:

- name: "Validate required variables"
  assert:
    that:
      - netbox_url is defined and netbox_url | length > 0
      - netbox_token is defined and netbox_token | length > 0
      - site_list is defined and site_list | length > 0
      - manufacturers is defined and manufacturers | length > 0
      - device_types is defined and device_types | length > 0
      - platforms is defined and platforms | length > 0
    fail_msg: "Missing or empty required variables for NetBox setup"

- name: "TASK 10: SETUP SITES"
netbox.netbox.netbox_site:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data: "{{ item }}"
loop: "{{ site_list }}"

Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
- name: "TASK 20: SETUP MANUFACTURERS"
netbox.netbox.netbox_manufacturer:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
name: "{{ manufacturer }}"
loop: "{{ manufacturers }}"
loop_control:
loop_var: manufacturer

Check failure on line 18 in ansible/roles/netbox_ansible/tasks/setup_tasks.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/tasks/setup_tasks.yaml#L18

Trailing spaces. (trailing-spaces)
- name: "TASK 30: SETUP DEVICE TYPES"
netbox.netbox.netbox_device_type:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
model: "{{ device_type.model }}"
manufacturer: "{{ device_type.manufacturer }}"
slug: "{{ device_type.slug }}"
part_number: "{{ device_type.part_number }}"
is_full_depth: "{{ device.type_full_depth }}"
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
loop: "{{ device_types }}"
loop_control:
loop_var: device_type
label: "{{ device_type['model'] }}"

Check failure on line 33 in ansible/roles/netbox_ansible/tasks/setup_tasks.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/tasks/setup_tasks.yaml#L33

Trailing spaces. (trailing-spaces)
- name: "TASK 40: SETUP PLATFORMS"
netbox.netbox.netbox_manufacturer:
netbox_url: "{{ netbox_url }}"
netbox_token: "{{ netbox_token }}"
data:
name: "{{ platform.name }}"
slug: "{{ platform.slug }}"
loop: "{{ platforms }}"
loop_control:
loop_var: platform
label: "{{ platform['name'] }}"
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
49 changes: 49 additions & 0 deletions ansible/roles/netbox_ansible/vars/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# Sites in HomeLab
site_list:
- name: "HomeLab"
time_zone: America/New_York
status: Active
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved

Check failure on line 7 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L7

Trailing spaces. (trailing-spaces)
manufacturers:
- Bmax
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved
- Lenovo
- Terramaster
- TP-Link

Check failure on line 13 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L13

Trailing spaces. (trailing-spaces)
device_types:

Check failure on line 14 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L14

Trailing spaces. (trailing-spaces)
- model: "B2 S Mini PC N4020C"
manufacturer: "BMax"
slug: "b2s-mini-pc"
part_number: "n4020c"
Full_depth: False

Check warning on line 19 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L19

Truthy value should be one of [false, true] (truthy)

Check failure on line 20 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L20

Trailing spaces. (trailing-spaces)
- model: "ThinkCentre M93P"
manufacturer: "Lenovo"
slug: "thinkcentre-m93p"
part_number: "m93p"
Full_depth: False

Check warning on line 25 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L25

Truthy value should be one of [false, true] (truthy)

Check failure on line 26 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L26

Trailing spaces. (trailing-spaces)
- model: "F4-423"
manufacturer: "Terramaster"
slug: "f4-423"
part_number: "f4-423"
Full_depth: False

Check warning on line 31 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L31

Truthy value should be one of [false, true] (truthy)

Check failure on line 31 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L31

Trailing spaces. (trailing-spaces)

Check failure on line 32 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L32

Trailing spaces. (trailing-spaces)
- model: "TL-SG105"
manufacturer: "TP-Link"
slug: "tl-sg105"
part_number: "tl-sg105"
Full_depth: False

Check warning on line 37 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L37

Truthy value should be one of [false, true] (truthy)

Check failure on line 38 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L38

Trailing spaces. (trailing-spaces)
platforms:
- name: "Proxmox"

Check failure on line 40 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L40

Wrong indentation: expected 3 but found 2.
slug: "proxmox"
- name: "TrueNAS Scale"
slug: "truenas-scale"
- name: "Ubuntu"
slug: "ubuntu"
- name: "Debian"
slug: "debian"
- name: "Alpine-Linux"
slug: "alpine"

Check failure on line 49 in ansible/roles/netbox_ansible/vars/main.yaml

View check run for this annotation

codefactor.io / CodeFactor

ansible/roles/netbox_ansible/vars/main.yaml#L49

No new line character at the end of file. (new-line-at-end-of-file)
Masked-Kunsiquat marked this conversation as resolved.
Show resolved Hide resolved