diff --git a/40.Handlers/40.Handlers.md b/40.Handlers/40.Handlers.md new file mode 100644 index 0000000..483049a --- /dev/null +++ b/40.Handlers/40.Handlers.md @@ -0,0 +1,14 @@ +#40 - Handlers + +This exercise makes you use handlers to control flow execution. + +## Playbook creation +Write a playbook following the given specs: +* Run on host group `lab` +* Install a web server (eg. `nginx` or `httpd`) on each host using the module adapted to the package manager of your hosts +* Trigger a handler that starts the corresponding service (using appropriate module as well) + +## Validation +Run the playbook and checks if the service is installed and started. + +There is a working playbook in `solution/solution.yml` (for an Alpine host) diff --git a/40.Handlers/solution/cleanup.yml b/40.Handlers/solution/cleanup.yml new file mode 100644 index 0000000..189ee1a --- /dev/null +++ b/40.Handlers/solution/cleanup.yml @@ -0,0 +1,7 @@ +- hosts: lab + gather_facts: false + tasks: + - include_tasks: tasks/stop_nginx.yml + - include_tasks: tasks/uninstall_nginx.yml + +# vim: set ft=yaml sw=2 et: diff --git a/40.Handlers/solution/solution.yml b/40.Handlers/solution/solution.yml new file mode 100644 index 0000000..0d3ed15 --- /dev/null +++ b/40.Handlers/solution/solution.yml @@ -0,0 +1,8 @@ +- hosts: lab + gather_facts: false + tasks: + - include_tasks: tasks/install_nginx.yml + handlers: + - import_tasks: tasks/restart_nginx.yml + +# vim: set ft=yaml sw=2 et: diff --git a/40.Handlers/solution/tasks/install_nginx.yml b/40.Handlers/solution/tasks/install_nginx.yml new file mode 100644 index 0000000..929590b --- /dev/null +++ b/40.Handlers/solution/tasks/install_nginx.yml @@ -0,0 +1,7 @@ +- name: Ensure nginx is installed + become: true + apk: + name: nginx + state: present + notify: Restart Nginx +# vim: set ft=yaml sw=2 et: diff --git a/40.Handlers/solution/tasks/restart_nginx.yml b/40.Handlers/solution/tasks/restart_nginx.yml new file mode 100644 index 0000000..4194aa4 --- /dev/null +++ b/40.Handlers/solution/tasks/restart_nginx.yml @@ -0,0 +1,6 @@ +- name: Restart Nginx + become: true + sysvinit: + name: nginx + state: started +# vim: set ft=yaml sw=2 et: diff --git a/40.Handlers/solution/tasks/stop_nginx.yml b/40.Handlers/solution/tasks/stop_nginx.yml new file mode 100644 index 0000000..60af528 --- /dev/null +++ b/40.Handlers/solution/tasks/stop_nginx.yml @@ -0,0 +1,6 @@ +- name: Stop Nginx + become: true + sysvinit: + name: nginx + state: stopped +# vim: set ft=yaml sw=2 et: diff --git a/40.Handlers/solution/tasks/uninstall_nginx.yml b/40.Handlers/solution/tasks/uninstall_nginx.yml new file mode 100644 index 0000000..77eba1d --- /dev/null +++ b/40.Handlers/solution/tasks/uninstall_nginx.yml @@ -0,0 +1,6 @@ +- name: Ensure nginx is uninstalled + become: true + apk: + name: nginx + state: absent +# vim: set ft=yaml sw=2 et: