A typical chef template definition would look like this:
template "/etc/motd" do
While the template itself could look like this:
Hello and welcome to <%= node[:mycookbook][:systemname] %> !
To make this work, the variable [:mycookbook][:systemname] has to be defined somewhere. This could be in several places:
But in certain scenarios this could cause a headache. For example when we have several nodes and all of them run the exact same role. The only difference is the recipe which is used to differ the nodes. When the variable is defined in the attribute files (attributes/host1.rb, attributes/host2.rb, etc.) the variables get mixed up.
default[:mycookbook][:systemname] = "host1"
default[:mycookbook][:systemname] = "host2"
All attribute files of the cookbook are read when chef runs and because the variable is always defined with the same name (node[:mycookbook][:systemname]), it cannot be guaranteed that the correct value is taken for the system. They overwrite themselves according to the order in which they are parsed by the chef run.
A fast and easy possibility to solve this is to set the variables directly in the recipe within the template command:
template "/etc/motd" do
variables( :sysname => "host1" )
With the variables line, special variables can be defined (here the variable sysname is set and given the value "host1") and sent directly to the template file. The template file in return needs to look a bit different to handle direct input from the template command:
Hello and welcome to <%= @sysname %> !
Now every node has its correct system name in the motd output, because every node uses its own recipe (in which the variable for the sysname is set).
No comments yet.
AWS Android Ansible Apache Apple Atlassian Automation BSD Backup Bash Bluecoat CMS Chef Cloud Consul Container Containers CouchDB DB DNS Database Databases Docker ELK ElasticSearch Elasticsearch Filebeat FreeBSD GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Icingaweb2 InfluxDB Internet Java Kibana Kubernetes LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Rancher SSL Security Shell SmartOS Solaris Surveillance SystemD TLS Tomcat Ubuntu Unix VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder