Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .config/ansible-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ skip_list:

exclude_paths:
- docs/dev
- .ansible
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: tfindley
name: netbox

# The version of the collection. Must be compatible with semantic versioning
version: 1.0.2
version: 1.1.1

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
2 changes: 1 addition & 1 deletion roles/buildvm/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
label: "{{ vm.name }}"

- name: "BuildVM - Loop through VM task"
ansible.builtin.include_tasks: buildvm_new_vm.yml
ansible.builtin.include_tasks: new_vm.yml
when:
- buildvm_virtual_machines is defined
loop: "{{ buildvm_virtual_machines }}"
Expand Down
2 changes: 1 addition & 1 deletion roles/buildvm/tasks/new_vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
state: "{{ disk.state | default('present') }}"

- name: "BuildVM.New-VM - Loop through VM task"
ansible.builtin.include_tasks: buildvm_new_vm_interface.yml
ansible.builtin.include_tasks: new_vm_interface.yml
when:
- vm.interfaces is defined
loop: "{{ vm.interfaces }}"
Expand Down
2 changes: 1 addition & 1 deletion roles/buildvm/tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
hosts: localhost
remote_user: root
roles:
- buildvm
- tfindley.netbox.buildvm
4 changes: 2 additions & 2 deletions roles/hostsync/defaults/main/device.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ hostsync_device_features:
# hostsync_device_nvidiagpu: true # unused

hostsync_device_custom_fields:
ssh_host_key_ecdsa: "{{ ansible_ssh_host_key_ecdsa_public }}"
ssh_host_key_ed25519: "{{ ansible_ssh_host_key_ed25519_public }}"
ssh_host_key_ecdsa: "{{ ansible_facts['ssh_host_key_ecdsa_public'] }}"
ssh_host_key_ed25519: "{{ ansible_facts['ssh_host_key_ed25519_public'] }}"
# ssh_host_key_rsa_public: "{{ ansible_ssh_host_key_rsa_public }}"
# os_version: "{{ hostsync_os_selection_slug }}"
# gpu_count: "{{ nvgpu_count | default(omit) | int }}"
Expand Down
4 changes: 2 additions & 2 deletions roles/hostsync/defaults/main/hostsync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ hostsync_hostname: "{{ inventory_hostname }}"

# OS Selection logic
# This is here so it can be overwritten on a per-host basis if needed.
hostsync_os_selection_slug: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}{% if ansible_distribution_major_version is defined %}_{{ ansible_distribution_major_version }}{% if ansible_distribution_minor_version is defined %}_{{ ansible_distribution_minor_version }}{% endif %}{% endif %}" # noqa: yaml[line-length]
hostsync_os_selection_slug: "{{ ansible_facts['system'] | lower }}_{{ ansible_facts['distribution'] | lower }}{% if ansible_facts['distribution_major_version'] is defined %}_{{ ansible_facts['distribution_major_version'] }}{% if ansible_facts['distribution_minor_version'] is defined %}_{{ ansible_facts['distribution_minor_version'] }}{% endif %}{% endif %}" # noqa: yaml[line-length]


# ------ Features
Expand Down Expand Up @@ -33,4 +33,4 @@ hostsync_netbox_version: "{{ netbox_version | default(4) }}"
hostsync_add_tags:
- 'ansible_inventoried'
# - "{{ hostsync_os_tag }}"
hostsync_primary_ipv4: "{{ ansible_default_ipv4.address }}/{{ _hostsync_primary_ipv4_cidr_logic | ansible.utils.ipaddr('prefix') }}"
hostsync_primary_ipv4: "{{ ansible_facts['default_ipv4']['address'] }}/{{ _hostsync_primary_ipv4_cidr_logic | ansible.utils.ipaddr('prefix') }}"
4 changes: 2 additions & 2 deletions roles/hostsync/defaults/main/virtual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# hostsync_vm_features:
hostsync_vm_status: active
hostsync_vm_custom_fields_mapping:
ssh_host_key_ecdsa: "{{ ansible_ssh_host_key_ecdsa_public }}"
ssh_host_key_ed25519: "{{ ansible_ssh_host_key_ed25519_public }}"
ssh_host_key_ecdsa: "{{ ansible_facts['ssh_host_key_ecdsa_public'] }}"
ssh_host_key_ed25519: "{{ ansible_facts['ssh_host_key_ed25519_public'] }}"
# ssh_host_key_rsa_public: "{{ ansible_ssh_host_key_rsa_public }}"
# os_version: "{{ hostsync_os_selection_slug }}"
product_uuid: "{{ _hostsync_host_identity_product_uuid | default(omit) }}"
Expand Down
16 changes: 11 additions & 5 deletions roles/hostsync/tasks/device/device_write.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@

