2016-10-31 7 views
0

私はchefに新しく、ロープを学び始め、以下のことが可能かどうか、また達成する方法を知りたい。私は過去2年間、ansibleを使ってきた人から来ています。あなたはその私を見ることができ、vhosts.conf.j2jinja template)上記のコードからシェフテンプレート(ERB)のループスルー配列

{% 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 %} 

から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 -

は私が.erbテンプレート

ansible codeを操作する方法を知りたいですの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 

答えて

1

多かれ少なかれ複製するためにあなたの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 :) 

テンプレート構文でシェフがerbを使用している場合は、documentation hereに記載されており、テンプレート内では普通のルビーを受け入れます。

通常recomendationはすなわち、ここで、コミュニティの料理本を活用するために、それはREADMEだし、それの基本的な使用例は、web_appリソースの中で素敵なUsageセクションを持っていapache2です。

+0

ありがとう、私は彼らが非常に圧倒的であると信じているので、コミュニティの料理本に重点を置いて避けています。私は彼らのパターンと構造を見なければなりません。私の目標は、構文を理解し、テンプレートを作成して属性を操作できるようにすることで、自分の料理本を作ったときに私は驚くことはありません。 –

+0

@ shadyこのケースでは、http://learn.chef.ioにアクセスすることを強くお勧めします;) – Tensibai

+0

これは 'ruby'言語と' erb'エンジンを知ることともっと関係していると思います。同じテンプレートエンジンのドキュメントに加えて、 'python'の知識だけでなく、' jinja2'テンプレートの使い方を教えてくれません –

関連する問題