This is a script for setting the owner and creation date of a Virtual Machine upon creation or deployment
The script is meant to be run by an alarm set in vCenter.
The script can be placed almost anywhere on your vCenter Appliance, but I recommend placing it in "/root/alarmscripts"
You need to create a user that has access to set custom attributes vCenter tree. Normally set in the top of the tree (On the vCenter object) You should create a custom role for this limit the users permissions as much as possible in case the account gets compromised.
- Create a Role in vCenter
- Create a User or use existing user
- Give the user access to set custom attributes by using the new role
- Copy the script to vCenter
- Change the username and password variables in the script to suit your needs or enable password obfuscation and run the script from the console to create the secrets file
- Set the correct file rights on the file to prohibit non root users from reading the service account password
- Create the custom attribute in vCenter. The script can do it automatically, but the default role does not have access
- Create the trigger alarm
- Test by Creating a new vm
- Test by Deploying a new vm from a template or ovf
- The alarm does not work for VM created from a clone.
Role Name | Rule Privileges |
---|---|
Set Custom Attributes | Global -> Set custom attribute |
Username | Permission Path | Role | Propagate to children |
---|---|---|---|
[email protected] | vCenter Object | Set Custom Attributes | YES |
chmod 700 /root/alarmscripts/vm.alarm.new-vm.py
chmod 700 /root/alarmscripts/retrieve_information.py
Setting | Value |
---|---|
Alarm Name | Add Creator Information to Virtual Machine on Creation |
Target Type | Virtual Machines |
Alarm 1 - Rule Trigger | Creating VM |
Alarm 1 - Trigger the alarm and | Keep the target's current starte |
Alarm 1 - Run Script | Enabled |
Alarm 1 - Run this Script | /root/alarmscripts/vm.alarm.new-vm.py |
Alarm 2 - Rule Trigger | Deploying VM |
Alarm 2 - Trigger the alarm and | Keep the target's current starte |
Alarm 2 - Run Script | Enabled |
Alarm 2 - Run this Script | /root/alarmscripts/vm.alarm.new-vm.py |
The custom attribute is not set, or does not exists on the VM. You need to manually crete the custom attribute in vCenter.
I did take inspiration from Bryan McClellan and MARK III SYSTEMS BLOG.
I just didn't like the python code, and I wanted to improve it a little. If you need inspiration building the alarms have a look at Mark's blog.