community.vmware icon indicating copy to clipboard operation
community.vmware copied to clipboard

Set custom attributes on vCenter objects using an ansible module

Open liamwh opened this issue 3 years ago • 7 comments

SUMMARY

An Ansible module for host custom attributes exists.

An ansible module for guest custom attributes also exists.

An ansible module for gathering custom attribute info from an object exists.

But no module exists to configure the custom attributes on any object. This request is to create such a module.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

community.vmware.vmware_object_custom_attributes, very similar to:

vmware_object_custom_attributes_info_module

ADDITIONAL INFORMATION

This would allow the idempotent setting of custom attributes on all objects on vCenter using Ansible, not just hosts and VMs.

Example Playbook:

- name: Set custom attribute on a datacenter object
  community.vmware.vmware_object_custom_attributes:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: false
    object_type: Datacenter
    name: "MyDatacenter" # Or moid would also suffice, for example: moid: "{{ moid }}"
    attributes:
        - name: MyDatacenterCustomAttributeKey
          value: MyDatacenterCustomAttributeValue
  register: set_attributes_on_datacenter

liamwh avatar Mar 10 '22 13:03 liamwh

Files identified in the description: None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot avatar Mar 10 '22 13:03 ansibullbot

Thank you @liamwh for requesting the feature.
As you say, the modules exist for each object, so I wonder if I create the module for each object or cover all objects in one module.

What do you think? @mariolenz

sky-joker avatar Mar 27 '22 06:03 sky-joker

As you say, the modules exist for each object, so I wonder if I create the module for each object or cover all objects in one module.

What do you think? @mariolenz

@sky-joker I think that instead of having several modules for different object types, it would be easier to have just one. Similar to vmware_object_role_permission where there's a object_type parameter.

This would also be more consistent, as we would have only one module to get custom attributes (vmware_object_custom_attributes_info) and only one to set them (plain vmware_object_custom_attributes?).

Thanks @liamwh for the suggestion, I haven't spotted this yet!

mariolenz avatar Mar 27 '22 16:03 mariolenz

I've started to have a look into this, and I already found something that might be problematic. Both vmware_guest_custom_attributes and vmware_host_custom_attributes seem to silently create the attribute if it doesn't exist:

https://github.com/ansible-collections/community.vmware/blob/39f18d61a4dde7f2b6c861133064d3b9a306ce9b/plugins/modules/vmware_guest_custom_attributes.py#L187-L189

https://github.com/ansible-collections/community.vmware/blob/39f18d61a4dde7f2b6c861133064d3b9a306ce9b/plugins/modules/vmware_host_custom_attributes.py#L135-L137

I'm not sure if this is a wise decision. This means that every typo leads to a new custom attribute. I think it would be better to fail the module if someone tries to set a custom attribute that's not defined.

What do you think @liamwh and @sky-joker?

mariolenz avatar Mar 28 '22 13:03 mariolenz

Thank you @mariolenz for checking it! Users can freely assign the custom attributes as user-specific, so I think it is the correct behavior to add it even if the custom attribute is a typo :) https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.vcenterhost.doc/GUID-73606C4C-763C-4E27-A1DA-032E4C46219D.html

I think only users will know if it is a typo or not🤔

sky-joker avatar Mar 28 '22 15:03 sky-joker

For example, I think it also likes to precheck by using vmware_object_custom_attributes_info module whether the custom attribute name is correct.

sky-joker avatar Mar 28 '22 15:03 sky-joker

Thanks for looking into this guys. We definitely would like to set the value of custom attributes that don't exist yet.

I also agree that keeping it with 1 generic "custom_attributes" module with an object type param makes more sense over having many custom attribute modules for many different object types.

liamwh avatar Apr 13 '22 14:04 liamwh

@liamwh @remkolodder If you're still around and interested in this, you might want to have a look at #1536:

mkdir -p  ~/.ansible/collections/ansible_collections/community
git clone https://github.com/ansible-collections/community.vmware ~/.ansible/collections/ansible_collections/community/vmware
cd ~/.ansible/collections/ansible_collections/community/vmware
git fetch origin pull/1536/head:issue1250
git checkout issue1250

The modules are vmware_custom_attribute to define custom attributes and vmware_custom_attribute_manager to manage them on a specific object.

mariolenz avatar Nov 08 '22 18:11 mariolenz

I am still around :-) I am finishing up a release at work and will add this to the todo for the next internal release.

Many thanks to start with!

remkolodder avatar Nov 13 '22 09:11 remkolodder

Hello,

It took me a while, but I finally managed to start using them. It seems that these custom attributes can be defined easily on datacenter / datastore / folder / etc level. But you can only create an item on global level, but not set the value, where you can do so in the GUI. Is there a way to "add" the Global option to setting the value on that as well? it should then cascade through all objects below (like it does when you add a key via the global, you can read it on the lowest level, but you cannot set a value on the toplevel and see that cascaded on the lowest level as well).

remkolodder avatar May 04 '23 12:05 remkolodder