---
- name: Add domain/search/nameservers/options to resolv.conf
  blockinfile:
    path: "{{resolvconffile}}"
    block: |-
      {% for item in [domainentry] + [searchentries] + nameserverentries.split(',') -%}
      {{ item }}
      {% endfor %}
      options ndots:{{ ndots }}
      options timeout:2
      options attempts:2
    state: present
    insertbefore: BOF
    create: yes
    backup: yes
    marker: "# Ansible entries {mark}"
  notify: Preinstall | restart network

- name: Remove search/domain/nameserver options before block
  replace:
    dest: "{{item[0]}}"
    regexp: '^{{ item[1] }}[^#]*(?=# Ansible entries BEGIN)'
    backup: yes
    follow: yes
  with_nested:
    - "{{ [resolvconffile, base|default(''), head|default('')] | difference(['']) }}"
    - [ 'search ', 'nameserver ', 'domain ', 'options ' ]
  notify: Preinstall | restart network

- name: Remove search/domain/nameserver options after block
  replace:
    dest: "{{item[0]}}"
    regexp: '(# Ansible entries END\n(?:(?!^{{ item[1] }}).*\n)*)(?:^{{ item[1] }}.*\n?)+'
    replace: '\1'
    backup: yes
    follow: yes
  with_nested:
    - "{{ [resolvconffile, base|default(''), head|default('')] | difference(['']) }}"
    - [ 'search ', 'nameserver ', 'domain ', 'options ' ]
  notify: Preinstall | restart network
