Last active
September 21, 2023 13:11
-
-
Save abeluck/49ed68707d69862038f22d1c0d2f5cfc to your computer and use it in GitHub Desktop.
Ansible tasks to create and delete alertmanager silences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would also offer as alternative