diff --git a/.config/ansible-lint.yml b/.config/ansible-lint.yml index 2e73d20..be05b12 100644 --- a/.config/ansible-lint.yml +++ b/.config/ansible-lint.yml @@ -3,3 +3,4 @@ skip_list: exclude_paths: - docs/dev + - .ansible diff --git a/galaxy.yml b/galaxy.yml index 09fa8ba..e9100d4 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -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 diff --git a/roles/buildvm/tasks/main.yml b/roles/buildvm/tasks/main.yml index 4834f9e..f32d5e2 100644 --- a/roles/buildvm/tasks/main.yml +++ b/roles/buildvm/tasks/main.yml @@ -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 }}" diff --git a/roles/buildvm/tasks/new_vm.yml b/roles/buildvm/tasks/new_vm.yml index 6c6745c..84341ef 100644 --- a/roles/buildvm/tasks/new_vm.yml +++ b/roles/buildvm/tasks/new_vm.yml @@ -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 }}" diff --git a/roles/buildvm/tests/test.yml b/roles/buildvm/tests/test.yml index de68fe3..28aa100 100644 --- a/roles/buildvm/tests/test.yml +++ b/roles/buildvm/tests/test.yml @@ -3,4 +3,4 @@ hosts: localhost remote_user: root roles: - - buildvm + - tfindley.netbox.buildvm diff --git a/roles/hostsync/defaults/main/device.yml b/roles/hostsync/defaults/main/device.yml index 8852731..26f1acc 100644 --- a/roles/hostsync/defaults/main/device.yml +++ b/roles/hostsync/defaults/main/device.yml @@ -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 }}" diff --git a/roles/hostsync/defaults/main/hostsync.yml b/roles/hostsync/defaults/main/hostsync.yml index 4ec3ebf..b9b70e0 100644 --- a/roles/hostsync/defaults/main/hostsync.yml +++ b/roles/hostsync/defaults/main/hostsync.yml @@ -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 @@ -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') }}" diff --git a/roles/hostsync/defaults/main/virtual.yml b/roles/hostsync/defaults/main/virtual.yml index fbc9f0a..e586e25 100644 --- a/roles/hostsync/defaults/main/virtual.yml +++ b/roles/hostsync/defaults/main/virtual.yml @@ -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) }}" diff --git a/roles/hostsync/tasks/device/device_write.yml b/roles/hostsync/tasks/device/device_write.yml index b4ee229..d1fb286 100644 --- a/roles/hostsync/tasks/device/device_write.yml +++ b/roles/hostsync/tasks/device/device_write.yml @@ -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: @@ -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 @@ -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 diff --git a/roles/hostsync/tasks/device/interface.yml b/roles/hostsync/tasks/device/interface.yml index 7b32ca9..9ef934a 100644 --- a/roles/hostsync/tasks/device/interface.yml +++ b/roles/hostsync/tasks/device/interface.yml @@ -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) ) }) @@ -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 @@ -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: {} @@ -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, @@ -104,7 +122,7 @@ - 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 }}" # - - - - - - @@ -112,7 +130,7 @@ 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 @@ -123,8 +141,8 @@ 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 }}" @@ -132,7 +150,7 @@ - name: Create bond masters type delegate_to: localhost - loop: "{{ _device_bonds }}" + loop: "{{ _hostsync_device_bonds }}" loop_control: label: "{{ diface_bond }}" loop_var: diface_bond @@ -144,21 +162,21 @@ 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 @@ -166,7 +184,7 @@ 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 @@ -177,8 +195,8 @@ 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 }}" @@ -186,23 +204,23 @@ - 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 }}" @@ -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 @@ -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 diff --git a/roles/hostsync/tasks/device/main.yml b/roles/hostsync/tasks/device/main.yml index 12e16de..67d4e19 100644 --- a/roles/hostsync/tasks/device/main.yml +++ b/roles/hostsync/tasks/device/main.yml @@ -5,7 +5,7 @@ connection: local become: false check_mode: true - register: netbox_ci_info + register: _hostsync_netbox_ci_info netbox.netbox.netbox_device: netbox_url: "{{ hostsync_netbox_api }}" netbox_token: "{{ hostsync_netbox_api_key }}" diff --git a/roles/hostsync/tasks/hostfact.yml b/roles/hostsync/tasks/hostfact.yml index 84e68df..29ea042 100644 --- a/roles/hostsync/tasks/hostfact.yml +++ b/roles/hostsync/tasks/hostfact.yml @@ -23,8 +23,8 @@ - name: "Hostsync.Hostfact - Build host_identity base fields" ansible.builtin.set_fact: _hostsync_host_identity_inventory_id: "{{ inventory_hostname }}" - _hostsync_host_identity_hostname: "{{ ansible_hostname }}" - _hostsync_host_identity_fqdn: "{{ ansible_fqdn | default(ansible_hostname) }}" + _hostsync_host_identity_hostname: "{{ ansible_facts['hostname'] }}" + _hostsync_host_identity_fqdn: "{{ ansible_facts['fqdn'] | default(ansible_facts['hostname']) }}" _hostsync_host_identity_product_uuid: "{{ (_hostsync_product_uuid_raw.content | default('')) | b64decode | trim }}" _hostsync_host_identity_machine_id: "{{ (_hostsync_machine_id_raw.content | default('')) | b64decode | trim }}" diff --git a/roles/hostsync/tasks/main.yml b/roles/hostsync/tasks/main.yml index e80bf3d..dd7e2d8 100644 --- a/roles/hostsync/tasks/main.yml +++ b/roles/hostsync/tasks/main.yml @@ -11,7 +11,7 @@ - name: "Hostsync.Main - Gather all network interfaces" ansible.builtin.set_fact: - hostsync_interfaces: "{{ ansible_interfaces | difference(hostsync_interface_exclude) | list | sort }}" + hostsync_interfaces: "{{ ansible_facts['interfaces'] | difference(hostsync_interface_exclude) | list | sort }}" - name: "Hostsync.Main - Initialize network adapters dictionary" ansible.builtin.set_fact: @@ -19,12 +19,12 @@ - name: "Hostsync.Main - Trigger Physical Device hostsync" when: - - ansible_virtualization_role == "host" + - ansible_facts['virtualization_role'] == "host" ansible.builtin.include_tasks: device/main.yml - name: "Hostsync.Main - Trigger Virtual Machine hostsync" when: - - ansible_virtualization_role == "guest" + - ansible_facts['virtualization_role'] == "guest" ansible.builtin.include_tasks: virtual/main.yml # - name: Display network adapter dictionary diff --git a/roles/hostsync/tasks/preflight.yml b/roles/hostsync/tasks/preflight.yml index 8ef0ec7..bed55c3 100644 --- a/roles/hostsync/tasks/preflight.yml +++ b/roles/hostsync/tasks/preflight.yml @@ -8,20 +8,22 @@ - hostsync_options['uuid'] is defined and hostsync_options['uuid'] | bool is boolean - name: "Hostsync.Preflight - Report options" + vars: + _hostsync_debugmsg: | + Hostsync Role options + --------------------- + CI UUID Enabled: {{ hostsync_options['uuid'] }} + Device Serial No: {{ hostsync_device_features['serial'] }} + Device Type: {{ hostsync_device_features['type'] }} ansible.builtin.debug: - msg: | - "Hostsync Role options" - "---------------------" - " CI UUID Enabled: {{ hostsync_options['uuid'] }}" - "Device Serial No: {{ hostsync_device_features['serial'] }}" - " Device Type: {{ hostsync_device_features['type'] }}" + msg: "{{ _hostsync_debugmsg.split('\n') }}" - name: "Hostsync.Preflight - Check required Netbox information has been set" delegate_to: localhost become: false ansible.builtin.assert: that: - - hostsync_netbox_api is defined and hostsync_netbox_api| length > 0 + - hostsync_netbox_api is defined and hostsync_netbox_api | length > 0 - hostsync_netbox_api_key is defined and hostsync_netbox_api_key | length > 0 - name: "Hostsync.Preflight - Check that you can connect to NetBox" diff --git a/roles/hostsync/tasks/virtual/interface.yml b/roles/hostsync/tasks/virtual/interface.yml index 26a2d08..8e582e2 100644 --- a/roles/hostsync/tasks/virtual/interface.yml +++ b/roles/hostsync/tasks/virtual/interface.yml @@ -1,45 +1,40 @@ --- -# - name: Interfaces to include (exclude lo) -# ansible.builtin.set_fact: -# _ifaces: "{{ hostvars[hostsync_hostname].ansible_facts.interfaces | difference(['lo']) }}" - -# Original names as keys - name: Build interface facts map + loop: "{{ hostvars[hostsync_hostname]['ansible_facts']['interfaces'] | difference(['lo']) }}" + loop_control: + label: "{{ viface }}" + loop_var: viface ansible.builtin.set_fact: - viface_facts: >- + _hostsync_viface_facts: >- {{ - (viface_facts | default({})) + (_hostsync_viface_facts | default({})) | combine({ viface: ( - hostvars[hostsync_hostname]['ansible_' + (viface | replace('-', '_'))] + hostvars[hostsync_hostname]['ansible_facts'][(viface | replace('-', '_'))] | default({}, true) ) }) }} - loop: "{{ hostvars[hostsync_hostname].ansible_facts.interfaces | difference(['lo']) }}" - loop_control: - label: "{{ viface }}" - loop_var: viface - name: Debug all vifaces when: - ansible_check_mode | bool ansible.builtin.debug: - var: viface_facts + var: _hostsync_viface_facts -- name: Derive ether and bridge iface lists from viface_facts +- name: Derive ether and bridge iface lists from _hostsync_viface_facts ansible.builtin.set_fact: - _ether_ifaces: >- + _hostsync_ether_ifaces: >- {{ - viface_facts | dict2items + _hostsync_viface_facts | dict2items | selectattr('value.type','equalto','ether') | map(attribute='key') | reject('match', hostsync_interface_skip_regex) | list }} - _bridge_ifaces: >- + _hostsync_bridge_ifaces: >- {{ - viface_facts | dict2items + _hostsync_viface_facts | dict2items | selectattr('value.type','equalto','bridge') | map(attribute='key') | reject('match', hostsync_interface_skip_regex) @@ -48,11 +43,11 @@ - name: Derive missing interfaces ansible.builtin.set_fact: - _other_ifaces: >- + _hostsync_other_ifaces: >- {{ - viface_facts | dict2items - | difference(_ether_ifaces) - | difference(_bridge_ifaces) + _hostsync_viface_facts | dict2items + | difference(_hostsync_ether_ifaces) + | difference(_hostsync_bridge_ifaces) | list }} @@ -60,7 +55,7 @@ when: - hostsync_netbox_version is version(4.2, '>=') delegate_to: localhost - loop: "{{ _ether_ifaces }}" + loop: "{{ _hostsync_ether_ifaces }}" loop_control: label: "{{ viface_ether }}" loop_var: viface_ether @@ -71,8 +66,8 @@ data: mac_address: >- {{ - viface_facts[viface_ether].perm_macaddress - | default(viface_facts[viface_ether].macaddress) + _hostsync_viface_facts[viface_ether].perm_macaddress + | default(_hostsync_viface_facts[viface_ether].macaddress) | default(omit) }} tags: "{{ hostsync_add_tags }}" @@ -80,7 +75,7 @@ - name: Create ether interfaces in NetBox delegate_to: localhost - loop: "{{ _ether_ifaces }}" + loop: "{{ _hostsync_ether_ifaces }}" loop_control: label: "{{ viface_ether }}" loop_var: viface_ether @@ -91,29 +86,29 @@ data: virtual_machine: "{{ hostsync_hostname }}" name: "{{ viface_ether }}" - enabled: "{{ viface_facts[viface_ether].active | default(true) }}" + enabled: "{{ _hostsync_viface_facts[viface_ether].active | default(true) }}" mac_address: >- {{ - viface_facts[viface_ether].perm_macaddress - | default(viface_facts[viface_ether].macaddress) + _hostsync_viface_facts[viface_ether].perm_macaddress + | default(_hostsync_viface_facts[viface_ether].macaddress) | default(omit) }} primary_mac_address: >- {{ omit if hostsync_netbox_version is version(4.2, '<') else - viface_facts[viface_ether].perm_macaddress - | default(viface_facts[viface_ether].macaddress) + _hostsync_viface_facts[viface_ether].perm_macaddress + | default(_hostsync_viface_facts[viface_ether].macaddress) | default(omit) }} - mtu: "{{ viface_facts[viface_ether].mtu | default(omit) }}" + mtu: "{{ _hostsync_viface_facts[viface_ether].mtu | default(omit) }}" tags: "{{ hostsync_add_tags }}" state: present - name: Create bridge interfaces delegate_to: localhost when: - - (_bridge_ifaces | length) > 0 - loop: "{{ _bridge_ifaces }}" + - (_hostsync_bridge_ifaces | length) > 0 + loop: "{{ _hostsync_bridge_ifaces }}" loop_control: label: "{{ viface_bridge }} -> {{ _parent | default('UNSET') }}" loop_var: viface_bridge @@ -126,7 +121,7 @@ data: virtual_machine: "{{ hostsync_hostname }}" name: "{{ viface_bridge }}" - enabled: "{{ viface_facts[viface_bridge].active | default(true) }}" - mtu: "{{ viface_facts[item].mtu | default(omit) }}" + enabled: "{{ _hostsync_viface_facts[viface_bridge].active | default(true) }}" + mtu: "{{ _hostsync_viface_facts[item].mtu | default(omit) }}" tags: "{{ hostsync_add_tags }}" state: present diff --git a/roles/hostsync/tasks/virtual/main.yml b/roles/hostsync/tasks/virtual/main.yml index 1ebecb0..19af7ac 100644 --- a/roles/hostsync/tasks/virtual/main.yml +++ b/roles/hostsync/tasks/virtual/main.yml @@ -5,7 +5,7 @@ connection: local become: false check_mode: true - register: netbox_ci_info + register: _hostsync_netbox_ci_info netbox.netbox.netbox_virtual_machine: netbox_url: "{{ hostsync_netbox_api }}" netbox_token: "{{ hostsync_netbox_api_key }}" diff --git a/roles/hostsync/tasks/virtual/vm_write.yml b/roles/hostsync/tasks/virtual/vm_write.yml index 631d7eb..06aaecc 100644 --- a/roles/hostsync/tasks/virtual/vm_write.yml +++ b/roles/hostsync/tasks/virtual/vm_write.yml @@ -15,7 +15,7 @@ status: "{{ hostsync_vm_status | default(omit) }}" custom_fields: "{{ hostsync_vm_custom_fields_mapping }}" # primary_ip4: "{{ hostsync_primary_ipv4 }}" # No sense writing this as we required it to access the host in Netbox - tags: "{{ (netbox_ci_info.virtual_machine.tags | default([])) + ((hostsync_add_tags) | default([])) }}" + tags: "{{ (_hostsync_netbox_ci_info.virtual_machine.tags | default([])) + ((hostsync_add_tags) | default([])) }}" state: present @@ -32,8 +32,8 @@ - "name: {{ hostsync_hostname }}" - "platform: {{ hostsync_os_selection_slug }}" - "custom_fields: {{ hostsync_vm_custom_fields_mapping }}" - - "tags: {{ (netbox_ci_info.virtual_machine.tags | default([])) + ((hostsync_add_tags) | default([])) }}" + - "tags: {{ (_hostsync_netbox_ci_info.virtual_machine.tags | default([])) + ((hostsync_add_tags) | default([])) }}" - '' - "Original Data" - "------------------" - - "{{ netbox_ci_info.virtual_machine }}" + - "{{ _hostsync_netbox_ci_info.virtual_machine }}" diff --git a/roles/hostsync/vars/main/device.yml b/roles/hostsync/vars/main/device.yml index 7f237be..3c8f75d 100644 --- a/roles/hostsync/vars/main/device.yml +++ b/roles/hostsync/vars/main/device.yml @@ -12,13 +12,31 @@ hostsync_device_logic: | regex_replace('_+','_') | regex_replace('^_|_$','') }} - # platform: "{{ 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] + # platform: >- + # {{ + # 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 %} + platform: >- {{ - [ ansible_system, - ansible_distribution, - (ansible_distribution_major_version | default('')), - (ansible_distribution_minor_version | default('')) + [ ansible_facts['system'], + ansible_facts['distribution'], + (ansible_facts['distribution_major_version'] | default('')), + (ansible_facts['distribution_minor_version'] | default('')) ] | map('string') | select('truthy') @@ -49,7 +67,7 @@ _hostsync_mb_logic: "default": serialno: "dmidecode -s system-serial-number" model: "dmidecode -s system-product-name" - manufacturer: "{{ ansible_board_vendor | lower | regex_replace('[^a-z0-9]+', '_') }}" + manufacturer: "{{ ansible_facts['board_vendor'] | lower | regex_replace('[^a-z0-9]+', '_') }}" # hostsync_mb_lookup: "{{ _hostsync_mb_logic[ansible_board_vendor | lower] | default(_hostsync_mb_logic['default']) }}" # hostsync_mb_lookup: >- # {{ @@ -61,7 +79,7 @@ _hostsync_mb_logic: _hostsync_vendor_key: >- {{ - ansible_board_vendor + ansible_facts['board_vendor'] | lower | regex_replace('[^a-z0-9. ]','') | trim diff --git a/roles/hostsync/vars/main/interface.yml b/roles/hostsync/vars/main/interface.yml index 7dcc7b5..d27974a 100644 --- a/roles/hostsync/vars/main/interface.yml +++ b/roles/hostsync/vars/main/interface.yml @@ -43,4 +43,4 @@ hostsync_interface_mgmt: hostsync_interface_exclude: "{{ hostsync_interface_system + hostsync_interface_mgmt }}" -_hostsync_primary_ipv4_cidr_logic: "{{ ansible_default_ipv4.network }}/{{ ansible_default_ipv4.netmask }}" +_hostsync_primary_ipv4_cidr_logic: "{{ ansible_facts['default_ipv4']['network'] }}/{{ ansible_facts['default_ipv4']['netmask'] }}" diff --git a/roles/hostsync/vars/main/os.yml b/roles/hostsync/vars/main/os.yml index b912173..834fcab 100644 --- a/roles/hostsync/vars/main/os.yml +++ b/roles/hostsync/vars/main/os.yml @@ -1,24 +1,69 @@ --- _hostsync_os_logic: - linux: "{{ 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] - windows: "{{ ansible_system | lower }}" - default: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}" + linux: >- + {{ 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_major_version'] is defined %}_ + {{ ansible_facts['distribution_major_version'] }} + {% endif %} + {% endif %} + windows: "{{ ansible_facts['system'] | lower }}" + default: "{{ ansible_facts['system'] | lower }}_{{ ansible_facts['distribution'] | lower }}" # _hostsync_os_logic: # linux: -# ubuntu: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}_{{ ansible_distribution_major_version }}_{{ ansible_distribution_minor_version }}" -# rocky: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}_{{ ansible_distribution_major_version }}" -# centos: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}_{{ ansible_distribution_major_version }}" -# debian: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}_{{ ansible_distribution_major_version }}_{{ ansible_distribution_minor_version }}" +# ubuntu: >- +# {{ ansible_system | lower }}_ +# {{ ansible_distribution | lower }}_ +# {{ ansible_distribution_major_version }}_ +# {{ ansible_distribution_minor_version }} +# rocky: >- +# {{ ansible_system | lower }}_ +# {{ ansible_distribution | lower }}_ +# {{ ansible_distribution_major_version }} +# centos: >- +# {{ ansible_system | lower }}_ +# {{ ansible_distribution | lower }}_ +# {{ ansible_distribution_major_version }} +# debian: >- +# {{ ansible_system | lower }}_ +# {{ ansible_distribution | lower }}_ +# {{ ansible_distribution_major_version }}_ +# {{ ansible_distribution_minor_version }} # windows: -# server: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}_server_{{ ansible_distribution_major_version }}" -# desktop: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}_{{ ansible_distribution_major_version }}" -# default: "{{ ansible_system | lower }}_{{ ansible_distribution | lower }}" -# hostsync_platform_lookup: "{{ _hostsync_os_logic[ansible_system | lower][ansible_distribution | lower] | default(hostsync_platform_lookup[default]) }}" +# server: >- +# {{ ansible_system | lower }}_ +# {{ ansible_distribution | lower }}_server_ +# {{ ansible_distribution_major_version }} +# desktop: >- +# {{ ansible_system | lower }}_ +# {{ ansible_distribution | lower }}_ +# {{ ansible_distribution_major_version }} +# default: >- +# {{ansible_system | lower }}_ +# {{ ansible_distribution | lower }} +# hostsync_platform_lookup: >- +# {{ +# _hostsync_os_logic +# [ansible_system | lower] +# [ansible_distribution | lower] | +# default(hostsync_platform_lookup[default]) +# }} # ------ -# _hostsync_platform_lookup_linux: "{{ ansible_system }}_{{ ansible_distribution | lower }}{{ if ansible_distribution_major_version '_' + ansible_distribution_major_version }}{{ if ansible_distribution_minor_version '_' + ansible_distribution_minor_version }}" +# _hostsync_platform_lookup_linux: >- +# {{ ansible_system }}_ +# {{ ansible_distribution | lower }} +# {{ if ansible_distribution_major_version '_' + ansible_distribution_major_version }} +# {{ if ansible_distribution_minor_version '_' + ansible_distribution_minor_version }} # _hostsync_platform_lookup_windows: -# hostsync_platform_lookup: "{{ if 'linux' in ansible_system | lower _hostsync_platform_lookup_linux else if 'windows' in ansible_system | lower _hostsync_platform_lookup_windows }}" +# hostsync_platform_lookup: >- +# {{ +# if 'linux' in ansible_system | +# lower _hostsync_platform_lookup_linux else if 'windows' in ansible_system | +# lower _hostsync_platform_lookup_windows +# }} diff --git a/roles/hostsync/vars/main/virtual.yml b/roles/hostsync/vars/main/virtual.yml index 4b1dae7..5e10436 100644 --- a/roles/hostsync/vars/main/virtual.yml +++ b/roles/hostsync/vars/main/virtual.yml @@ -1,3 +1,11 @@ --- hostsync_vm_logic: - platform: "{{ 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] + platform: >- + {{ 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 %}