-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart-service.yml
57 lines (53 loc) · 1.78 KB
/
restart-service.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
#####
# Run command - default to restart sssd.service
# ansible-playbook ./playbooks/server-service-restart.yml -i <targetServer>, -K
#
# Run command to restart specific service - for example restart autofs.service
# ansible-playbook ./playbooks/server-service-restart.yml -i <targetServer>, --extra-vars "target_service=autofs.service" -K
#
# To post message to slack, export slack token to env variable then add extra-var to command line,
# for example: --extra-vars "slack_webhook_token=$SLACK_TOKEN"
#####
---
- name: Ansible restart a service
hosts: all
become: true
gather_facts: true
vars:
# set default service to start or change it from cli
target_service: "sssd.service"
slack_webhook_token: ""
tasks:
# restart service with service module
- name: Restart services
ansible.builtin.service:
name: "{{ target_service }}"
state: restarted
register: result
tags:
- setup
# For debuging
- name: Print debug messages
ansible.builtin.debug:
var: result
verbosity: 2
tags:
- setup
# post message to Slack if web hook token available in env variable or AWX 's variables
- name: Post message to Slack
slack:
token: "{{ slack_webhook_token }}"
msg: "Service '{{ target_service }}' has been restarted at '{{ ansible_host }}' on '{{ ansible_date_time.iso8601 }}'"
no_log: True
register: slack_posted
when: slack_webhook_token != ""
tags:
- setup
# print task run to log for audit trail if we skip post message to slack
- name: Print service restart time stamp
ansible.builtin.debug:
msg:
- "Service '{{ target_service }}' has been restarted at '{{ ansible_host }}' on '{{ ansible_date_time.iso8601 }}'"
when: slack_posted is skipped
tags:
- setup