#!/bin/sh # Validate a modified BIND config fragment in the context of the full named.conf. # # Usage: named-checkconf-wrapper.sh # # Ansible's 'validate' option must contain '%s'. When the module runs, '%s' is # replaced with a temporary copy of the file that was just edited (the fragment, # e.g. /etc/bind/named.conf.options). named-checkconf, however, only accepts a # single config file and the fragment is only valid as part of the main config. # # This wrapper strips '%s' handling from the caller and instead: # 1. takes the modifications that were written to a temporary fragment (%s) # 2. creates a temporary copy of the main named.conf where the original # fragment path is replaced by that temporary fragment # 3. runs named-checkconf against the temporary main config # # Arguments: # $1 fragment_path : the temporary fragment as passed by Ansible ('%s') # $2 named_conf : path to the main named.conf # $3 fragment_orig : path of the original fragment inside named_conf # $4 named_checkconf : path to named-checkconf set -eu FRAGMENT="$1" NAMED_CONF="${2:-/etc/bind/named.conf}" FRAGMENT_ORIG="${3:-/etc/bind/named.conf.options}" NAMED_CHECKCONF="${4:-/usr/bin/named-checkconf}" TMP_CONF="$(mktemp "$(dirname "$NAMED_CONF")/.named-checkconf.XXXXXX")" trap 'rm -f "$TMP_CONF"' EXIT esc_fragment=$(printf '%s' "$FRAGMENT" | sed 's/[\/&]/\\&/g') esc_orig=$(printf '%s' "$FRAGMENT_ORIG" | sed 's/[\/&]/\\&/g') sed "s/${esc_orig}/${esc_fragment}/g" "$NAMED_CONF" > "$TMP_CONF" # named-checkconf writes its diagnostics to stdout. Ansible only reports the # stderr of a failed 'validate' command, so merge stdout into stderr to make # validation failures readable in the playbook output. "$NAMED_CHECKCONF" "$TMP_CONF" >&2