Register Agent with Director Self-Service API for existing Host AND change assigned Template

Hi,

I would like to use some kind of a “two step” Migration from a legacy monitoring system to an icinga2 based one.

Step 1: import all hosts and associate them with a “ping-only” host template
- fiddle around with custom attributes, grouping etc.
- hosts don’t have a self-service key!

Step 2: Rollout Agents
- agents will be installed using self-service API
- using self-service API Key for host-template to be used from now on

The problem I am struggling with:

Step 2 works like charm - except: the Host-Template associated with the used Self-Service API key gets not assigned to the already existing host.

What I would like to accomplish is:

During agent-installation the template of a host does change to the one denoted by the Self-Service API key - even if there is already a template on a host.

Maybe I can use an API-Call after registration to change the template - but this needs to use Self-Service API & Templatename during installation - and I hoped to just somehow could use one parameter only: key or template name.

Is there any “short” way to accomplish this?

Thx!

I have nearly the same problem.
My hosts are imported by the vSphere DB module.
I thought the template I use for the Self Service Api Is automatically assigned to the Host during installation. In my host template I have a custom var that flags the host as “Agent was installed” to assign additional services to the host.

Option 1)
in your VShpere db import you can add a sync property
Destination field inheritance (import)
Source Column → choose your template
based on filters you could do that for all of your host types

Option 2)
create in import source director
set up a sync rule with this sourcename
Destination field inheritance (import)
Source Column → choose your template
based on filters you could do that for all of your host types

Option 3)
Use IcingaWeb Director Api to fetch the Host object
change the import field
use PUT to replace the object object should be merged with your changes
or i think you can also just post a json {“imports”: [“yourtemplatename”]}
to https://icinga-master/director/host?name=hostname.example.com

I already import 1-2 Templates depending of OS. One that sets a custom property to identify that the host is a vmware vm, and one for the os from the infos given by vsphere db
image

But it seems its not possible trigger the import of the template that holds the self-service api key. I get an updated host in the Activity Log after installing the agent but that has no changes.

Your idea to call the Director API after installation by PowerScript seems like a good idea as I have all information i need. I’ll test this!

Here a basic PowerShell that sets vars.agent of a host by an api user

$paswd = ConvertTo-SecureString "xxx" -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ('myuser', $paswd)
Invoke-RestMethod -Uri "https://monitoring.host.tld/director/host?name=$(Get-IcingaHostname -AutoUseFQDN $true -LowerCase $true)" -Method Get -Headers @{"Accept"="application/json"} -Credential $cred
Invoke-RestMethod -Uri "https://monitoring.host.tld/director/host?name=$(Get-IcingaHostname -AutoUseFQDN $true -LowerCase $true)" -Method Post -Headers @{"Accept"="application/json"} -Credential $cred -Body (@{"vars.agent"=$true} | ConvertTo-Json)
Invoke-RestMethod -Uri "https://monitoring.host.tld/director/host?name=$(Get-IcingaHostname -AutoUseFQDN $true -LowerCase $true)" -Method Get -Headers @{"Accept"="application/json"} -Credential $cred

(You can skip the Get commands in production and catch error 304 if the host already has this flag.

The role for the user needs just 3 permissions:
image

1 Like

Hi,
which is the command to post an update of template?

For example, I would like to import another template on my host.
How to xecute by powwershell?

I tried:
$response = Invoke-RestMethod ‘https://myicingasite.domain.net/icingaweb2/director/host?name=namehosttochange.domain.net’ -Method ‘POST’ -Headers $headers -Body (@{“imports”=[“windows-host”]} | ConvertTo-Json)

but I obtain:

n riga:1 car:163
+ ... ar.net' -Method 'POST' -Headers $headers -Body (@{"imports"=["windows ...
+                                                                  ~
Nome di tipo mancante dopo '['.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingTypename

Could you help me?

Thanks
Mario