Test before remote daemon restart?

I have a cron based script which based on local changes, generates a configuration file (in my case for unbound) and them via ansible pushes/copies it off to several institutional caching dns servers, restarting the daemon if necessary.

- name: Write some files to be included for unbound
  ansible.builtin.copy:
      src: "files/unbound/{{item}}"
      dest: "{{remote_dir}}"
      backup: true
      owner: root
  notify: Restart unbound

etc..

Is there some builtin ansible methods for testing the config file (even locally) say for syntax errors before copying and restarting? Otherwise some very bad things happen on the far end.

Thanks!

ansible.builtin.copy has the validate option. This allows you to call out to an on-system tool to validate the config file before the copy is completed.

If the application you’re trying to use to validate can’t you alternate/temp files, there is also documentaiton on how to do that process yourself.

It involves copying the file, running the test, and then reverting that copy if the test does not pass.

Hope that helps.

3 Likes

In the Ansible unbound role I have written I use unbound-checkconf via the ansible.builtin.command module for this:

    - name: Check Unbound config
      ansible.builtin.command: unbound-checkconf
      environment:
        PATH: /usr/sbin:/usr/bin:/sbin:/bin
      check_mode: false
      changed_when: false