Recipes
In this page, you give nginx its own site config. The nginx steps grow, so you move them into a recipe: a directory with the steps, templates, and handlers of one service.
The recipe
Create the recipe nginx:
recipes/nginx/
recipe.yaml
templates/index.html.tmpl
templates/site.tmpltemplates/index.html.tmpl is the home page:
<!doctype html>
<title>{{ .vars.domain }}</title>
<p>Welcome to {{ .vars.domain }}.</p>templates/site.tmpl is the site config of nginx:
server {
listen 80;
listen [::]:80;
server_name {{ .vars.domain }};
root /var/www/{{ .vars.domain }};
}recipe.yaml holds the steps:
steps:
- name: Install nginx
pkg:
name: nginx
- name: Write the home page
file:
path: !tpl /var/www/{{ .vars.domain }}/index.html
template: index.html.tmpl
- name: Configure the site
file:
path: !tpl /etc/nginx/sites-enabled/{{ .vars.domain }}
template: site.tmpl
validate: nginx -t
notify: reload
- name: Remove the other sites
dir:
path: /etc/nginx/sites-enabled
purge: true
notify: reload
- name: Run nginx
service:
name: nginx
running: true
enabled: true
handlers:
reload:
- name: Reload nginx
service:
name: nginx
reloaded: trueThe new steps:
Write the home pagefills inindex.html.tmpland writes it to the directory of the site.Configure the sitefills insite.tmpl.validate: nginx -tchecks the config after winch writes it. When the check fails, winch puts the old file back, and the step fails.Remove the other sitesremoves everything insites-enabledthat no step writes: here, the default site of Debian.notify: reloadruns the handlerreloadwhen the step changes something. The handler reloads nginx once, after the last step of the recipe.
Use the recipe
In pipelines/site.yaml, replace the steps of the stage web with the
recipe:
- name: web
hosts: web
steps:
- recipe: nginxPlan and apply
winch plan site --inventory stagingstaging1.example.com
web
nginx
+ Write the home page: file /var/www/staging.example.com/index.html
create directory /var/www/staging.example.com (owner root, group root, mode 0755)
owner root, group root, mode 0644
@@ -0,0 +1,3 @@
+<!doctype html>
+<title>staging.example.com</title>
+<p>Welcome to staging.example.com.</p>
+ Configure the site: file /etc/nginx/sites-enabled/staging.example.com
owner root, group root, mode 0644
@@ -0,0 +1,6 @@
+server {
+ listen 80;
+ listen [::]:80;
+ server_name staging.example.com;
+ root /var/www/staging.example.com;
+}
validate: nginx -t
~ Remove the other sites: dir /etc/nginx/sites-enabled
remove symbolic link /etc/nginx/sites-enabled/default
! handler nginx.reload
1 host: 1 to change, 0 unchanged, 0 unknown
plan 738aee7064ea504dThe plan shows the steps under the stage and the recipe. ! marks the
handler that will run.
Apply it to staging:
winch apply site --inventory stagingThen apply it to production:
winch apply site --inventory productionApply these changes? [y/N] y
web1.example.com: nginx: + Write the home page: file /var/www/example.com/index.html
web1.example.com: nginx: + Configure the site: file /etc/nginx/sites-enabled/example.com
web1.example.com: nginx: ~ Remove the other sites: dir /etc/nginx/sites-enabled
web1.example.com: nginx: ~ handler nginx.reload: Reload nginx: service nginx
2 hosts: 1 changed, 1 unchangedThe old page /var/www/html/index.html stays on the server: winch
changes only what the steps declare. To remove it, add a step with
absent: true:
- file:
path: /var/www/html/index.html
absent: trueNext: HTTPS.