Apply LifeCycle Policy To Many Indices

I have about 200 older indices that do not have my ILM policy applied to them. Instead of going through Kibana and adding the policy one by one, what's the API endpoint and syntax for adding a policy to an index?

Disregard, I figured out how to do it with browser tools and PowerShell..slightly over-engineered the solution but it's functional for anyone else that needs to do the same.

#Instructions
#Line 1: Add all indices you intend to modify, each index double quoted and comma separated.
#Line 2: Change to the name of the policy you want to apply, retain double quotes.
#Line 5: Change to version of Kibana being used.
#Line 13: Change URI parts: kibana.FQDN.com to FQDN of Kibana server, SPACENAME to the name of a space the indexes exist in.
$Indices = "index1", "index2", "etc"
$policy = "NameOfPolicy"
$Options = @{
  headers = @{
    'kbn-version' = "7.0.0"
  }
  ContentType = "application/json"
  Method = "Post"
}
foreach ($index in $indices) {
  $payload = '{"indexName":"'+$index+'","policyName":"'+$policy+'"}'
  try {
    $result = Invoke-WebRequest @Options -Body $payload -Uri "http://kibana.FQDN.com:5601/s/SPACENAME/api/index_lifecycle_management/index/add"
  }
  Catch {
  Write-Host Index: $index  Result: ($error[0].errordetails | ConvertFrom-Json | Select -ExpandProperty error)
  Continue
  }
  Write-Host Index: $index  Result: $result.Content
}

2 Likes

Re-factored it a bit....asks for information instead of having to fiddle with the script. Probably not a good idea to use in large environments since I'm calling ALL indices. If there's a way to query indices that match an index name, someone please let me know so I can change the initial call and shorten this by a line or two.

Save the below as ILMScript.ps1 (or any other name you prefer) and execute.

$Options = @{
  headers = @{
    'kbn-version' = "7.0.0"
  }
  ContentType = "application/json"
  Method = "GET"
}
$i = 1
$eserver = Read-Host "Enter Elasticsearch server:port number"
$kserver = Read-Host "Enter Kibana server:port number"
$indices = Read-Host "Enter index name to search for"
$result = Invoke-WebRequest @options -Uri http://$eserver/_ilm/policy
Clear-Host
Write-Host "Index policies discovered:"
$result.content | ConvertFrom-Json | Get-Member -MemberType NoteProperty | Select Name -ExpandProperty Name
$policy = Read-Host "Enter name of policy to apply"
$result = Invoke-WebRequest @Options -Uri http://$kserver/api/index_management/indices
$tindices = $result.Content | ConvertFrom-Json
$tindices = $tindices | Where-Object {$_.name -match ".*$indices.*"}
$Options.Method = "POST"
Clear-Host
Do {
$act = Read-Host $tindices.count"indices will be effected.`n(v)iew effected indices`n(a)pply policy`n(q)uit."
Clear-Host
if ($act -match "v|view") {
  $tindices | Format-Table -AutoSize
}
  else {
    if ($act -match "q|quit") {
      Exit
    }
  }
}
Until ($act -match "a|apply")
Clear-Host
foreach ($index in $tindices) {
  $activity = "Applying $policy to "+$index.name
  $status = "Index $i of "+$tindices.count
  Write-Progress -Activity $activity -Status $status -PercentComplete (($i / $tindices.count) * 100)
  $i++
  $payload = '{"indexName":"'+$index.name+'","policyName":"'+$policy+'"}'
  try {
    $result = Invoke-WebRequest @Options -Body $payload -Uri "http://ssselklog.stlouisco.net:5601/s/wel/api/index_lifecycle_management/index/add"
  }
  Catch {
    Write-Host Index: $index.name  Result: -NoNewLine; Write-Host -ForegroundColor Red($error[0].errordetails | ConvertFrom-Json | Select -ExpandProperty error)
  Continue
  }
  Write-Host Index: $index.name Result: -NoNewLine; Write-Host -ForegroundColor Green ($result.content | ConvertFrom-Json | Select -ExpandProperty acknowledged)
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.