3

I'm new to chef and starting to learn the ropes and want to know if the below is possible and how to achieve it. I'm coming from someone who's been using ansible for the past 2 years.

I want to know how to manipulate .erb templates

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

From the code above, you can see that I'm looping through apache_vhosts in the .yml file and using the inner objects as I create the template. Is this possible with .erb how do I replicate this in a .rb attributes file.

Currently I just have the below;

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
moh_abk
  • 2,064
  • 7
  • 36
  • 65

1 Answers1

4

To replicate more or less your ansible format:

# 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

In the template file:

<% 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 :)

The template syntax in chef is using erb, it is covered in the documentation here and it accept plain ruby within the template.

The usual recomendation is to take advantage of community cookbooks, here namely apache2 which has a nice Usage section in it's readme and a basic example usage of it's web_app resource.

Tensibai
  • 15,557
  • 1
  • 37
  • 57
  • thanks for this, I'm avoiding focusing alot on the community cookbooks as I believe they're very overwhelming compare to there equivalent ansible galaxy roles. I'll ofcourse be looking at their patterns and structure. My goal is to understand the syntax and be able to create templates and work with attributes so that when I then start making my own cookbooks I don't run into any surprises. – moh_abk Oct 31 '16 at 14:05
  • @shady I'll highly encourage you to got through http://learn.chef.io in this case ;) – Tensibai Oct 31 '16 at 14:06
  • I believe this is something more to do with knowing `ruby` language and the `erb` engine.. Same way learning `ansible` won't teach you how to use `jinja2` templates rather your knowledge of `python` in addition to the template engine docs – moh_abk Oct 31 '16 at 14:09
  • For the templates, yes, but the chef doc cover the basic usage if looping over attribute hash. Advanced ruby is usually not needed until you write complex providers for chef – Tensibai Oct 31 '16 at 14:10
  • Maybe I'm being to ambitious trying to lift and shift `yaml` to `rb` lol.. I'll need to create hashes of hashes and loop through to achieve that – moh_abk Oct 31 '16 at 14:13
  • Well, yaml, json or ruby hashes are still what a pythonist would call a dictionary IIRC :) That's jsut a matter of syntax at first, the core principles are the same – Tensibai Oct 31 '16 at 14:15