我有一个单一的Ansibleconfiguration服务器运行一些网站。
我的Ansible任务大致如下:
- name: site nginx config template: src="nginx-site.conf.j2" dest=/etc/nginx/conf.d/{{item.name}}.conf owner=root group=root mode=0444 with_items: sites notify: restart nginx - name: nginx conf template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf owner=root group=root mode=0444 notify: restart nginx
我想使用Ansible模板模块的validate
参数来调用nginx -t
,并确保我的新configuration在语法上是有效的。 它适用于主要的nginx.conf:
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf owner=root group=root mode=0444 validate="/usr/sbin/nginx -c %s -t"
但它似乎没有拿起对特定于站点的configuration文件的更改。 validate
网站特定的模板不起作用,因为他们需要包装在一个http
指令是有效的。
我能做些什么来检查这些特定于站点的文件的有效性?
你可以在那里做一些技巧和validation放置的文件,如(从https://gist.github.com/psd/5042334借用的想法): validate: bash -c 'nginx -t -c /dev/stdin <<< "events {worker_connections 1;} http { include %s; }"'
考虑到其他答案的问题,我采用了类似于接受答案的方法。
我为此创造了这个要点 。
这个想法是将整个/etc/nginx
目录复制到一个临时目录,从%s
参数中更改一个文件并testing主要的nginxconfiguration是否存在问题。 如果你认为最初的nginxconfiguration是有效的,并且所有修改nginxconfiguration的任务都使用它来validation,那么我猜这是没有问题的。
作为一个class轮,它看起来像这样:
validate: bash -c 'NGINX_CONF_DIR=`mktemp -d`; cp -rTp /etc/nginx/ "$NGINX_CONF_DIR" && cp -Tp %s "$NGINX_CONF_DIR"/sites-enabled/new-site.conf && nginx -t -c "$NGINX_CONF_DIR"/nginx.conf'
直接调用对nginx主configuration文件中包含的文件validate
是没有意义的,因为特定configuration文件中指令的有效性可能取决于configuration文件的其余部分(例如,您有两个声明同一服务器的configuration文件块等)。
您必须始终在主configuration文件上调用nginx -t
,而不是每当您要validation任何nginx的configuration更改时,它的子部分之一。