Can ansible tasks be used as dependencies?

I currently have three ansible tasks:

  • create vhosts
  • test config
  • reload nginx

I now registered the last two as handlers, but the forward notifications of ansible feel wrong for what i am doing:

  • create vhosts, notify test config (okay)
  • test config, notify reload (why does a config test imply a reload)
  • reload nginx

I would like a structure like:

  • create vhosts, notify nginx reload
  • nginx reload: require config test
  • config test: success
  • nginx reload

Just because the semantics seem more correct. It should be neither just a sequence, nor should something like a config test notify a reload, because this is just implementing a sequence again without logic behind (like a reload requires a test first)


A simple conditional in your playbook with the use of when should work, in case you are ignoring errors. As by default, Ansible playbook run terminates when it encounters an error. Nginx configtest exits with shell status code of 0 on success and 1 on failure, and you can use that to run different tasks depending on the result -

tasks:
  - shell: service nginx configtest
    ignore_errors: True
    register: result

  - shell: service nginx reload
    when: result|success

  - local_action: mail subject='Nginx config error.'
    when: result|failed