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
|
- name: using deprecated syntax but missing keyword
set_fact:
this_will_error: "{{ lookup('csvfile', 'file=people.csv, delimiter=, col=1') }}"
ignore_errors: yes
register: no_keyword
- name: using modern syntax but missing keyword
set_fact:
this_will_error: "{{ lookup('csvfile', file='people.csv', delimiter=' ', col=1) }}"
ignore_errors: yes
register: modern_no_keyword
- name: extra arg in k=v syntax (deprecated)
set_fact:
this_will_error: "{{ lookup('csvfile', 'foo file=people.csv delimiter=, col=1 thisarg=doesnotexist') }}"
ignore_errors: yes
register: invalid_arg
- name: extra arg in config syntax
set_fact:
this_will_error: "{{ lookup('csvfile', 'foo', file='people.csv', delimiter=',' col=1, thisarg='doesnotexist') }}"
ignore_errors: yes
register: invalid_arg2
- set_fact:
this_will_error: "{{ lookup('csvfile', 'foo', file='doesnotexist', delimiter=',', col=1) }}"
ignore_errors: yes
register: missing_file
- name: Make sure we failed above
assert:
that:
- no_keyword is failed
- >
"Search key is required but was not found" in no_keyword.msg
- modern_no_keyword is failed
- >
"Search key is required but was not found" in modern_no_keyword.msg
- invalid_arg is failed
- invalid_arg2 is failed
- >
"is not a valid option" in invalid_arg.msg
- missing_file is failed
- >
"need string or buffer" in missing_file.msg or
"expected str, bytes or os.PathLike object" in missing_file.msg or
"No such file or directory" in missing_file.msg
- name: Check basic comma-separated file
assert:
that:
- lookup('csvfile', 'Smith', file='people.csv', delimiter=',', col=1) == "Jane"
- lookup('csvfile', 'Jane', file='people.csv', delimiter=',', col=0, keycol=1) == "Smith"
- lookup('csvfile', 'German von Lastname file=people.csv delimiter=, col=1') == "Demo"
- name: Check tab-separated file
assert:
that:
- lookup('csvfile', 'electronics file=tabs.csv delimiter=TAB col=1') == "tvs"
- "lookup('csvfile', 'fruit', file='tabs.csv', delimiter='TAB', col=1) == 'bananas'"
- lookup('csvfile', 'fruit file=tabs.csv delimiter="\t" col=1') == "bananas"
- lookup('csvfile', 'electronics', 'fruit', file='tabs.csv', delimiter='\t', col=1) == "tvs,bananas"
- lookup('csvfile', 'electronics', 'fruit', file='tabs.csv', delimiter='\t', col=1, wantlist=True) == ["tvs", "bananas"]
- name: Check \x1a-separated file
assert:
that:
- lookup('csvfile', 'again file=x1a.csv delimiter=\x1a col=1') == "because"
- name: Check CSV file with CRLF line endings
assert:
that:
- lookup('csvfile', 'this file file=crlf.csv delimiter=, col=2') == "crlf"
- lookup('csvfile', 'ansible file=crlf.csv delimiter=, col=1') == "parses"
- name: Check file with multi word filename
assert:
that:
- lookup('csvfile', 'maybe file="cool list of things.csv" delimiter=, col=3') == "work"
- name: Test default behavior
assert:
that:
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2') == []
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2 default=what?') == "what?"
- name: Pass wrong terms value fails parse_kv
set_fact:
people_col_2: '{{ lookup("csvfile", "notfound file=people.csv delimiter=, col=2, default=what?") }}'
ignore_errors: yes
register: people_col_2_r
- name: Check if wrong terms value fails parse_kv
assert:
that:
- people_col_2_r.failed
- "'Invalid type for configuration option' in people_col_2_r.msg"
# NOTE: For historical reasons, this is correct; quotes in the search field must
# be treated literally as if they appear (escaped as required) in the field in the
# file. They cannot be used to surround the search text in general.
- name: Test quotes in the search field
assert:
that:
- lookup('csvfile', '"The Rock" Johnson file=people.csv delimiter=, col=1') == "Dwayne"
|