This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
catchingerrors-playbook.yaml
86 lines (72 loc) · 3.28 KB
/
catchingerrors-playbook.yaml
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
78
79
80
81
82
83
84
85
86
---
- name: A playbook for demonstrating error handling techniques
hosts: planetexpress
gather_facts: yes
vars:
apps_to_install: [apache2, vsftpd]
tasks:
- name: PRECHECK PHASE
block:
- name: Ensure platforms we logged into are all Debian
fail:
msg: "Ansible has detected this host is not supported"
when: ansible_os_family != "Debian"
rescue:
- name: PRECHECK PHASE - FAILED
fail:
msg: "PRECHECK PHASE - FAILED! Nothing to rollback. Exiting..."
- name: MAINTENANCE PHASE
block:
- name: Install services to our remate hosts
apt:
state: present
name: "{{ apps_to_install }}"
become: yes
- name: Turn up the service
service:
state: started
name: "{{ item }}"
loop: "{{ apps_to_install }}"
become: yes
# with_item <--- old way to do a loop
rescue:
- name: MAINTENANCE PHASE - FAILED - Rollback services on remote hosts
apt:
state: absent
name: "{{ apps_to_install }}"
become: yes
- name: MAINTENANCE PHASE FAILED - ROLL BACK COMPLETE... EXITING
fail:
msg: "FAILED! Rollback complete. Exiting..."
- name: POSTCHECK PHASE
block:
- name: populate service facts
service_facts:
- name: show the services running
debug:
var: ansible_facts.services # this was auto created by service_facts
- name: ensure our new services are installed
fail:
msg: "Ansible has detected not all services have installed properly"
when: ansible_facts.services.get(item).state != "running"
loop: "{{ apps_to_install }}"
rescue:
- name: POSTCHECK - FAILED - Rollback services on remote hosts
apt:
state: absent
name: "{{ apps_to_install }}"
become: yes
- name: POSTCHECK PHASE FAILED - ROLL BACK COMPLETE... EXITING
fail:
msg: "FAILED! Rollback complete. Exiting..."
## in a real deployment you might consider an ALWAYS section to provide the results
## of how the playbook ran
# always:
# - name: EXAMPLE - Email your team the results of the playbook
# mail: # mail module is used to send SMTP (email)
# host:
# port:
# password:
# username:
# subject:
# body: send an email with results via the mail module