9

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)

allo
  • 1,620
  • 2
  • 22
  • 39
  • Testing your config is probably not something that should be in a handler to begin with. It should just be another play. If the test fails, you _want_ to bail out and fix the problem before executing anything else. – Michael Hampton Dec 24 '15 at 00:12
  • That's the point. First i want to create some config snippets, then i want to test the config and reload when it works. But i want to have the logical step "i want to reload", where the reload task/handler has "the test succeeded" as dependency. – allo Dec 24 '15 at 00:42
  • 1
    Unless you specify `ignore_errors` on a task then your playbook will simply terminate when an error is encountered. So simply running your tasks in order should be enough. If one fails then execution halts before the next task is executed. – Bruce P Dec 25 '15 at 02:47

1 Answers1

9

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
  
Daniel t.
  • 9,291
  • 1
  • 33
  • 36
  • This works, but it has the dependencies upside down. I am searching for a way with logic dependencies, which means "if i have new configs, restart nginx, if a config test works". So "new configs" is the trigger (easy to do), "restart nginx" is the action. But "configtest" should be a dependency, which is invoced by the "restart nginx" task. Just like debian-package dependencies, where are the dependencies packages are pulled by the package you actually want to install. – allo Jan 06 '16 at 15:50
  • If you using `template` to push new config, then Ansible can tell there is a new config. You then use `notify` handler to reload nginx. Now between the `template` task and handler, you can place a `shell` configtest. Since Ansible executes the tasks in order, it will not go to the handler if the configtest fails. – Daniel t. Jan 09 '16 at 16:49
  • So it does not jump direct to the task, but runs the whole task list before? Do i need different playbooks for different targets then, to avoid running the same task stack for different handlers? Sorry when my question seems dumb, i am rather new to ansible and maybe i want to achive something which is not done that way there. – allo Jan 09 '16 at 22:57