Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

#!/usr/bin/python 

# -*- coding: utf-8 -*- 

# Copyright: (c) 2021, XLAB Steampunk <steampunk@xlab.si> 

# 

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 

 

 

from __future__ import absolute_import, division, print_function 

 

__metaclass__ = type 

 

 

DOCUMENTATION = r""" 

module: incident 

 

author: 

- Manca Bizjak (@mancabizjak) 

- Miha Dolinar (@mdolin) 

- Tadej Borovsak (@tadeboro) 

- Matej Pevec (@mysteriouswolf) 

 

short_description: Manage ServiceNow incidents 

 

description: 

- Create, delete or update a ServiceNow incident. 

- For more information, refer to the ServiceNow incident management documentation at 

U(https://docs.servicenow.com/bundle/paris-it-service-management/page/product/incident-management/concept/c_IncidentManagement.html). 

version_added: 1.0.0 

extends_documentation_fragment: 

- servicenow.itsm.instance 

- servicenow.itsm.sys_id 

- servicenow.itsm.number 

- servicenow.itsm.attachments 

- servicenow.itsm.incident_mapping 

options: 

state: 

description: 

- State of incident. 

- If I(state) value is C(on_hold), I(on_hold_reason) parameter must be filled in. 

- Default choices are C(new), C(in_progress), C(on_hold), C(resolved), C(closed), C(canceled), C(absent). 

One can override them by setting I(incident_mapping.state). 

type: str 

hold_reason: 

description: 

- Reason why incident is on hold. 

- Required if I(state) value is C(on_hold). 

- Default choices are C(awaiting_caller), C(awaiting_change), C(awaiting_problem), C(awaiting_vendor). 

One can override them by setting I(incident_mapping.hold_reason). 

type: str 

caller: 

description: 

- A person who reported or is affected by this incident. 

- Expected value for I(caller) is user id (usually in the form of 

C(first_name.last_name)). 

- Required if the incident does not exist yet. 

type: str 

short_description: 

description: 

- Short description of the incident. 

- Required if the incident does not exist yet. 

type: str 

description: 

description: 

- Long description of the incident with some more details. 

type: str 

impact: 

description: 

- The measure of the business criticality of the affected service. 

- Default choices are C(low), C(medium), C(high). 

One can override them by setting I(incident_mapping.impact). 

type: str 

urgency: 

description: 

- The extent to which resolution of an incident can bear delay. 

- Default choices are C(low), C(medium), C(high). 

One can override them by setting I(incident_mapping.urgency). 

type: str 

close_code: 

description: 

- Provide information on how the incident was resolved. 

choices: [ Solved (Work Around), Solved (Permanently), 

Solved Remotely (Work Around), Solved Remotely (Permanently), 

Not Solved (Not Reproducible), Not Solved (Too Costly), 

Closed/Resolved by Caller ] 

type: str 

close_notes: 

description: 

- Resolution notes added by the user who closed the incident. 

type: str 

other: 

description: 

- Optional remaining parameters. 

- For more information on optional parameters, refer to the ServiceNow 

create incident documentation at 

U(https://docs.servicenow.com/bundle/paris-it-service-management/page/product/incident-management/task/create-an-incident.html). 

type: dict 

""" 

 

EXAMPLES = r""" 

- name: Create incident 

servicenow.itsm.incident: 

instance: 

host: https://instance_id.service-now.com 

username: user 

password: pass 

 

state: new 

caller: some.user 

short_description: User is not receiving email 

description: User has been unable to receive email for the past 15 minutes 

attachments: 

- path: path/to/attachment.txt 

impact: low 

urgency: low 

 

other: 

expected_start: 2021-02-12 

 

- name: Change state of the incident 

servicenow.itsm.incident: 

instance: 

host: https://instance_id.service-now.com 

username: user 

password: pass 

 

state: in_progress 

number: INC0000001 

 

- name: Close incident 

servicenow.itsm.incident: 

instance: 

host: https://instance_id.service-now.com 

username: user 

password: pass 

 

state: closed 

number: INC0000001 

close_code: "Solved (Permanently)" 

close_notes: "Closed" 

 

- name: Delete incident 

servicenow.itsm.incident: 

instance: 

host: https://instance_id.service-now.com 

username: user 

password: pass 

 

state: absent 

number: INC0000001 

""" 

 

from ansible.module_utils.basic import AnsibleModule 

 

from ..module_utils import ( 

arguments, 

attachment, 

client, 

errors, 

table, 

utils, 

validation, 

) 

from ..module_utils.incident import PAYLOAD_FIELDS_MAPPING 

 

from ..module_utils.utils import get_mapper 

 

DIRECT_PAYLOAD_FIELDS = ( 

"state", 

"hold_reason", 

"short_description", 

"description", 

"impact", 

"urgency", 

"close_code", 

"close_notes", 

) 

 

 

def ensure_absent(module, table_client, attachment_client): 

