-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-scheduled-task.yml
77 lines (66 loc) · 2.56 KB
/
batch-scheduled-task.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
- name: 'Test playbook to sleep for a specified interval'
#################################################
#This selects a random host from hosts file
#################################################
hosts: "{{ groups['cluster_members'] | random }}"
#################################################
gather_facts: false
vars:
- sleep_interval: 3600
- slack_token: ${SLACK_TOKEN}
- datadog_api_key: ${DATADOG_API_KEY}
- datadog_app_key: ${DATADOG_APP_KEY}
tasks:
- name: Send notification message via Slack of start
slack:
token: '{{ slack_token }}'
msg: 'sleep on {{ inventory_hostname }} beginning run'
channel: '#general'
delegate_to: localhost
##################################################
# This is where you would add your scheduled task
##################################################
- name: sleep for a specified interval
command: sleep '{{ sleep_interval }}'
register: result
##################################################
- name: Add success to syslog
when: result|succeeded
syslogger:
msg: "sleep ran successfully"
- name: Add fail to syslog
when: result|failed
syslogger:
msg: "sleep ran unsuccessfully"
- name: Send notification message via Slack of success
slack:
token: '{{ slack_token }}'
msg: 'sleep on {{ inventory_hostname }} ran successfully'
channel: '#general'
when: result|succeeded
delegate_to: localhost
- name: Send notification message via Slack of failure
slack:
token: '{{ slack_token }}'
msg: 'sleep on {{ inventory_hostname }} ran unsuccessfully'
channel: '#general'
when: result|failed
delegate_to: localhost
- name: Send event notification to Datadog of success
datadog_event:
title: 'Ansible on {{ inventory_hostname }}'
text: 'sleep on {{ inventory_hostname }} ran successfully'
api_key: '{{ datadog_api_key }}'
app_key: '{{ datadog_app_key }}'
tags: '#host:{{ inventory_hostname }}'
when: result|succeeded
delegate_to: localhost
- name: Send event notification to Datadog of failure
datadog_event:
title: 'Ansible on {{ inventory_hostname }}'
text: 'sleep on {{ inventory_hostname }} ran unsuccessfully'
api_key: '{{ datadog_api_key }}'
app_key: '{{ datadog_app_key }}'
tags: '#host:{{ inventory_hostname }}'
when: result|failed
delegate_to: localhost