Skip to content

Instantly share code, notes, and snippets.

@abeluck
Last active September 21, 2023 13:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save abeluck/49ed68707d69862038f22d1c0d2f5cfc to your computer and use it in GitHub Desktop.
Save abeluck/49ed68707d69862038f22d1c0d2f5cfc to your computer and use it in GitHub Desktop.
Ansible tasks to create and delete alertmanager silences
# 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,
}
@Lusitaniae
Copy link

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