gpt4 book ai didi

ruby - 在 Chef 模板 (ERB) 中循环遍历数组

转载 作者:行者123 更新时间:2023-12-01 12:25:29 24 4
gpt4 key购买 nike

我是 chef 的新手,开始学习技巧,想知道以下是否可行以及如何实现。我来自过去 2 年一直在使用 ansible 的人。

我想知道如何操作.erb模板

ansible 代码 - 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 代码 - 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

最佳答案

要或多或少地复制您的 ansible 格式:

# Apache attributes

default["altostack"]["test.test.io"]["apache_conf_path"] = "/etc/apache2/sites-enabled"
default["altostack"]["test.test.io"]["apache_redirect_https"] = false
default["altostack"]["test.test.io"]["apache_documentroot"] = "/var/www/test/current/web"
default["altostack"]["test.test.io"]["apache_ssl_crt_dir"] = case node.environment
when '_default'
"/etc/apache2/ssl/"
end

#Alternative synteax with hash:

default["altostack"]["test_2.test.io"]= {
"apache_conf_path" => "/etc/apache2/sites-enabled",
"apache_redirect_https" => false,
"apache_documentroot" => "/var/www/test/current/web"
}

# For the case statement, better use the usual approach, easier to maitain IMHO
default["altostack"]["test_2.test.io"]["apache_ssl_crt_dir"] = case node.environment
when '_default'
"/etc/apache2/ssl/"
end

在模板文件中:

<% node['altostack'].each do |servername,properties| -%>
<VirtualHost *:<%= properties['apache_redirect_https'] %>
ServerName <%= servername %>
<% if !properties['redirect_https'].nil? and properties['redirect_https'] == true -%>
Redirect 301 / https://<%= servername %>/
<% else -%>
DocumentRoot <%= properties['documentroot'] %>
<% if !properties['serveradmin'].nil? -%>
ServerAdmin <%= properties['serveradmin'] %>
<% endif -%>
# Rest of template to be translated by yourself :)

Chef 中的模板语法使用 erb,它在 documentation here 中有介绍它在模板中接受纯 ruby​​。

通常的建议是利用社区 Recipe ,这里是 apache2这有一个很好的Usage自述文件中的部分和它的基本示例用法 web_app资源。

关于ruby - 在 Chef 模板 (ERB) 中循环遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40343148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com