mapper = get_mapper(module, "incident_mapping", PAYLOAD_FIELDS_MAPPING) 

query = utils.filter_dict(module.params, "sys_id", "number") 

incident = table_client.get_record("incident", query) 

 

if incident: 

attachment_client.delete_attached_records( 

"incident", 

incident["sys_id"], 

module.check_mode, 

) 

table_client.delete_record("incident", incident, module.check_mode) 

return True, None, dict(before=mapper.to_ansible(incident), after=None) 

 

return False, None, dict(before=None, after=None) 

 

 

def build_payload(module, table_client): 

payload = (module.params["other"] or {}).copy() 

payload.update(utils.filter_dict(module.params, *DIRECT_PAYLOAD_FIELDS)) 

 

if module.params["caller"]: 

user = table.find_user(table_client, module.params["caller"]) 

payload["caller_id"] = user["sys_id"] 

 

return payload 

 

 

def validate_params(params, incident=None): 

missing = [] 

if params["state"] in ("resolved", "closed"): 

missing.extend( 

validation.missing_from_params_and_remote( 

("close_code", "close_notes"), params, incident 

) 

) 

 

if missing: 

raise errors.ServiceNowError( 

"Missing required parameters {0}".format(", ".join(missing)) 

) 

 

 

def ensure_present(module, table_client, attachment_client): 

mapper = get_mapper(module, "incident_mapping", PAYLOAD_FIELDS_MAPPING) 

 

query = utils.filter_dict(module.params, "sys_id", "number") 

payload = build_payload(module, table_client) 

attachments = attachment.transform_metadata_list( 

module.params["attachments"], module.sha256 

) 

 

if not query: 

# User did not specify existing incident, so we need to create a new one. 

validate_params(module.params) 

new = mapper.to_ansible( 

table_client.create_record( 

"incident", mapper.to_snow(payload), module.check_mode 

) 

) 

 

# When we execute in check mode, new["sys_id"] is not defined. 

# In order to give users back as much info as possible, we fake the sys_id in the 

# next call. 

new["attachments"] = attachment_client.upload_records( 

"incident", 

new.get("sys_id", "N/A"), 

attachments, 

module.check_mode, 

) 

 

return True, new, dict(before=None, after=new) 

 

old = mapper.to_ansible(table_client.get_record("incident", query, must_exist=True)) 

 

old["attachments"] = attachment_client.list_records( 

dict(table_name="incident", table_sys_id=old["sys_id"]) 

) 

 

if utils.is_superset(old, payload) and not any( 

attachment.are_changed(old["attachments"], attachments) 

): 

# No change in parameters we are interested in - nothing to do. 

return False, old, dict(before=old, after=old) 

 

validate_params(module.params, old) 

new = mapper.to_ansible( 

table_client.update_record( 

"incident", mapper.to_snow(old), mapper.to_snow(payload), module.check_mode 

) 

) 

new["attachments"] = attachment_client.update_records( 

"incident", 

old["sys_id"], 

attachments, 

old["attachments"], 

module.check_mode, 

) 

 

return True, new, dict(before=old, after=new) 

 

 

def run(module, table_client, attachment_client): 

if module.params["state"] == "absent": 

return ensure_absent(module, table_client, attachment_client) 

return ensure_present(module, table_client, attachment_client) 

 

 

def main(): 

module_args = dict( 

arguments.get_spec( 

"instance", "sys_id", "number", "attachments", "incident_mapping" 

), 

state=dict( 

type="str", 

), 

hold_reason=dict( 

type="str", 

), 

caller=dict( 

type="str", 

), 

short_description=dict( 

type="str", 

), 

description=dict( 

type="str", 

), 

impact=dict( 

type="str", 

), 

urgency=dict( 

type="str", 

), 

close_code=dict( 

type="str", 

choices=[ 

"Solved (Work Around)", 

"Solved (Permanently)", 

"Solved Remotely (Work Around)", 

"Solved Remotely (Permanently)", 

"Not Solved (Not Reproducible)", 

"Not Solved (Too Costly)", 

"Closed/Resolved by Caller", 

], 

), 

close_notes=dict( 

type="str", 

), 

other=dict( 

type="dict", 

), 

) 

 

module = AnsibleModule( 

argument_spec=module_args, 

supports_check_mode=True, 

required_if=[ 

("state", "absent", ("sys_id", "number"), True), 

("state", "on_hold", ("hold_reason",)), 

], 

# If there is no sys id or number create a new ticket 

) 

 

try: 

snow_client = client.Client(**module.params["instance"]) 

table_client = table.TableClient(snow_client) 

attachment_client = attachment.AttachmentClient(snow_client) 

changed, record, diff = run(module, table_client, attachment_client) 

module.exit_json(changed=changed, record=record, diff=diff) 

except errors.ServiceNowError as e: 

module.fail_json(msg=str(e)) 

 

 

if __name__ == "__main__": 

main()