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

Rename devices and other entities #215

Closed
monkz opened this issue Jun 2, 2020 · 10 comments · Fixed by #222
Closed

Rename devices and other entities #215

monkz opened this issue Jun 2, 2020 · 10 comments · Fixed by #222

Comments

@monkz
Copy link

monkz commented Jun 2, 2020

ISSUE TYPE
  • Feature Idea
SOFTWARE VERSIONS
Ansible:

ansible 2.9.9

Netbox:

(v2.8.4)

Collection:

0.2.1

SUMMARY

It is not possible to rename a device.
This collection uses the name of an entity as identification. So on every rename (aka. destroy and create) the device loses the changelog.

I would like to identify the device by asset tag (which is also unique) or another unique attribute to rename a device and keep the changelog.

@FragmentedPacket
Copy link
Contributor

Let me think about how we can accomplish this. Do you find yourself needing to change device names, etc. often?

I'm wondering if we can add a "new_name" sort of thing to modules to act upon to find the existing device/item and then update the name.

What other endpoints do you find yourself doing this on?

@monkz
Copy link
Author

monkz commented Jun 5, 2020

We recycle bare metal devices very often.

  • New tenant - new name.
  • Repairs done - new name as it gets a new purpose.
  • Reinstall - new name.

I don't see other endpoints so far. Maybe Inventory Items. As HDDs may be mounted with another purpose.
But that is just our case. I think there may be other cases, where other things might be renamed.

In my eyes a "key" selector (default is the current setting "name") for every module would be great:

- hosts: all
  become: yes 
  tasks:
   - name: Create device within NetBox with dynamic information
      delegate_to: localhost
      become: no
      netbox.netbox.netbox_device:
        netbox_url: https://netbox.example.com
        netbox_token: 123456789123456789123456789123456
        data:
          name: "{{ ansible_hostname }}"
          device_type: TypeX
          device_role: Customer System
          status: Staged
          asset_tag: "{{ company_asset_tag }}"
        key_field: asset_tag
        state: present

this would set name, device_type, device_role, stage where an asset_tag is given or it would create an entry. The fact would have to be gathered first obviously - and the fact should be unique.

Another possibility would be mass updates:

- name: "Do things local"
  connection: local
  hosts: localhost
   - name: Set every device in a site as staged
      netbox.netbox.netbox_device:
        netbox_url: https://netbox.example.com
        netbox_token: 123456789123456789123456789123456
        data:
          site: very new site
          status: Staged
        key_field: site
        state: updated

This would require a new state, that would not create an item if none exists.

@FragmentedPacket
Copy link
Contributor

I think I like the idea of the key_field. I may just rename that to query_params.

If query_params is defined, it will ignore the built-in ones in the netbox_utils and this allows the users to search however they want. So you won't be able to change anything that is defined in the query_params, but gives you that flexibility you're looking for.

I think that should work for changing the name, but let me prototype this and then I'll submit a PR and have you test it.

@FragmentedPacket
Copy link
Contributor

@monkz I submitted PR #222 for this. Do you mind testing it. The error message provided if an invalid query is provided is cryptic. I did add a new method to validate the query_params passed in by the user, but it adds 3-5 seconds per task for it instead of being sub second so not sure the validation is worth the extra delay at this point.

@monkz
Copy link
Author

monkz commented Jun 7, 2020

Ok, i created a full device inventory with your change - works. During next week we will have such a rename event. So i'll test the ability to track devices on a serial in a production environment.

@monkz
Copy link
Author

monkz commented Jun 10, 2020

Test in production environment will delay for another week.

@monkz
Copy link
Author

monkz commented Jun 10, 2020

Well - scratch the delay. It works as expected. @FragmentedPacket Thanks a lot!

@FragmentedPacket
Copy link
Contributor

Thanks for testing!

@damo2929
Copy link

damo2929 commented Dec 11, 2023

so based on this I can rename a device as follows ?
``

- name: Rename interface matching mac
      netbox.netbox.netbox_device_interface:
        netbox_url: http://netbox.local
        netbox_token: thisIsMyToken
        data:
          device: test100
          name: enxaeae90ef1bf2
          mac_address: ae:ae:90:ef:1b:f2 
        state: present
        query_params: mac_address

``

@monkz
Copy link
Author

monkz commented Feb 18, 2024

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants