- name: Create a new file and add block
  ansible.builtin.blockinfile:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
    block: |
      This is a block added to the beginning of the file.
      Line BOF 1
      Line BOF 2
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR BOF"
    insertbefore: BOF
    create: yes
    encoding: cp273
  register: add_block_bof

- name: add block at end of file
  ansible.builtin.blockinfile:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
    block: |
      This is a block added to the end of the file.
      Line EOF 1
      Line EOF 2
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR EOF"
    insertafter: EOF
    encoding: cp273
  register: add_block_eof

- name: stat the new file
  stat:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
  register: result1

- name: check idempotency by adding same block at end of file again
  ansible.builtin.blockinfile:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
    block: |
      This is a block added to the end of the file.
      Line EOF 1
      Line EOF 2
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR EOF"
    insertafter: EOF
    encoding: cp273
  register: add_block_eof_1

- name: assert the results for adding block EOF and BOF
  assert:
    that:
      - add_block_bof is changed and add_block_eof is changed
      - 'add_block_bof.msg == "File created"'
      - 'add_block_eof.msg == "Block inserted"'
      - result1.stat.exists
      - result1.stat.checksum == '724f92d56c2bdaf8e701359e71091bce898af988'
      - add_block_eof_1 is not changed

- name: Add block after Line
  blockinfile:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
    insertafter: Line BOF 1
    block: |
      This is block added after Line BOF 1
      Line Added After BOF 1 1
      Line Added After BOF 1 2
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR AFTER_LINE"  
    encoding: cp273  
  register: insert_after_line

- name: Add block Before Line
  blockinfile:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
    insertbefore: Line EOF 2
    block: |
      This is block added Before Line EOF 2
      Line Added Before EOF 1 1
      Line Added Before EOF 1 2
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR BEFORE_LINE"  
    encoding: cp273  
  register: insert_before_line

- name: stat the new file
  stat:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
  register: result1  

- name: assert the results for Insert After and Before line
  assert:
    that:
      - insert_after_line is changed and insert_before_line is changed
      - 'insert_after_line.msg == "Block inserted"'
      - 'insert_before_line.msg == "Block inserted"'
      - result1.stat.exists
      - result1.stat.checksum == '11af61de9ed9e9182eee8a2c271921d0dd1992c9'

- name: Delete the custom Block 
  ansible.builtin.blockinfile:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR EOF"
    state: absent
    encoding: cp273
  register: delete_custom_block

- name: stat the new file
  stat:
    path: "{{ remote_tmp_dir_test }}/encoding_file.txt"
  register: result1  

- name: assert the results for Insert After and Before with Regexp
  assert:
    that:
      - delete_custom_block is changed
      - 'delete_custom_block.msg == "Block removed"'
      - result1.stat.exists
      - result1.stat.checksum == '6e192ae0a60a7f0e6299a2918b6e6708a59b8183'
