Retrieve selected tags in a script

automate

#1

Hello everyone.

So i’m currently working on a script that syncs particular manageiq and vcenter tags.
I’m calling the script after the vm is provisioned because i need the id of the vm in vcenter for the script.

2018-04-09_14-17-06
But the thing I’m struggling with is retrieving the tags and name from the provisioning wizard.
I’m also planning to do it with a service dialog in the future but now (for testing purposes) I’m just doing it with the vm provisioning wizard.

This is what i got at the moment to retrieve the tags and vm name. The portion to retrieve the vm name works but the portion for the tags doesn’t.

prov = $evm.root['miq_provision']
vm_target_name = prov.get_option(:vm_target_name)
tagcodename = prov.get_tags(:cat_code)
tagbackupname = prov.get_tags(:cat_backup)
tagreplicationname = prov.get_tags(:cat_replication)

Is there anyone who can help me? i’ve already read the cloudforms automation guide but can’t find out how to retrieve the tags.

Thanks in advance.


#2

So i finally got it working :slight_smile:
I don’t know why it works now and not before but this is the code i got at the moment:

prov = $evm.root['miq_provision']
vmname = prov.get_option(:vm_target_name)
tags = prov.get_tags
tagcodename = tags[:cat_code]
tagbackupname = tags[:cat_backup]
tagreplicationname = tags[:cat_replication]

#3

get_tags function does not have any arguments, but returns a hash of all tags. Try this:

tagcodename = prov.get_tags[:cat_code]

#4

@xian thanks, that works.
Do you know by chance what the difference is between the square [] and normal () brackets?


#5

With normal brackets you specify function arguments, for example get_option has one argument: the name of the option you want to get. See the User Reference.

get_tags has no arguments and will return a hash of all tags (you can see it has no arguments in the User Reference above).
Out of that hash you can get the one you are interested in with the [] hash method (this is odd but in Ruby [] can be a name of a method, see Hash class). Kind of an array-ish syntax.

If you want to get tags of a particular category only, use get_tag(category) instead.


#6

Thanks, that was really helpfull :slight_smile: