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

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> 

# 

# This file is part of Ansible 

# 

# Ansible is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# Ansible is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 

 

# Make coding more python3-ish 

from __future__ import (absolute_import, division, print_function) 

__metaclass__ = type 

 

from ansible.errors import AnsibleParserError 

from ansible.playbook.attribute import FieldAttribute 

from ansible.playbook.base import Base 

from ansible.playbook.become import Become 

from ansible.playbook.conditional import Conditional 

from ansible.playbook.helpers import load_list_of_tasks 

from ansible.playbook.role import Role 

from ansible.playbook.taggable import Taggable 

 

 

class Block(Base, Become, Conditional, Taggable): 

 

# main block fields containing the task lists 

_block = FieldAttribute(isa='list', default=[], inherit=False) 

_rescue = FieldAttribute(isa='list', default=[], inherit=False) 

_always = FieldAttribute(isa='list', default=[], inherit=False) 

 

# other fields 

_delegate_to = FieldAttribute(isa='string') 

_delegate_facts = FieldAttribute(isa='bool', default=False) 

 

# for future consideration? this would be functionally 

# similar to the 'else' clause for exceptions 

# _otherwise = FieldAttribute(isa='list') 

 

def __init__(self, play=None, parent_block=None, role=None, task_include=None, use_handlers=False, implicit=False): 

self._play = play 

self._role = role 

self._parent = None 

self._dep_chain = None 

self._use_handlers = use_handlers 

self._implicit = implicit 

 

# end of role flag 

self._eor = False 

 

if task_include: 

self._parent = task_include 

elif parent_block: 

self._parent = parent_block 

 

super(Block, self).__init__() 

 

def __repr__(self): 

return "BLOCK(uuid=%s)(id=%s)(parent=%s)" % (self._uuid, id(self), self._parent) 

 

def __eq__(self, other): 

'''object comparison based on _uuid''' 

return self._uuid == other._uuid 

 

def get_vars(self): 

''' 

Blocks do not store variables directly, however they may be a member 

of a role or task include which does, so return those if present. 

''' 

 

all_vars = self.vars.copy() 

 

if self._parent: 

all_vars.update(self._parent.get_vars()) 

 

return all_vars 

 

@staticmethod 

def load(data, play=None, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None): 

implicit = not Block.is_block(data) 

b = Block(play=play, parent_block=parent_block, role=role, task_include=task_include, use_handlers=use_handlers, implicit=implicit) 

return b.load_data(data, variable_manager=variable_manager, loader=loader) 

 

@staticmethod 

def is_block(ds): 

is_block = False 

if isinstance(ds, dict): 

for attr in ('block', 'rescue', 'always'): 

if attr in ds: 

is_block = True 

break 

return is_block 

 

def preprocess_data(self, ds): 

''' 

If a simple task is given, an implicit block for that single task 

is created, which goes in the main portion of the block 

''' 

 

if not Block.is_block(ds): 

if isinstance(ds, list): 

return super(Block, self).preprocess_data(dict(block=ds)) 

else: 

return super(Block, self).preprocess_data(dict(block=[ds])) 

 

return super(Block, self).preprocess_data(ds) 

 

def _load_block(self, attr, ds): 

try: 

return load_list_of_tasks( 

ds, 

play=self._play, 

block=self, 

role=self._role, 

task_include=None, 

variable_manager=self._variable_manager, 

loader=self._loader, 

use_handlers=self._use_handlers, 

) 

except AssertionError as e: 

raise AnsibleParserError("A malformed block was encountered while loading a block", obj=self._ds, orig_exc=e) 

 

def _load_rescue(self, attr, ds): 

try: 

return load_list_of_tasks( 

ds, 

play=self._play, 

block=self, 

role=self._role, 

task_include=None, 

variable_manager=self._variable_manager, 

loader=self._loader, 

use_handlers=self._use_handlers, 

) 

except AssertionError as e: 

raise AnsibleParserError("A malformed block was encountered while loading rescue.", obj=self._ds, orig_exc=e) 

 

def _load_always(self, attr, ds): 

try: 

return load_list_of_tasks( 

ds, 

play=self._play, 

block=self, 

role=self._role, 

task_include=None, 

variable_manager=self._variable_manager, 

loader=self._loader, 

use_handlers=self._use_handlers, 

) 

except AssertionError as e: 

raise AnsibleParserError("A malformed block was encountered while loading always", obj=self._ds, orig_exc=e) 

 

def get_dep_chain(self): 

