The best idea in this case, will be modify the statemachine for the vm provision, and add some method to query foreman API in order to ask it when the puppet run was executed, and if not, retry.
Something like this:
require 'httparty'
@method = 'check_puppet'
@debug = true
$evm.log("info", "#{@method} Started")
attributes = $evm.root.attributes
prov = $evm.root['miq_provision']
if not prov.nil?
vm = prov.vm
else
vm = $evm.root['vm']
end
hostname=prov.get_option(:vm_target_hostname)
domain=nil
domain ||= prov.get_option(:global_domain)
domain ||= prov.get_option(:dns_domain)
unless prov.get_option(:ws_values).nil?
domain ||= prov.get_option(:ws_values)[:global_domain]
end
$evm.log("info","check_puppet: domain: #{domain}")
foremanhost, foremanport, foremanuser, foremanpassword, foremanprotocol, waitfor = nil, nil, nil, nil, nil, nil
foremanhost ||= $evm.object['foremanhost']
foremanport ||= $evm.object['foremanport']
foremanprotocol ||= $evm.object['foremanprotocol']
foremanuser ||= $evm.object['foremanuser']
foremanpassword ||= $evm.object['foremanpassword']
foremanhostgroup_default ||= $evm.object['foremanhostgroup_default']
foremanpuppet ||= $evm.object['foremanpuppet']
foremanroot = nil
foremanroot ||= $evm.object['foremanroot']
v2 = nil
v2 ||= $evm.object['v2']
v2parameters = nil
v2parameters ||= $evm.object['v2parameters']
managed = nil
managed ||= $evm.object['managed']
foremanorganization ||= $evm.object['foremanorganization']
foremanlocation ||= $evm.object['foremanlocation']
waitfor ||=$evm.object['waitfor']
headers = { 'Content-type' => 'application/json', 'Accept' => 'application/json' }
auth = {:username => foremanuser, :password => foremanpassword}
fqdn = "#{hostname}.#{domain}"
if foremanroot != '/'
uri = "#{foremanprotocol}://#{foremanhost}:#{foremanport}/#{foremanroot}/api"
else
uri = "#{foremanprotocol}://#{foremanhost}:#{foremanport}/api"
end
if v2
uri = "#{uri}/v2"
end
if fqdn.nil?
$evm.log("info","The hostname cannot be found, exiting")
exit MIQ_ERROR
end
$evm.log("info","FQDN: #{fqdn}")
hostid = HTTParty.get("#{uri}/hosts?search=#{fqdn}", :headers => headers, :basic_auth => auth)['results'][0]['id']
lastpuppetstatus = HTTParty.get("#{uri}/reports/last?id=#{hostid}", :headers => headers, :basic_auth => auth)['status']
$evm.log("info","Checking puppet status for #{fqdn}")
lastpuppetstatus.each do |status,times|
$evm.log("info","#{status} -> #{times}")
if (status == 'applied' || status == 'failed') && times > 0
$evm.log("info","Last puppet status: #{status}")
$evm.root['ae_result'] = 'ok'
exit MIQ_OK
end
end
$evm.log("info","Not run yet, retrying")
$evm.root['ae_result'] = 'retry'
$evm.root['ae_retry_interval'] = "#{waitfor}.seconds"