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

# (c) 2013, seth vidal <skvidal@fedoraproject.org> red hat, inc 

# (c) 2017 Ansible Project 

# 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 = """ 

lookup: first_found 

author: Seth Vidal <skvidal@fedoraproject.org> 

version_added: historical 

short_description: return first file found from list 

description: 

- this lookup checks a list of files and paths and returns the full path to the first combination found. 

options: 

_terms: 

description: list of file names 

required: True 

paths: 

description: list of paths in which to look for the files 

""" 

 

EXAMPLES = """ 

- name: show first existin file 

debug: var=item 

with_first_found: 

- "/path/to/foo.txt" 

- "bar.txt" # will be looked in files/ dir relative to play or in role 

- "/path/to/biz.txt" 

 

- name: copy first existing file found to /some/file 

copy: src={{item}} dest=/some/file 

with_first_found: 

- foo 

- "{{inventory_hostname}} 

- bar 

 

- name: same copy but specific paths 

copy: src={{item}} dest=/some/file 

with_first_found: 

- files: 

- foo 

- "{{inventory_hostname}} 

- bar 

paths: 

- /tmp/production 

- /tmp/staging 

 

- name: INTERFACES | Create Ansible header for /etc/network/interfaces 

template: 

src: "{{ item }}" 

dest: "/etc/foo.conf" 

with_first_found: 

- "{{ ansible_virtualization_type }}_foo.conf" 

- "default_foo.conf" 

""" 

 

RETURN = """ 

_raw: 

description: 

- path to file found 

""" 

import os 

 

from jinja2.exceptions import UndefinedError 

 

from ansible.errors import AnsibleFileNotFound, AnsibleLookupError, AnsibleUndefinedVariable 

from ansible.module_utils.six import string_types 

from ansible.module_utils.parsing.convert_bool import boolean 

from ansible.plugins.lookup import LookupBase 

 

 

class LookupModule(LookupBase): 

 

def run(self, terms, variables, **kwargs): 

 

anydict = False 

skip = False 

 

for term in terms: 

80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true if isinstance(term, dict): 

anydict = True 

 

total_search = [] 

84 ↛ 85line 84 didn't jump to line 85, because the condition on line 84 was never true if anydict: 

for term in terms: 

if isinstance(term, dict): 

files = term.get('files', []) 

paths = term.get('paths', []) 

skip = boolean(term.get('skip', False), strict=False) 

 

filelist = files 

if isinstance(files, string_types): 

files = files.replace(',', ' ') 

files = files.replace(';', ' ') 

filelist = files.split(' ') 

 

pathlist = paths 

if paths: 

if isinstance(paths, string_types): 

paths = paths.replace(',', ' ') 

paths = paths.replace(':', ' ') 

paths = paths.replace(';', ' ') 

pathlist = paths.split(' ') 

 

if not pathlist: 

total_search = filelist 

else: 

for path in pathlist: 

for fn in filelist: 

f = os.path.join(path, fn) 

total_search.append(f) 

else: 

total_search.append(term) 

else: 

total_search = self._flatten(terms) 

 

117 ↛ 129line 117 didn't jump to line 129, because the loop on line 117 didn't complete for fn in total_search: 

try: 

fn = self._templar.template(fn) 

except (AnsibleUndefinedVariable, UndefinedError): 

continue 

 

# get subdir if set by task executor, default to files otherwise 

subdir = getattr(self, '_subdir', 'files') 

path = None 

path = self.find_file_in_search_path(variables, subdir, fn, ignore_missing=True) 

127 ↛ 117line 127 didn't jump to line 117, because the condition on line 127 was never false if path is not None: 

return [path] 

if skip: 

return [] 

raise AnsibleLookupError("No file was found when using with_first_found. Use the 'skip: true' option to allow this task to be skipped if no " 

"files are found")