if self._dep_chain is None: 

if self._parent: 

return self._parent.get_dep_chain() 

else: 

return None 

else: 

return self._dep_chain[:] 

 

def copy(self, exclude_parent=False, exclude_tasks=False): 

def _dupe_task_list(task_list, new_block): 

new_task_list = [] 

for task in task_list: 

new_task = task.copy(exclude_parent=True) 

174 ↛ 192line 174 didn't jump to line 192, because the condition on line 174 was never false if task._parent: 

new_task._parent = task._parent.copy(exclude_tasks=True) 

# go up the parentage tree until we find an 

# object without a parent and make this new 

# block their parent 

cur_obj = new_task 

while cur_obj._parent: 

181 ↛ 183line 181 didn't jump to line 183, because the condition on line 181 was never false if cur_obj._parent: 

prev_obj = cur_obj 

cur_obj = cur_obj._parent 

 

# Ensure that we don't make the new_block the parent of itself 

186 ↛ 190line 186 didn't jump to line 190, because the condition on line 186 was never false if cur_obj != new_block: 

cur_obj._parent = new_block 

else: 

# prev_obj._parent is cur_obj, to allow for mutability we need to use prev_obj 

prev_obj._parent = new_block 

else: 

new_task._parent = new_block 

new_task_list.append(new_task) 

return new_task_list 

 

new_me = super(Block, self).copy() 

new_me._play = self._play 

new_me._use_handlers = self._use_handlers 

new_me._eor = self._eor 

 

if self._dep_chain is not None: 

new_me._dep_chain = self._dep_chain[:] 

 

new_me._parent = None 

if self._parent and not exclude_parent: 

new_me._parent = self._parent.copy(exclude_tasks=exclude_tasks) 

 

if not exclude_tasks: 

new_me.block = _dupe_task_list(self.block or [], new_me) 

new_me.rescue = _dupe_task_list(self.rescue or [], new_me) 

new_me.always = _dupe_task_list(self.always or [], new_me) 

 

new_me._role = None 

if self._role: 

new_me._role = self._role 

 

new_me.validate() 

return new_me 

 

def serialize(self): 

''' 

Override of the default serialize method, since when we're serializing 

a task we don't want to include the attribute list of tasks. 

''' 

 

data = dict() 

for attr in self._valid_attrs: 

if attr not in ('block', 'rescue', 'always'): 

data[attr] = getattr(self, attr) 

 

data['dep_chain'] = self.get_dep_chain() 

data['eor'] = self._eor 

 

if self._role is not None: 

data['role'] = self._role.serialize() 

if self._parent is not None: 

data['parent'] = self._parent.copy(exclude_tasks=True).serialize() 

data['parent_type'] = self._parent.__class__.__name__ 

 

return data 

 

def deserialize(self, data): 

''' 

Override of the default deserialize method, to match the above overridden 

serialize method 

''' 

 

# import is here to avoid import loops 

from ansible.playbook.task_include import TaskInclude 

from ansible.playbook.handler_task_include import HandlerTaskInclude 

 

# we don't want the full set of attributes (the task lists), as that 

# would lead to a serialize/deserialize loop 

for attr in self._valid_attrs: 

if attr in data and attr not in ('block', 'rescue', 'always'): 

setattr(self, attr, data.get(attr)) 

 

self._dep_chain = data.get('dep_chain', None) 

self._eor = data.get('eor', False) 

 

# if there was a serialized role, unpack it too 

role_data = data.get('role') 

if role_data: 

r = Role() 

r.deserialize(role_data) 

self._role = r 

 

parent_data = data.get('parent') 

if parent_data: 

parent_type = data.get('parent_type') 

if parent_type == 'Block': 

p = Block() 

elif parent_type == 'TaskInclude': 

p = TaskInclude() 

elif parent_type == 'HandlerTaskInclude': 

p = HandlerTaskInclude() 

p.deserialize(parent_data) 

self._parent = p 

self._dep_chain = self._parent.get_dep_chain() 

 

def set_loader(self, loader): 

self._loader = loader 

283 ↛ 284line 283 didn't jump to line 284, because the condition on line 283 was never true if self._parent: 

self._parent.set_loader(loader) 

285 ↛ 286line 285 didn't jump to line 286, because the condition on line 285 was never true elif self._role: 

self._role.set_loader(loader) 

 

dep_chain = self.get_dep_chain() 

289 ↛ 290line 289 didn't jump to line 290, because the condition on line 289 was never true if dep_chain: 

