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, | |
} |
Would also offer as alternative
start_datetime: "{{ lookup('pipe', 'date --iso-8601=seconds') }}"
future_datetime: "{{ lookup('pipe', 'date --iso-8601=seconds -d \"+60 min\" ')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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