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 BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Icingaweb Icingaweb2 Influx Internet Java KVM Kibana Kodi Kubernetes LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder