- name: Get Public IP
  uri:
    url: https://ipinfo.io/ip
    return_content: yes
  register: public_ip

- name: Read file
  command: "cat /root/{{ domain }}.dkim.txt"
  register: dkim_txt

- name: 'Clean up DKIM line 1 and 2'
  set_fact:
    line1: "{{ dkim_txt.stdout_lines[1] | regex_replace('^.+?\\\"p=|\\\"', '') }}"
    line2: "{{ dkim_txt.stdout_lines[2] | regex_replace('^.+?\\\"|\\\".+', '') }}"

- name: SPF Record
  debug:
    msg: "{{ domain }}. 1800 IN TXT 'v=spf1 mx ip4:{{ public_ip.content | replace('\n', '') }} -all'"

- name: DMARC Record
  debug:
    msg: "_dmarc.{{ domain }}. 1800 IN TXT 'v=DMARC1; p=none; rua=mailto:postmaster@{{ domain }}'"
  
- name: 'DKIM Record'
  debug:
    msg: "mail._domainkey.{{ domain }}. 1800 IN TXT 'v=DKIM1; h=sha256; k=rsa; p={{ line1 }}{{ line2 }}'"
