【发布时间】:2016-10-31 21:59:26
【问题描述】:
我是chef 的新手,开始学习技巧,想知道以下是否可行以及如何实现。我来自过去 2 年一直在使用 ansible 的人。
我想知道如何操作.erb模板
ansible code - varible.yml
apache_vhosts:
- servername: "{{ enterprise }}.test.io"
serveralias: "{{ inventory_hostname }}"
documentroot: "/var/www/test/current/web"
symfony_prod: true
redirect_https: true
- servername: "{{ enterprise }}forms.test.io"
documentroot: "/var/www/test/current/web"
symfony_form: true
redirect_https: true
- servername: "{{ enterprise }}trk.test.io"
documentroot: "/var/www/test/current/web"
symfony_track: true
redirect_https: true
ansible code - vhosts.conf.j2 (jinja template)
{% for vhost in apache_vhosts %}
<VirtualHost *:{{ apache_listen_port_http }}>
ServerName {{ vhost.servername }}
{% if vhost.redirect_https is defined and vhost.redirect_https == true %}
Redirect 301 / https://{{ vhost.servername }}/
{% else %}
DocumentRoot {{ vhost.documentroot }}
{% if vhost.serveradmin is defined %}
ServerAdmin {{ vhost.serveradmin }}
{% endif %}
{% if vhost.symfony_dev is defined %}
DirectoryIndex app_dev.php
<Directory "{{ vhost.documentroot }}">
AllowOverride None
Options -Indexes +FollowSymLinks
Order allow,deny
Allow from all
# Symfony2 rewriting rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app_dev.php [L]
</Directory>
{% elif vhost.symfony_prod is defined %}
DirectoryIndex app.php
<Directory "{{ vhost.documentroot }}">
AllowOverride None
Options -Indexes +FollowSymLinks
Order allow,deny
Allow from all
# Symfony2 rewriting rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</Directory>
{% else %}
<Directory "{{ vhost.documentroot }}">
AllowOverride All
Options -Indexes +FollowSymLinks
Order allow,deny
Allow from all
</Directory>
{% endif %}
{% if vhost.extra_parameters is defined %}
{{ vhost.extra_parameters }}
{% endif %}
{% endif %}
</VirtualHost>
{% endfor %}
从上面的代码中,您可以看到我在.yml 文件中循环使用apache_vhosts,并在创建模板时使用内部对象。 .erb 可以做到这一点吗?我如何在.rb 属性文件中复制它。
目前我只有以下;
chef code - default.rb
# Apache attributes
default["altostack"]["apache_conf_path"] = "/etc/apache2/sites-enabled"
default["altostack"]["apache_redirect_https"] = false
default["altostack"]["apache_servername"] = "test.test.io"
default["altostack"]["apache_documentroot"] = "/var/www/test/current/web"
default["altostack"]["apache_ssl_crt_dir"] = case node.environment
when '_default'
default["altostack"]["apache_ssl_crt_dir"] = "/etc/apache2/ssl/"
end
【问题讨论】:
标签: ruby chef-infra ansible jinja2 erb