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
|
# setup
- set_fact: remote_tmp_dir_test={{remote_tmp_dir}}/test_replace
- name: make sure our testing sub-directory does not exist
file: path="{{ remote_tmp_dir_test }}" state=absent
- name: create our testing sub-directory
file: path="{{ remote_tmp_dir_test }}" state=directory
# tests
- name: create test files
copy:
content: |-
The quick brown fox jumps over the lazy dog.
We promptly judged antique ivory buckles for the next prize.
Jinxed wizards pluck ivy from the big quilt.
Jaded zombies acted quaintly but kept driving their oxen forward.
dest: "{{ remote_tmp_dir_test }}/pangrams.{{ item }}.txt"
with_sequence: start=0 end=6 format=%02x #increment as needed
## test `before` option
- name: remove all spaces before "quilt"
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.00.txt"
before: 'quilt'
regexp: ' '
register: replace_test0
- command: "cat {{ remote_tmp_dir_test }}/pangrams.00.txt"
register: replace_cat0
- name: validate before assertions
assert:
that:
- replace_test0 is successful
- replace_test0 is changed
- replace_cat0.stdout_lines[0] == 'Thequickbrownfoxjumpsoverthelazydog.'
- replace_cat0.stdout_lines[-1] == 'Jaded zombies acted quaintly but kept driving their oxen forward.'
## test `after` option
- name: remove all spaces after "promptly"
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.01.txt"
after: 'promptly'
regexp: ' '
register: replace_test1
- command: "cat {{ remote_tmp_dir_test }}/pangrams.01.txt"
register: replace_cat1
- name: validate after assertions
assert:
that:
- replace_test1 is successful
- replace_test1 is changed
- replace_cat1.stdout_lines[0] == 'The quick brown fox jumps over the lazy dog.'
- replace_cat1.stdout_lines[-1] == 'Jadedzombiesactedquaintlybutkeptdrivingtheiroxenforward.'
## test combined `before` and `after` options
- name: before "promptly" but after "quilt", replace every "e" with a "3"
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.02.txt"
before: 'promptly'
after: 'quilt'
regexp: 'e'
replace: '3'
register: replace_test2
- name: validate after+before assertions
assert:
that:
- replace_test2 is successful
- not replace_test2 is changed
- replace_test2.msg.startswith("Pattern for before/after params did not match the given file")
- name: before "quilt" but after "promptly", replace every "e" with a "3"
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.03.txt"
before: 'quilt'
after: 'promptly'
regexp: 'e'
replace: '3'
register: replace_test3
- command: "cat {{ remote_tmp_dir_test }}/pangrams.03.txt"
register: replace_cat3
- name: validate before+after assertions
assert:
that:
- replace_test3 is successful
- replace_test3 is changed
- replace_cat3.stdout_lines[1] == 'We promptly judg3d antiqu3 ivory buckl3s for th3 n3xt priz3.'
## test ^$ behavior in MULTILINE, and . behavior in absense of DOTALL
- name: quote everything between bof and eof
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.04.txt"
regexp: ^([\S\s]+)$
replace: '"\1"'
register: replace_test4_0
- command: "cat {{ remote_tmp_dir_test }}/pangrams.04.txt"
register: replace_cat4_0
- name: quote everything between bol and eol
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.04.txt"
regexp: ^(.+)$
replace: '"\1"'
register: replace_test4_1
- command: "cat {{ remote_tmp_dir_test }}/pangrams.04.txt"
register: replace_cat4_1
- name: validate before+after assertions
assert:
that:
- replace_test4_0 is successful
- replace_test4_0 is changed
- replace_test4_1 is successful
- replace_test4_1 is changed
- replace_cat4_0.stdout_lines[0] == '"The quick brown fox jumps over the lazy dog.'
- replace_cat4_0.stdout_lines[-1] == 'Jaded zombies acted quaintly but kept driving their oxen forward."'
- replace_cat4_1.stdout_lines[0] == '""The quick brown fox jumps over the lazy dog."'
- replace_cat4_1.stdout_lines[-1] == '"Jaded zombies acted quaintly but kept driving their oxen forward.""'
## test \b escaping in short and long form
- name: short form with unescaped word boundaries
replace: path="{{ remote_tmp_dir_test }}/pangrams.05.txt" regexp='\b(.+)\b' replace='"\1"'
register: replace_test5_0
- name: short form with escaped word boundaries
replace: path="{{ remote_tmp_dir_test }}/pangrams.05.txt" regexp='\\b(.+)\\b' replace='"\1"'
register: replace_test5_1
- command: "cat {{ remote_tmp_dir_test }}/pangrams.05.txt"
register: replace_cat5_1
- name: long form with unescaped word boundaries
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.05.txt"
regexp: '\b(.+)\b'
replace: '"\1"'
register: replace_test5_2
- command: "cat {{ remote_tmp_dir_test }}/pangrams.05.txt"
register: replace_cat5_2
- name: long form with escaped word boundaries
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.05.txt"
regexp: '\\b(.+)\\b'
replace: '"\1"'
register: replace_test5_3
- name: validate before+after assertions
assert:
that:
- not replace_test5_0 is changed
- replace_test5_1 is changed
- replace_test5_2 is changed
- not replace_test5_3 is changed
- replace_cat5_1.stdout_lines[0] == '"The quick brown fox jumps over the lazy dog".'
- replace_cat5_1.stdout_lines[-1] == '"Jaded zombies acted quaintly but kept driving their oxen forward".'
- replace_cat5_2.stdout_lines[0] == '""The quick brown fox jumps over the lazy dog"".'
- replace_cat5_2.stdout_lines[-1] == '""Jaded zombies acted quaintly but kept driving their oxen forward"".'
## test backup behaviors
- name: replacement with backup
replace:
path: "{{ remote_tmp_dir_test }}/pangrams.06.txt"
regexp: ^(.+)$
replace: '"\1"'
backup: true
register: replace_test6
- command: "cat {{ remote_tmp_dir_test }}/pangrams.06.txt"
register: replace_cat6_0
- command: "cat {{ replace_test6.backup_file }}"
register: replace_cat6_1
- name: validate backup
assert:
that:
- replace_test6 is successful
- replace_test6 is changed
- replace_test6.backup_file is search('/pangrams.06.txt.')
- replace_cat6_0.stdout != replace_cat6_1.stdout
## test filesystem failures
- name: fail on directory
replace:
path: "{{ remote_tmp_dir_test }}"
regexp: ^(.+)$
register: replace_test7_1
ignore_errors: true
- name: fail on missing file
replace:
path: "{{ remote_tmp_dir_test }}/missing_file.txt"
regexp: ^(.+)$
register: replace_test7_2
ignore_errors: true
- name: validate backup
assert:
that:
- replace_test7_1 is failure
- replace_test7_2 is failure
- replace_test7_1.msg.endswith(" is a directory !")
- replace_test7_2.msg.endswith(" does not exist !")
## test subsection replacement when before/after potentially match more than once
- name: test file for subsection replacement gone awry
copy:
content: |-
# start of group
0.0.0.0
127.0.0.1
127.0.1.1
# end of group
# start of group
0.0.0.0
127.0.0.1
127.0.1.1
# end of group
# start of group
0.0.0.0
127.0.0.1
127.0.1.1
# end of group
dest: "{{ remote_tmp_dir_test }}/addresses.txt"
- name: subsection madness
replace:
path: "{{ remote_tmp_dir_test }}/addresses.txt"
after: '# start of group'
before: '# end of group'
regexp: '0'
replace: '9'
register: replace_test8
- command: "cat {{ remote_tmp_dir_test }}/addresses.txt"
register: replace_cat8
- name: validate before+after assertions
assert:
that:
- replace_test8 is successful
- replace_test8 is changed
- replace_cat8.stdout_lines[1] == "9.9.9.9"
- replace_cat8.stdout_lines[7] == "0.0.0.0"
- replace_cat8.stdout_lines[13] == "0.0.0.0"
# https://github.com/ansible/ansible/issues/79364
- name: Handle bad escape character in regular expression
replace:
path: /dev/null
after: ^
before: $
regexp: \.
replace: '\D'
ignore_errors: true
register: replace_test9
- name: Validate the failure
assert:
that:
- replace_test9 is failure
- replace_test9.msg.startswith("Unable to process replace")
|