- name: "Hostsync.Device.Write - Debug device_type variable" # noqa: yaml[line-length]
ansible.builtin.debug:
msg: |
"device_type: {{ hostsync_mb_lookup.manufacturer | default('unset') }}_{{ hostsync_model_output.stdout | default('unset') | lower | replace(' ', '_') }}"
msg: >-
device_type:
{{
hostsync_mb_lookup.manufacturer | default('unset')
}}_
{{
hostsync_model_output.stdout | default('unset') | lower | replace(' ', '_')
}}

- name: Debug Type vars 1
ansible.builtin.debug:
Expand Down Expand Up @@ -167,11 +173,11 @@
- "device_type: {{ hostsync_device_override['type'] | default(hostsync_device_logic['type']) | default('unset') }}"
- "serial: {{ hostsync_device_override['serial'] | default(hostsync_serial_output.stdout) | default('unset') }}"
- "custom_fields: {{ hostsync_device_custom_fields }}"
- "tags: {{ (netbox_ci_info.device.tags | default([])) + ((hostsync_add_tags) | default([])) }}"
- "tags: {{ (_hostsync_netbox_ci_info.device.tags | default([])) + ((hostsync_add_tags) | default([])) }}"
- ''
- "Original Data"
- "------------------"
- "{{ netbox_ci_info.device }}"
- "{{ _hostsync_netbox_ci_info.device }}"

- name: "Hostsync.Device.Write - Write CI information"
delegate_to: localhost
Expand All @@ -196,7 +202,7 @@
| default(hostsync_serial_output.stdout)
| default(omit) }}"
custom_fields: "{{ hostsync_device_custom_fields }}"
tags: "{{ (netbox_ci_info.device.tags
tags: "{{ (_hostsync_netbox_ci_info.device.tags
| default([])) + ((hostsync_add_tags)
| default([])) }}"
state: present
110 changes: 64 additions & 46 deletions roles/hostsync/tasks/device/interface.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
---
# - name: Build interface facts map
# delegate_to: localhost
# loop: "{{ hostvars[hostsync_hostname].ansible_facts.interfaces | difference(['lo']) }}"
# loop_control:
# label: "{{ diface }}"
# loop_var: diface
# ansible.builtin.set_fact:
# _hostsync_diface_facts: >-
# {{
# (_hostsync_diface_facts | default({}))
# | combine({
# diface: (
# hostvars[hostsync_hostname]['ansible_' + (diface | replace('-', '_'))]
# | default({}, true)
# )
# })
# }}

