| 1 |
- name: using deprecated syntax but missing keyword |
| 2 |
set_fact: |
| 3 |
this_will_error: "{{ lookup('csvfile', 'file=people.csv, delimiter=, col=1') }}" |
| 4 |
ignore_errors: yes |
| 5 |
register: no_keyword |
| 6 |
|
| 7 |
- name: using modern syntax but missing keyword |
| 8 |
set_fact: |
| 9 |
this_will_error: "{{ lookup('csvfile', file='people.csv', delimiter=' ', col=1) }}" |
| 10 |
ignore_errors: yes |
| 11 |
register: modern_no_keyword |
| 12 |
|
| 13 |
- name: extra arg in k=v syntax (deprecated) |
| 14 |
set_fact: |
| 15 |
this_will_error: "{{ lookup('csvfile', 'foo file=people.csv delimiter=, col=1 thisarg=doesnotexist') }}" |
| 16 |
ignore_errors: yes |
| 17 |
register: invalid_arg |
| 18 |
|
| 19 |
- name: extra arg in config syntax |
| 20 |
set_fact: |
| 21 |
this_will_error: "{{ lookup('csvfile', 'foo', file='people.csv', delimiter=',' col=1, thisarg='doesnotexist') }}" |
| 22 |
ignore_errors: yes |
| 23 |
register: invalid_arg2 |
| 24 |
|
| 25 |
- set_fact: |
| 26 |
this_will_error: "{{ lookup('csvfile', 'foo', file='doesnotexist', delimiter=',', col=1) }}" |
| 27 |
ignore_errors: yes |
| 28 |
register: missing_file |
| 29 |
|
| 30 |
- name: Make sure we failed above |
| 31 |
assert: |
| 32 |
that: |
| 33 |
- no_keyword is failed |
| 34 |
- > |
| 35 |
"Search key is required but was not found" in no_keyword.msg |
| 36 |
- modern_no_keyword is failed |
| 37 |
- > |
| 38 |
"Search key is required but was not found" in modern_no_keyword.msg |
| 39 |
- invalid_arg is failed |
| 40 |
- invalid_arg2 is failed |
| 41 |
- > |
| 42 |
"is not a valid option" in invalid_arg.msg |
| 43 |
- missing_file is failed |
| 44 |
- > |
| 45 |
"need string or buffer" in missing_file.msg or |
| 46 |
"expected str, bytes or os.PathLike object" in missing_file.msg or |
| 47 |
"No such file or directory" in missing_file.msg |
| 48 |
|
| 49 |
- name: Check basic comma-separated file |
| 50 |
assert: |
| 51 |
that: |
| 52 |
- lookup('csvfile', 'Smith', file='people.csv', delimiter=',', col=1) == "Jane" |
| 53 |
- lookup('csvfile', 'Jane', file='people.csv', delimiter=',', col=0, keycol=1) == "Smith" |
| 54 |
- lookup('csvfile', 'German von Lastname file=people.csv delimiter=, col=1') == "Demo" |
| 55 |
|
| 56 |
- debug: |
| 57 |
msg: output {{ lookup('csvfile', 'fruit', file='tabs.csv', delimiter='TAB', col=1) }} |
| 58 |
|
| 59 |
- name: Check tab-separated file |
| 60 |
assert: |
| 61 |
that: |
| 62 |
- lookup('csvfile', 'electronics file=tabs.csv delimiter=TAB col=1') == "tvs" |
| 63 |
- "lookup('csvfile', 'fruit', file='tabs.csv', delimiter='TAB', col=1) == 'bananas'" |
| 64 |
- lookup('csvfile', 'fruit file=tabs.csv delimiter="\t" col=1') == "bananas" |
| 65 |
- lookup('csvfile', 'electronics', 'fruit', file='tabs.csv', delimiter='\t', col=1) == "tvs,bananas" |
| 66 |
- lookup('csvfile', 'electronics', 'fruit', file='tabs.csv', delimiter='\t', col=1, wantlist=True) == ["tvs", "bananas"] |
| 67 |
|
| 68 |
- name: Check \x1a-separated file |
| 69 |
assert: |
| 70 |
that: |
| 71 |
- lookup('csvfile', 'again file=x1a.csv delimiter=\x1a col=1') == "because" |
| 72 |
|
| 73 |
- name: Check CSV file with CRLF line endings |
| 74 |
assert: |
| 75 |
that: |
| 76 |
- lookup('csvfile', 'this file file=crlf.csv delimiter=, col=2') == "crlf" |
| 77 |
- lookup('csvfile', 'ansible file=crlf.csv delimiter=, col=1') == "parses" |
| 78 |
|
| 79 |
- name: Check file with multi word filename |
| 80 |
assert: |
| 81 |
that: |
| 82 |
- lookup('csvfile', 'maybe file="cool list of things.csv" delimiter=, col=3') == "work" |
| 83 |
|
| 84 |
- name: Test default behavior |
| 85 |
assert: |
| 86 |
that: |
| 87 |
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2') == [] |
| 88 |
- lookup('csvfile', 'notfound file=people.csv delimiter=, col=2 default=what?') == "what?" |
| 89 |
- lookup('csvfile', 'notfound', file='people.csv', delimiter=',', col=2, default=['a', 'b']) == 'a,b' |
| 90 |
|
| 91 |
- name: Pass wrong terms value fails parse_kv |
| 92 |
set_fact: |
| 93 |
people_col_2: '{{ lookup("csvfile", "notfound file=people.csv delimiter=, col=2, default=what?") }}' |
| 94 |
ignore_errors: yes |
| 95 |
register: people_col_2_r |
| 96 |
|
| 97 |
- name: Check if wrong terms value fails parse_kv |
| 98 |
assert: |
| 99 |
that: |
| 100 |
- people_col_2_r.failed |
| 101 |
- people_col_2_r.msg is contains "has an invalid value" |
| 102 |
|
| 103 |
# NOTE: For historical reasons, this is correct; quotes in the search field must |
| 104 |
# be treated literally as if they appear (escaped as required) in the field in the |
| 105 |
# file. They cannot be used to surround the search text in general. |
| 106 |
- name: Test quotes in the search field |
| 107 |
assert: |
| 108 |
that: |
| 109 |
- lookup('csvfile', '"The Rock" Johnson file=people.csv delimiter=, col=1') == "Dwayne" |