-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathspoke-hello-world.yml
128 lines (111 loc) · 4.18 KB
/
spoke-hello-world.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
- name: Implement closed loop automation with Ansible and ServiceNow
hosts: localhost
connection: local
gather_facts: no
vars_files:
- ./vars/default_vars.yml
- ./credentials/servicenow-credentials.yml
- ./credentials/gmail_creds.yml
tasks:
- name: Create ServiceNow Change Request
servicenow.itsm.change_request:
instance:
host: "https://{{ SN_INSTANCE }}.service-now.com"
username: "{{ SN_USERNAME }}"
password: "{{ SN_PASSWORD }}"
type: normal
priority: high
risk: moderate
other:
approval: requested
state: assess
assignment_group: "CAB Approval"
short_description: Hello World Change Request
description: Hello World Change Request
register: new_change_request
- name: Print out Change Request number
ansible.builtin.debug:
msg: "The newly created change request is {{ new_change_request.record.number }}. Awaiting approval from Change Advisory Board."
- name: Print out Change Request details
ansible.builtin.debug:
var: new_change_request
#https://www.infinitypp.com/ansible/email-notifications-with-examples/
- name: Sending an email requesting approval
community.general.mail:
host: smtp.gmail.com
port: 587
username: "{{ gmail_username }}"
password: "{{ gmail_password }}"
to:
- "{{ primary_approver }}"
- "{{ secondary_approver }}"
subject: "Approval request for ServiceNow Change Request {{ new_change_request.record.number }}"
body: "You have a new CR Approval request. Please click on this URL to approve: https://{{ SN_INSTANCE }}.service-now.com/nav_to.do?uri=change_request.do?sysparm_query=number={{ new_change_request.record.number }}"
- name: Wait for Change Request approval
servicenow.itsm.change_request_info:
instance:
host: "https://{{ SN_INSTANCE }}.service-now.com"
username: "{{ SN_USERNAME }}"
password: "{{ SN_PASSWORD }}"
sys_id: "{{ new_change_request.record.sys_id }}"
register: change_request_status
until: change_request_status.records[0].state == "scheduled"
retries: 50
delay: 10
- name: Print out Change Request state
ansible.builtin.debug:
msg: "The current state of the change request is {{ change_request_status.records[0].state }}."
- name: Do the actual Ansible Task
ansible.builtin.debug:
msg:
- "{{ hello_world_msg }}"
- name: Move the Change request state to Review
servicenow.itsm.change_request:
instance:
host: "https://{{ SN_INSTANCE }}.service-now.com"
username: "{{ SN_USERNAME }}"
password: "{{ SN_PASSWORD }}"
state: "{{ item }}"
sys_id: "{{ new_change_request.record.sys_id }}"
loop:
- "implement"
- "review"
- name: Close the ServiceNow Change Request
servicenow.itsm.change_request:
instance:
host: "https://{{ SN_INSTANCE }}.service-now.com"
username: "{{ SN_USERNAME }}"
password: "{{ SN_PASSWORD }}"
close_code: "successful"
close_notes: "Closed"
state: closed
sys_id: "{{ new_change_request.record.sys_id }}"
- name: Sending a success email
mail:
host: smtp.gmail.com
port: 587
username: "{{ gmail_username }}"
password: "{{ gmail_password }}"
to:
- "{{ primary_approver }}"
- "{{ secondary_approver }}"
subject: "Change Request {{ new_change_request.record.number }} Successful"
body: "Your ServiceNow Change Request {{ new_change_request.record.number }} has been completed successfully!"
#state
# -5 is just created
# -4 means approval has been requested
# -3 means partial approval
# -2 means it has been approved
# -1 means it is in the implement stage
# 0 means it is in the review state
# 3 means CR closed successfully (perhaps with issues)
#approval
# - not requested
# - requested
# - approved
#close_code
# - successful
# - successful with issues
# - unsuccessful
#closed_notes