23 May 2024
使用playbook从dynatrace删除k8s集群
在删除被dynatrace监控的集群时,需要及时将集群从dynatrace删除,以免遇到下面错误:
Maximum number of monitored Kubernetes clusters reached. Please disable monitoring of some clusters.
playbook如下:
---
- name: Remove cluster from dynatrace
hosts: localhost
connection: local
gather_facts: false
vars:
ansible_python_interpreter: ""
excluded_labels:
- cluster1
- cluster2
tasks:
- name: List all kubernetes from dynatrace
ansible.builtin.uri:
url: https://.live.dynatrace.com/api/v2/settings/objects?schemaIds=builtin%3Acloud.kubernetes&fields=objectId%2Cvalue&sort=created&pageSize=500
method: GET
headers:
Authorization: Api-Token
register: cluster_list
- name: Disable cluster monitoring from dynatrace
ansible.builtin.uri:
url: https://.live.dynatrace.com/api/v2/settings/objects
method: POST
headers:
Accept: "application/json; charset=utf-8"
Content-Type: "application/json; charset=utf-8"
Authorization: "Api-Token "
body_format: json
body: |
[
{
"schemaId": "builtin:cloud.kubernetes",
"schemaVersion": "3.1",
"value": {
"enabled": false,
"label": "",
"clusterIdEnabled": true,
"clusterId": ""
}
}
]
loop: ""
when: item.value.label not in excluded_labels
register: post_response
changed_when: post_response.status == 200
- name: Delete cluster object from dynatrace
ansible.builtin.uri:
url: https://.live.dynatrace.com/api/v2/settings/objects/
method: DELETE
status_code:
- 204
headers:
Accept: "application/json; charset=utf-8"
Content-Type: "application/json; charset=utf-8"
Authorization: "Api-Token "
loop: ""
when: item.value.label not in excluded_labels
register: delete_response
changed_when: delete_response.status == 204
LEo
at 00:12