for dep in dep_chain: 

dep.set_loader(loader) 

 

def _get_parent_attribute(self, attr, extend=False, prepend=False): 

''' 

Generic logic to get the attribute or parent attribute for a block value. 

''' 

 

extend = self._valid_attrs[attr].extend 

prepend = self._valid_attrs[attr].prepend 

try: 

value = self._attributes[attr] 

if self._parent and (value is None or extend): 

try: 

if getattr(self._parent, 'statically_loaded', True): 

305 ↛ 308line 305 didn't jump to line 308, because the condition on line 305 was never false if hasattr(self._parent, '_get_parent_attribute'): 

parent_value = self._parent._get_parent_attribute(attr) 

else: 

parent_value = self._parent._attributes.get(attr, None) 

if extend: 

value = self._extend_value(value, parent_value, prepend) 

else: 

value = parent_value 

except AttributeError: 

pass 

if self._role and (value is None or extend): 

try: 

317 ↛ 318line 317 didn't jump to line 318, because the condition on line 317 was never true if hasattr(self._role, '_get_parent_attribute'): 

parent_value = self._role.get_parent_attribute(attr) 

else: 

parent_value = self._role._attributes.get(attr, None) 

if extend: 

value = self._extend_value(value, parent_value, prepend) 

else: 

value = parent_value 

 

dep_chain = self.get_dep_chain() 

if dep_chain and (value is None or extend): 

dep_chain.reverse() 

for dep in dep_chain: 

330 ↛ 331line 330 didn't jump to line 331, because the condition on line 330 was never true if hasattr(dep, '_get_parent_attribute'): 

dep_value = dep._get_parent_attribute(attr) 

else: 

dep_value = dep._attributes.get(attr, None) 

if extend: 

value = self._extend_value(value, dep_value, prepend) 

else: 

value = dep_value 

 

339 ↛ 340line 339 didn't jump to line 340, because the condition on line 339 was never true if value is not None and not extend: 

break 

except AttributeError: 

pass 

if self._play and (value is None or extend): 

try: 

play_value = self._play._attributes.get(attr, None) 

if play_value is not None: 

347 ↛ 350line 347 didn't jump to line 350, because the condition on line 347 was never false if extend: 

value = self._extend_value(value, play_value, prepend) 

else: 

value = play_value 

except AttributeError: 

pass 

except KeyError: 

pass 

 

return value 

 

def filter_tagged_tasks(self, play_context, all_vars): 

''' 

Creates a new block, with task lists filtered based on the tags contained 

within the play_context object. 

''' 

 

def evaluate_and_append_task(target): 

tmp_list = [] 

for task in target: 

if isinstance(task, Block): 

tmp_list.append(evaluate_block(task)) 

369 ↛ 366line 369 didn't jump to line 366, because the condition on line 369 was never false elif (task.action == 'meta' or 

(task.action == 'include' and task.evaluate_tags([], play_context.skip_tags, all_vars=all_vars)) or 

task.evaluate_tags(play_context.only_tags, play_context.skip_tags, all_vars=all_vars)): 

tmp_list.append(task) 

return tmp_list 

 

def evaluate_block(block): 

new_block = self.copy(exclude_tasks=True) 

new_block.block = evaluate_and_append_task(block.block) 

new_block.rescue = evaluate_and_append_task(block.rescue) 

new_block.always = evaluate_and_append_task(block.always) 

return new_block 

 

return evaluate_block(self) 

 

def has_tasks(self): 

return len(self.block) > 0 or len(self.rescue) > 0 or len(self.always) > 0 

 

def get_include_params(self): 

if self._parent: 

return self._parent.get_include_params() 

else: 

return dict() 

 

def all_parents_static(self): 

''' 

Determine if all of the parents of this block were statically loaded 

or not. Since Task/TaskInclude objects may be in the chain, they simply 

call their parents all_parents_static() method. Only Block objects in 

the chain check the statically_loaded value of the parent. 

''' 

from ansible.playbook.task_include import TaskInclude 

401 ↛ 402line 401 didn't jump to line 402, because the condition on line 401 was never true if self._parent: 

if isinstance(self._parent, TaskInclude) and not self._parent.statically_loaded: 

return False 

return self._parent.all_parents_static() 

 

return True 

 

def get_first_parent_include(self): 

from ansible.playbook.task_include import TaskInclude 

if self._parent: 

if isinstance(self._parent, TaskInclude): 

return self._parent 

return self._parent.get_first_parent_include() 

return None