Sync Vmware and ManageIQ tags (VM tags)

Hi all,

Please help me to find out how to solve Tags synchronization ManageIQ and Vmware ,
Does anybody has sync with ManageIQ and Vmware Tags ?

I have read AWS Tag Synchronization
which is 3 years ago published . so many things has been changed and maybe someone can point me to good documentation how to sync Tags ?

Maybe I can ask @carbonin to point me to right person ?

Tested in 2 different ways . 1. Created Vm in ManageIQ and assigned Tags ( which was not assigned in Vmware) . 2. Create Tag in Vmware and assigned to VM ( this vm in ManageIQ has no tags assigned to it )

So somewhere I miss sync configuration between ManageIQ -> Vmware


I’m not sure about this. @agrare would know if we pull in tag information from VMware inventory. If we don’t do any tag sync, maybe that’s a thing you could do through automate? @gmccullough should be able to point you in the right direction there.

1 Like

Thank you @carbonin ! I appreciate your fast and awesome support .

Hey @kalinux, we talked about this and there is an RFE open [] but the client libraries from VMware to retrieve the tags from vSphere aren’t in a state where we can readily use them yet.

1 Like

Hi @agrare,

Thanks for info . Then we have to postpone this discussion until Vmware build API logic for Tags.
Very satisfied how fast you guys reacting . Thanks a lot .!


Hey @kalinux,

I’m just getting into manageiq and i wanted to do the same thing as you.
After doing some research i’ve discovered that vsphere 6.5 has a rest api that includes methods to get all tags, attach tags to vm’s, create a new tag,…
Here’s the link to the documentation
Using this you can write some ruby scripts to automate it. I’m probably going to try it myself, and i can share my results if u want.

1 Like

Hi @Brackaboy,