- name: Build interface facts map
delegate_to: localhost
loop: "{{ hostvars[hostsync_hostname].ansible_facts.interfaces | difference(['lo']) }}"
loop: "{{ hostvars[hostsync_hostname]['ansible_facts']['interfaces'] | difference(['lo']) }}"
loop_control:
label: "{{ diface }}"
loop_var: diface
ansible.builtin.set_fact:
diface_facts: >-
_hostsync_diface_facts: >-
{{
(diface_facts | default({}))
(_hostsync_diface_facts | default({}))
| combine({
diface: (
hostvars[hostsync_hostname]['ansible_' + (diface | replace('-', '_'))]
hostvars[hostsync_hostname]['ansible_facts'][(diface | replace('-', '_'))]
| default({}, true)
)
})
Expand All @@ -22,29 +40,29 @@
when:
- ansible_check_mode
ansible.builtin.debug:
var: diface_facts
var: _hostsync_diface_facts

# Classify
- name: Derive ether and bridge iface lists from diface_facts
- name: Derive ether and bridge iface lists from _hostsync_diface_facts
delegate_to: localhost
ansible.builtin.set_fact:
_device_bonds: >-
_hostsync_device_bonds: >-
{{
diface_facts | dict2items
_hostsync_diface_facts | dict2items
| selectattr('value.type','in',['bond','bonding','team'])
| rejectattr('key','match', hostsync_interface_skip_regex)
| map(attribute='key') | list
}}
_device_phys: >-
_hostsync_device_phys: >-
{{
diface_facts | dict2items
_hostsync_diface_facts | dict2items
| selectattr('value.type','equalto','ether')
| rejectattr('key','match', hostsync_interface_skip_regex)
| map(attribute='key') | list
}}
_device_bridges: >-
_hostsync_device_bridges: >-
{{
diface_facts | dict2items
_hostsync_diface_facts | dict2items
| selectattr('value.type','equalto','bridge')
| rejectattr('key','match', hostsync_interface_skip_regex)
| map(attribute='key') | list
Expand All @@ -55,25 +73,25 @@
when:
- ansible_check_mode
ansible.builtin.debug:
var: _device_bonds
var: _hostsync_device_bonds

- name: Debug Physical
delegate_to: localhost
when:
- ansible_check_mode
ansible.builtin.debug:
var: _device_phys
var: _hostsync_device_phys

- name: Debug Bridges
delegate_to: localhost
when:
- ansible_check_mode
ansible.builtin.debug:
var: _device_bridges
var: _hostsync_device_bridges

- name: Ensure port-kind map exists (no phys NICs)
when:
- _device_phys | length == 0
- _hostsync_device_phys | length == 0
ansible.builtin.set_fact:
_hostsync_port_kind: {}

Expand All @@ -84,7 +102,7 @@
vars:
_q: ('device=' ~ hostsync_hostname)
ansible.builtin.set_fact:
_nb_existing_ifaces: >-
_hostsync_nb_existing_ifaces: >-
{{
query('netbox.netbox.nb_lookup', 'interfaces',
api_endpoint=hostsync_netbox_api,
Expand All @@ -104,15 +122,15 @@
- name: Build set of existing interface names
delegate_to: localhost
ansible.builtin.set_fact:
_nb_existing_iface_names: "{{ _nb_existing_ifaces | map(attribute='value.name') | list }}"
_hostsync_nb_existing_iface_names: "{{ _hostsync_nb_existing_ifaces | map(attribute='value.name') | list }}"

# - - - - - -

- name: Create MAC Address for device bonds
when:
- hostsync_netbox_version is version(4.2, '>=')
delegate_to: localhost
loop: "{{ _device_bonds }}"
loop: "{{ _hostsync_device_bonds }}"
loop_control:
label: "{{ diface_bond }}"
loop_var: diface_bond
Expand All @@ -123,16 +141,16 @@
data:
mac_address: >-
{{
diface_facts[diface_bond].perm_macaddress
| default(diface_facts[diface_bond].macaddress)
_hostsync_diface_facts[diface_bond].perm_macaddress
| default(_hostsync_diface_facts[diface_bond].macaddress)
| default(omit)
}}
tags: "{{ hostsync_add_tags }}"
state: present

- name: Create bond masters type
delegate_to: localhost
loop: "{{ _device_bonds }}"
loop: "{{ _hostsync_device_bonds }}"
loop_control:
label: "{{ diface_bond }}"
loop_var: diface_bond
Expand All @@ -144,29 +162,29 @@
device: "{{ hostsync_hostname }}"
name: "{{ diface_bond }}"
type: lag
enabled: "{{ diface_facts[diface_bond].active | default(true) }}"
enabled: "{{ _hostsync_diface_facts[diface_bond].active | default(true) }}"
mac_address: >-
{{
diface_facts[diface_bond].perm_macaddress
| default(diface_facts[diface_bond].macaddress)
_hostsync_diface_facts[diface_bond].perm_macaddress
| default(_hostsync_diface_facts[diface_bond].macaddress)
| default(omit)
}}
primary_mac_address: >-
{{
omit if hostsync_netbox_version is version(4.2, '<') else
diface_facts[diface_bond].perm_macaddress
| default(diface_facts[diface_bond].macaddress)
_hostsync_diface_facts[diface_bond].perm_macaddress
| default(_hostsync_diface_facts[diface_bond].macaddress)
| default(omit)
}}
mtu: "{{ diface_facts[diface_bond].mtu | default(omit) }}"
mtu: "{{ _hostsync_diface_facts[diface_bond].mtu | default(omit) }}"
tags: "{{ hostsync_add_tags }}"
state: present

- name: Create MAC Address for physical NICs
when:
- hostsync_netbox_version is version(4.2, '>=')
delegate_to: localhost
loop: "{{ _device_phys }}"
loop: "{{ _hostsync_device_phys }}"
loop_control:
label: "{{ diface_phys }}"
loop_var: diface_phys
Expand All @@ -177,32 +195,32 @@
data:
mac_address: >-
{{
diface_facts[diface_phys].perm_macaddress
| default(diface_facts[diface_phys].macaddress)
_hostsync_diface_facts[diface_phys].perm_macaddress
| default(_hostsync_diface_facts[diface_phys].macaddress)
| default(omit)
}}
tags: "{{ hostsync_add_tags }}"
state: present

- name: Create physical NICs
delegate_to: localhost
loop: "{{ _device_phys }}"
loop: "{{ _hostsync_device_phys }}"
loop_control:
label: "{{ diface_phys }} :: {{ _hostsync_port_kind[diface_phys] | default('tp') }} :: {{ diface_facts[diface_phys].speed | default('unk') }}"
label: "{{ diface_phys }} :: {{ _hostsync_port_kind[diface_phys] | default('tp') }} :: {{ _hostsync_diface_facts[diface_phys].speed | default('unk') }}"
loop_var: diface_phys
vars:
_port_kind: "{{ _hostsync_port_kind[diface_phys] | default('tp') }}"
_speed_key: "{{ (diface_facts[diface_phys].speed | default(-1)) | int | string }}"
_speed_key: "{{ (_hostsync_diface_facts[diface_phys].speed | default(-1)) | int | string }}"
_lag_master: >-
{{
(_device_bonds | select('in',
(diface_facts | dict2items
(_hostsync_device_bonds | select('in',
(_hostsync_diface_facts | dict2items
| selectattr('value.slaves','defined')
| selectattr('value.slaves','contains', diface_phys)
| map(attribute='key') | list)
) | list | first) | default(omit)
}}
_type_on_create: "{{ 'other' if (diface_phys not in _nb_existing_iface_names) else omit }}"
_type_on_create: "{{ 'other' if (diface_phys not in _hostsync_nb_existing_iface_names) else omit }}"
netbox.netbox.netbox_device_interface:
netbox_url: "{{ hostsync_netbox_api }}"
netbox_token: "{{ hostsync_netbox_api_key }}"
Expand All @@ -211,28 +229,28 @@
device: "{{ hostsync_hostname }}"
name: "{{ diface_phys }}"
type: "{{ _type_on_create }}"
enabled: "{{ diface_facts[diface_phys].active | default(true) }}"
enabled: "{{ _hostsync_diface_facts[diface_phys].active | default(true) }}"
mac_address: >-
{{
diface_facts[diface_phys].perm_macaddress
| default(diface_facts[diface_phys].macaddress)
_hostsync_diface_facts[diface_phys].perm_macaddress
| default(_hostsync_diface_facts[diface_phys].macaddress)
| default(omit)
}}
primary_mac_address: >-
{{
omit if hostsync_netbox_version is version(4.2, '<') else
diface_facts[diface_phys].perm_macaddress
| default(diface_facts[diface_phys].macaddress)
_hostsync_diface_facts[diface_phys].perm_macaddress
| default(_hostsync_diface_facts[diface_phys].macaddress)
| default(omit)
}}
mtu: "{{ diface_facts[diface_phys].mtu | default(omit) }}"
mtu: "{{ _hostsync_diface_facts[diface_phys].mtu | default(omit) }}"
lag: "{{ _lag_master }}"
tags: "{{ hostsync_add_tags }}"
state: present

- name: Create bridge interfaces
delegate_to: localhost
loop: "{{ _device_bridges }}"
loop: "{{ _hostsync_device_bridges }}"
loop_control:
label: "{{ diface_brdg }}"
loop_var: diface_brdg
Expand All @@ -244,7 +262,7 @@
device: "{{ hostsync_hostname }}"
name: "{{ diface_brdg }}"
type: bridge
enabled: "{{ diface_facts[diface_brdg].active | default(true) }}"
mtu: "{{ diface_facts[diface_brdg].mtu | default(omit) }}"
enabled: "{{ _hostsync_diface_facts[diface_brdg].active | default(true) }}"
mtu: "{{ _hostsync_diface_facts[diface_brdg].mtu | default(omit) }}"
tags: "{{ hostsync_add_tags }}"
state: present
Loading