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

# (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 

 

import os 

 

from ansible.playbook.task_include import TaskInclude 

from ansible.playbook.role_include import IncludeRole 

from ansible.template import Templar 

 

try: 

from __main__ import display 

except ImportError: 

from ansible.utils.display import Display 

display = Display() 

 

 

class IncludedFile: 

 

def __init__(self, filename, args, task, is_role=False): 

self._filename = filename 

self._args = args 

self._task = task 

self._hosts = [] 

self._is_role = is_role 

 

def add_host(self, host): 

45 ↛ exitline 45 didn't return from function 'add_host', because the condition on line 45 was never false if host not in self._hosts: 

self._hosts.append(host) 

 

def __eq__(self, other): 

return other._filename == self._filename and other._args == self._args 

 

def __repr__(self): 

return "%s (%s): %s" % (self._filename, self._args, self._hosts) 

 

@staticmethod 

def process_include_results(results, iterator, loader, variable_manager): 

included_files = [] 

 

for res in results: 

 

original_host = res._host 

original_task = res._task 

 

if original_task.action in ('include', 'include_tasks', 'include_role'): 

64 ↛ 69line 64 didn't jump to line 69, because the condition on line 64 was never false if original_task.loop: 

65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true if 'results' not in res._result: 

continue 

include_results = res._result['results'] 

else: 

include_results = [res._result] 

 

for include_result in include_results: 

# if the task result was skipped or failed, continue 

73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true if 'skipped' in include_result and include_result['skipped'] or 'failed' in include_result and include_result['failed']: 

continue 

 

task_vars = variable_manager.get_vars(play=iterator._play, host=original_host, task=original_task) 

templar = Templar(loader=loader, variables=task_vars) 

 

include_variables = include_result.get('include_variables', dict()) 

loop_var = 'item' 

index_var = None 

82 ↛ 85line 82 didn't jump to line 85, because the condition on line 82 was never false if original_task.loop_control: 

loop_var = original_task.loop_control.loop_var 

index_var = original_task.loop_control.index_var 

85 ↛ 87line 85 didn't jump to line 87, because the condition on line 85 was never false if loop_var in include_result: 

task_vars[loop_var] = include_variables[loop_var] = include_result[loop_var] 

87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never true if index_var and index_var in include_result: 

task_vars[index_var] = include_variables[index_var] = include_result[index_var] 

 

90 ↛ 145line 90 didn't jump to line 145, because the condition on line 90 was never false if original_task.action in ('include', 'include_tasks'): 

include_file = None 

92 ↛ 134line 92 didn't jump to line 134, because the condition on line 92 was never false if original_task: 

93 ↛ 94line 93 didn't jump to line 94, because the condition on line 93 was never true if original_task.static: 

continue 

 

96 ↛ 134line 96 didn't jump to line 134, because the condition on line 96 was never false if original_task._parent: 

# handle relative includes by walking up the list of parent include 

# tasks and checking the relative result to see if it exists 

parent_include = original_task._parent 

cumulative_path = None 

101 ↛ 134line 101 didn't jump to line 134, because the condition on line 101 was never false while parent_include is not None: 

if not isinstance(parent_include, TaskInclude): 

parent_include = parent_include._parent 

continue 

105 ↛ 106line 105 didn't jump to line 106, because the condition on line 105 was never true if isinstance(parent_include, IncludeRole): 

parent_include_dir = parent_include._role_path 

else: 

parent_include_dir = os.path.dirname(templar.template(parent_include.args.get('_raw_params'))) 

109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never true if cumulative_path is not None and not os.path.isabs(cumulative_path): 

cumulative_path = os.path.join(parent_include_dir, cumulative_path) 

else: 

cumulative_path = parent_include_dir 

include_target = templar.template(include_result['include']) 

114 ↛ 127line 114 didn't jump to line 127, because the condition on line 114 was never false if original_task._role: 

new_basedir = os.path.join(original_task._role._role_path, 'tasks', cumulative_path) 

candidates = [loader.path_dwim_relative(original_task._role._role_path, 'tasks', include_target), 

loader.path_dwim_relative(new_basedir, 'tasks', include_target)] 

118 ↛ 129line 118 didn't jump to line 129, because the loop on line 118 didn't complete for include_file in candidates: 

try: 

# may throw OSError 

os.stat(include_file) 

# or select the task file if it exists 

break 

except OSError: 

pass 

else: 

include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target) 

 

129 ↛ 132line 129 didn't jump to line 132, because the condition on line 129 was never false if os.path.exists(include_file): 

break 

else: 

parent_include = parent_include._parent 

 

134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true if include_file is None: 

if original_task._role: 

include_target = templar.template(include_result['include']) 

include_file = loader.path_dwim_relative(original_task._role._role_path, 'tasks', include_target) 

else: 

include_file = loader.path_dwim(include_result['include']) 

 

include_file = templar.template(include_file) 

inc_file = IncludedFile(include_file, include_variables, original_task) 

else: 

# template the included role's name here 

role_name = include_variables.get('name', include_variables.get('role', None)) 

if role_name is not None: 

role_name = templar.template(role_name) 

 

original_task._role_name = role_name 

for from_arg in original_task.FROM_ARGS: 

if from_arg in include_variables: 

from_key = from_arg.replace('_from', '') 

original_task._from_files[from_key] = templar.template(include_variables[from_arg]) 

 

inc_file = IncludedFile("role", include_variables, original_task, is_role=True) 

 

try: 

pos = included_files.index(inc_file) 

inc_file = included_files[pos] 

except ValueError: 

included_files.append(inc_file) 

 

inc_file.add_host(original_host) 

 

return included_files