That`'s awesome! please share results here , maybe not necessary me but other users will need this script/solution. We solved it by using only ManageIQ tags and not Vcenter.

1 Like

Hey everyone.

So I just finished my script for syncing Manageiq/cloudforms and vmware vcenter tags.
It’s kind of specific for the company where i’m doing my bachelor’s thesis, but it’s not that complicated and easy to adjust to your needs.
It’s made to only sync tags from specific categories i made in manageiq/cloudforms and vmware.
To use it you need to install “httparty” on your manageiq/cloudforms instance, i’ve tested it with httparty version 0.16.2.
It’s certainly not perfect but it works for my needs.

This is the code:

puts "Starting VMware TAG integration"
require 'httparty'

prov = $evm.root['miq_provision']
vmname = prov.get_option(:vm_target_name)
tags = prov.get_tags
puts "vm-tags: #{tags}"
tagcodename = tags[:cat_code]
tagbackupname = tags[:cat_backup]
tagreplicationname = tags[:cat_replication]

cat_id_code = $evm.object['cat_id_code']
cat_id_backup = $evm.object['cat_id_backup']
cat_id_replication = $evm.object['cat_id_replication']

base_url = $evm.object['base_url']

def getToken(base_url)
  puts "VMware TAG integration Entering getToken"
  auth = {:username => $evm.object['username'], :password => $evm.object.decrypt('password')}
  url = "#{base_url}/com/vmware/cis/session"

  response =, 
                     :basic_auth => auth, :verify => false)
  puts "VMware Debug URL. Auth is #{auth} <>"
  puts "VMware Debug URL. Response is #{response} <>"
  token = response.parsed_response["value"]
  puts "token = #{token}"
  return token

def getTags(token, base_url, category)
  puts "VMware TAG integration Entering getTags"
  tags =
  response ="#{base_url}/com/vmware/cis/tagging/tag/id:#{category}?~action=list-tags-for-category",
			:headers => { "vmware-api-session-id" => token },
			:verify => false)
  unless response.parsed_response["value"].nil?
    tags = response.parsed_response["value"]

#loop through each vmware tagname and see which one matches our name
def findTagMatch(token, base_url, tagname, tags)
  puts "VMware TAG integration Entering findTagMatch"  
  tags.each do |tag|
  response = HTTParty.get("#{base_url}/com/vmware/cis/tagging/tag/id:#{tag}",
                        :headers => { "vmware-api-session-id" => token },
                        :verify => false)
  unless response.parsed_response["value"].nil?
	puts "TagName: #{response.parsed_response["value"]["name"]}"
    if response.parsed_response["value"]["name"] == tagname
	puts "Tag found that matches #{34.chr}#{tagname}#{34.chr}"
      return tag

def newTag(token, base_url, tagname, cat_id_code)
  puts "VMware TAG integration Entering newTag"  
  if tagname =~ /[a-z][a-z][a-z]\d{4}/
  category = cat_id_code
  else abort "Defined tag name is not valid"
  payload = {
    create_spec: {
        category_id: category,
        description: "",
        name: tagname
  response ="#{base_url}/com/vmware/cis/tagging/tag",
    :headers => { "vmware-api-session-id" => token, "Content-Type" => "application/json" },
    :verify => false, :body => payload)
	unless response.parsed_response["value"].nil?
    puts "Tag #{34.chr}#{tagname}#{34.chr} with id #{34.chr}#{response.parsed_response["value"]}#{34.chr} is made"
	return response.parsed_response["value"]

def getVMID(token, base_url, vmname)
  puts "VMware TAG integration Entering getVMID"
  response = HTTParty.get("#{base_url}/vcenter/vm?filter.names=#{vmname}",
                        :headers => { "vmware-api-session-id" => token },
                        :verify => false)
  unless response.parsed_response["value"].nil?
    return response.parsed_response["value"][0]["vm"]

def attachTagObject(token, base_url, vmid, codetag, backuptag, replicationtag)
  puts "VMware TAG integration Entering attachTagObject"
  payload = {
    object_id: {
        id: vmid,
        type: "VirtualMachine"
    tag_ids: [
  response ="#{base_url}/com/vmware/cis/tagging/tag-association?~action=attach-multiple-tags-to-object",
    :headers => { "vmware-api-session-id" => token, "Content-Type" => "application/json" },
    :verify => false, :body => payload)
  unless response.parsed_response["value"].nil?
    puts "Tag attached to #{vmid} #{response.parsed_response["value"]}"

#Get a token to use
mytoken = getToken(base_url)

#Get array of all codetags in vcenter
codetags = getTags(mytoken, base_url, cat_id_code)
puts "Code Tag-id's: #{codetags}"

#Get array of all backuptags in vcenter
backuptags = getTags(mytoken, base_url, cat_id_backup)
puts "Backup Tag-id's: #{backuptags}"

#Get array of all replicationtags in vcenter
replicationtags = getTags(mytoken, base_url, cat_id_replication)
puts "Replication Tag-id's: #{replicationtags}"

#Get the tagID of the vcenter codetag matching the manageiq codetag 
codetag = findTagMatch(mytoken, base_url, tagcodename, codetags)
if codetag.kind_of?(Array)
codetag = "Tag not found"
puts "Code Tag-id: #{codetag}"

#if tag doesn't exist in vcenter, make a new one
if codetag == "Tag not found"
codetag = newTag(mytoken, base_url, tagcodename, cat_id_code)

#Get the tagID of the vcenter backuptag matching the manageiq backuptag 
backuptag = findTagMatch(mytoken, base_url, tagbackupname, backuptags)
puts "Backup Tag-id: #{backuptag}"

#Get the tagID of the vcenter replicationtag matching the manageiq replicationtag 
replicationtag = findTagMatch(mytoken, base_url, tagreplicationname, replicationtags)
puts "Replication Tag-id: #{replicationtag}"

#Get the id of the new VM in vcenter
vmid = getVMID(mytoken, base_url, vmname)
puts "VM-id: #{vmid}"

#Attach the tag to the vm
attachTagObject(mytoken, base_url, vmid, codetag, backuptag, replicationtag)

It needs to get called after the vm is provisioned so it can retrieve the vm-id from the vcenter appliance.

When it works your logs should look something like this:

I’ve also exported my code from my manageiq appliance so you can easily import it into yours :slight_smile:

Tagging.rar (7.3 KB)

1 Like

Hi, I am currently using CF 5.11 could you please help how to run this script for particular VM.

Can you provide steps to run your above code in my CF?

Just to update here we actually got vsphere tag mapping merged in this PR:

1 Like