| 1 |
- name: Create a new file and add block |
| 2 |
ansible.builtin.blockinfile: |
| 3 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 4 |
block: | |
| 5 |
This is a block added to the beginning of the file. |
| 6 |
Line BOF 1 |
| 7 |
Line BOF 2 |
| 8 |
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR BOF" |
| 9 |
insertbefore: BOF |
| 10 |
create: yes |
| 11 |
encoding: cp273 |
| 12 |
register: add_block_bof |
| 13 |
|
| 14 |
- name: add block at end of file |
| 15 |
ansible.builtin.blockinfile: |
| 16 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 17 |
block: | |
| 18 |
This is a block added to the end of the file. |
| 19 |
Line EOF 1 |
| 20 |
Line EOF 2 |
| 21 |
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR EOF" |
| 22 |
insertafter: EOF |
| 23 |
encoding: cp273 |
| 24 |
register: add_block_eof |
| 25 |
|
| 26 |
- name: stat the new file |
| 27 |
stat: |
| 28 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 29 |
register: result1 |
| 30 |
|
| 31 |
- name: check idempotency by adding same block at end of file again |
| 32 |
ansible.builtin.blockinfile: |
| 33 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 34 |
block: | |
| 35 |
This is a block added to the end of the file. |
| 36 |
Line EOF 1 |
| 37 |
Line EOF 2 |
| 38 |
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR EOF" |
| 39 |
insertafter: EOF |
| 40 |
encoding: cp273 |
| 41 |
register: add_block_eof_1 |
| 42 |
|
| 43 |
- name: assert the results for adding block EOF and BOF |
| 44 |
assert: |
| 45 |
that: |
| 46 |
- add_block_bof is changed and add_block_eof is changed |
| 47 |
- 'add_block_bof.msg == "File created"' |
| 48 |
- 'add_block_eof.msg == "Block inserted"' |
| 49 |
- result1.stat.exists |
| 50 |
- result1.stat.checksum == '724f92d56c2bdaf8e701359e71091bce898af988' |
| 51 |
- add_block_eof_1 is not changed |
| 52 |
|
| 53 |
- name: Add block after Line |
| 54 |
blockinfile: |
| 55 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 56 |
insertafter: Line BOF 1 |
| 57 |
block: | |
| 58 |
This is block added after Line BOF 1 |
| 59 |
Line Added After BOF 1 1 |
| 60 |
Line Added After BOF 1 2 |
| 61 |
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR AFTER_LINE" |
| 62 |
encoding: cp273 |
| 63 |
register: insert_after_line |
| 64 |
|
| 65 |
- name: Add block Before Line |
| 66 |
blockinfile: |
| 67 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 68 |
insertbefore: Line EOF 2 |
| 69 |
block: | |
| 70 |
This is block added Before Line EOF 2 |
| 71 |
Line Added Before EOF 1 1 |
| 72 |
Line Added Before EOF 1 2 |
| 73 |
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR BEFORE_LINE" |
| 74 |
encoding: cp273 |
| 75 |
register: insert_before_line |
| 76 |
|
| 77 |
- name: stat the new file |
| 78 |
stat: |
| 79 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 80 |
register: result1 |
| 81 |
|
| 82 |
- name: assert the results for Insert After and Before line |
| 83 |
assert: |
| 84 |
that: |
| 85 |
- insert_after_line is changed and insert_before_line is changed |
| 86 |
- 'insert_after_line.msg == "Block inserted"' |
| 87 |
- 'insert_before_line.msg == "Block inserted"' |
| 88 |
- result1.stat.exists |
| 89 |
- result1.stat.checksum == '11af61de9ed9e9182eee8a2c271921d0dd1992c9' |
| 90 |
|
| 91 |
- name: Delete the custom Block |
| 92 |
ansible.builtin.blockinfile: |
| 93 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 94 |
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR EOF" |
| 95 |
state: absent |
| 96 |
encoding: cp273 |
| 97 |
register: delete_custom_block |
| 98 |
|
| 99 |
- name: stat the new file |
| 100 |
stat: |
| 101 |
path: "{{ remote_tmp_dir_test }}/encoding_file.txt" |
| 102 |
register: result1 |
| 103 |
|
| 104 |
- name: assert the results for Insert After and Before with Regexp |
| 105 |
assert: |
| 106 |
that: |
| 107 |
- delete_custom_block is changed |
| 108 |
- 'delete_custom_block.msg == "Block removed"' |
| 109 |
- result1.stat.exists |
| 110 |
- result1.stat.checksum == '6e192ae0a60a7f0e6299a2918b6e6708a59b8183' |