-
-
Save abeluck/49ed68707d69862038f22d1c0d2f5cfc to your computer and use it in GitHub Desktop.
# creates a silence in alertmanager that starts `now` and lasts for 10 minutes | |
- hosts: localhost | |
gather_facts: yes | |
tasks: | |
- set_fact: | |
start_datetime: "{{ ''| local_time_iso8601 }}" | |
future_datetime: "{{ ''|local_time_iso8601 | add_time_iso8601(minutes=10) }}" | |
- debug: | |
var: future_datetime | |
- name: silence alertmanager for reboot | |
uri: | |
url: http://10.1.1.1:9093/api/v1/silences | |
method: POST | |
body_format: json | |
body: > | |
{ | |
"matchers": [ | |
{ | |
"name": "instance", | |
"value": "{{ inventory_hostname }}", | |
"isRegex": false | |
} | |
], | |
"startsAt": "{{ start_datetime }}", | |
"endsAt": "{{ future_datetime }}", | |
"createdBy": "ansible-system-update", | |
"comment": "Silence for system-update reboot", | |
"status": { | |
"state": "active" | |
} | |
} | |
register: silence_resp | |
- debug: | |
var: silence_resp | |
- pause: | |
- name: delete silence alertmanager for reboot | |
uri: | |
url: http://10.1.1.1:9093/api/v1/silence/{{ silence_resp.json.data.silenceId }} | |
method: DELETE | |
register: silence_resp_del | |
- debug: | |
var: silence_resp_del |
# filters/time.py | |
# drop in your filters dir | |
import datetime | |
import dateutil.parser | |
import sys | |
if sys.version_info[0] < 3: | |
raise Exception("Must be using Python 3") | |
fmt = "%Y-%m-%dT%H:%M:%S.%fZ" | |
def local_time_iso8601(string, **kwargs): | |
return datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() | |
def add_time_iso8601(string, **kwargs): | |
dt = dateutil.parser.parse(string) | |
return (dt + datetime.timedelta(**kwargs)).strftime(fmt) | |
class FilterModule(object): | |
def filters(self): | |
return { | |
"local_time_iso8601": local_time_iso8601, | |
"add_time_iso8601": add_time_iso8601, | |
} |
Hi Team, i have little modified the code to have time factor to removing the silence incase the activity is finished earlier using the below:
Create silence alert manager using below:
name: Run silence alert manager"
gather_facts: yes
hosts: host1
remote_user: user1
tasks:
-
name: Get start silence time
command: "date --iso-8601=seconds"
delegate_to: localhost
register: start_time
-
name: Get finish silence time
command: date -d "+{{ time }} minutes" --iso-8601=seconds
delegate_to: localhost
register: finish_time
-
name: silence alertmanager for reboot
uri:
body: >
{
"matchers": [
{
"isRegex": false,
"name": "alertname",
"value": "instance_name"
},
{
"isRegex": false,
"name": "alertname",
"value": "instance_status"
}
],
"startsAt": "{{ start_time.stdout }}",
"endsAt": "{{ finish_time.stdout }}",
"createdBy": "ansible-system-update",
"comment": "Silence for system-update reboot",
"status": {
"state": "active"
}
}
body_format: json
method: POST
url: http://host1:9093/api/v1/silences
return_content: yes
headers:
Content-Type: "application/json"
validate_certs: no
register: silence_resp
-
debug:
var: silence_resp
Remove silence alert manager using below:
-
name: Remove silence in alert manager"
gather_facts: yes
hosts: host1
remote_user: user1
tasks:-
name: get silences in alert manager
shell : curl -L -s http://host1:9093/api/v1/silences| jq '.data[]| select(.status.state == "active").id'
register: silence_resp -
name: delete silence alertmanager for reboot
shell: curl -s -X DELETE http://host1:9093/api/v1/silence/{{ silence_resp.stdout }}
register: silence_resp_del -
debug:
var: silence_resp_del
-
Would also offer as alternative
start_datetime: "{{ lookup('pipe', 'date --iso-8601=seconds') }}"
future_datetime: "{{ lookup('pipe', 'date --iso-8601=seconds -d \"+60 min\" ')
It is easier to delegate to localhost the calculation of the start and finish time with ta task like this:
and use in "endsAt" field the registered value as
finish_time.output
. This way you avoid the python script and setting facts.