This will update the serial number like you want on the zone files
that get changed. It's not pretty, but it works.
In fact, it's particularly ugly, using a Perl script to loop over
your
zone_files and print json to
stdout that gets read back in.
I've got to admit it: it was fun to play with Perl again. So, thanks
for that!
- name: Add TXT entry to zone file
lineinfile:
dest: "{{ item }}"
insertafter: EOF
line: '_some-name TXT "aZBBBBhonRJiOCL1ZtkKMKnx7U2k3wbSswyClGb6wGk"'
with_items: "{{ zone_files }}"
register: add_txt
- name: Show add_txt
ansible.builtin.debug:
var: add_txt
- name: Generate updated serial numbers for zone_files
ansible.builtin.command: >-
perl -n -e 'BEGIN {print "{";$sep="";}
if (m/\b(\d{8})(\d{2}).*serial/i) {
if ($1 eq "{{ today }}") {
printf("%s\"%s\": \"%s%02d\"", $sep, $ARGV, "{{ today }}", $2+1);
} else {
printf("%s\"%s\": \"%s%02d\"", $sep, $ARGV, "{{ today }}", "01");
}
$sep=",\n";
}
END{print"}"}' {{ zone_files | map('quote') | join(' ') }}
vars:
today: "{{ lookup('pipe', 'date +%Y%m%d') }}"
register: new_serial
- name: Make new_serial easily accessible
ansible.builtin.set_fact:
new_serials : "{{ new_serial.stdout | from_json() }}"
- name: Update serial number
lineinfile:
backrefs: true
dest: "{{ item }}"
regexp: '^(\s+)(\d{8})(\d{2})(\s+; Serial Number.*)'
line: '\g<1>{{ new_serials[item] }}\g<4>'
loop: "{{ add_txt.results | selectattr('changed', 'true') | map(attribute='item') }}"