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.
Personal Internet VMware PHP Linux Shell Bluecoat Proxy Windows Hardware Virtualization Nagios MySQL DB Monitoring Mail Android Network Wyse Hacks Tomcat Postgres Apple Mac Backup BSD ZFS Solaris SmartOS Unix Multimedia Perl Database MongoDB CMS OTRS FreeBSD Wordpress LXC Nginx Proxmox DNS Graphics GlusterFS Security Chef HAProxy Icinga Ansible HTML MariaDB Containers Rancher Docker AWS ELK Kibana Logstash Filebeat Varnish PGSQL PostgreSQL ElasticSearch CouchDB Bash Macintosh Container Minio Grafana InfluxDB Databases NFS OSSEC SystemD Java Zoneminder Surveillance Elasticsearch SSL TLS Icingaweb2 Cloud Wireless Kubernetes Ubuntu