Load attribute from file

I have a config file I need to update to deploy Filebeat. The file is all yammelly and will remain the same on all servers with the exception of the “prospectors” section. This section will consist of multiple lines without a uniform format. My thought was to try to add the data from this section into a multiline file for each environment, then have Chef load an attribute with the contents of that file based on the environment name. If that works, I could then simply insert that section into the template for the config file to get things working.
Is this doable?

How many lines are we talking about here and how often do they change. If you mean you want to load the data from an external file, I’m inclined to caution against that. In general, you don’t any your automation’s to be dependent on something outside of you managed code. Instead I would suggest keeping this info with the cookbook. You could do this in the files directory, in the attributes, or my personal recommendation would be to do it with templates.
Have all the different ones in the template, and use the erb <% %> tags to embed flow control. (without the equal sign)

Hope that helps!


Thanks Dan-Joe. I can have the file in the templates directory, or in the files directory for chef. It represents a section in the filebeats config file, that needs to be different per environment. Example below. So I want to insert the contents of one file into the template prospector section based on the environment.

**** Initialbulk log capture *****

  • input_type: log
    ##Global files, no processing
    • /var/log/cron
    • /var/log/messages
    • /var/log/secure

******** platform.json.log ******** (core,?)

  • input_type: log
    • /var/log/tomcat7/platform.json.log
      exclude_lines: [“c.r.a.m.h.OutboundHealthCheck”]
      json.message_key: “logger”

One concern is that trying to juggle control code and escape sequences in a template might be confusing. The file is YML, and has it’s own special characters. What I had hoped to do is have a generic config file, with the “prospectors” section empty. I would then have a separate file for each environment containing the appropriate “prospectors” section. I wanted to assign the contents of say, %ENVIRONMENT%.yml to a variable set under attributes,

default[‘filebeat’]['prospectors]= load the contents of a file based on environment name from files or resources folder

Then I could simply put that into the ERB file.

If you really want to load the contents from a file, you can:

<%= ::File.read(node['files'][node['env']] %>

However I still advise to keep all the code in the erb (and therefor source control with the cookbook), unless that is a completely unrealistic task

Here is some common text that will appear regardless of environment~

<% case node['env']

when 'a' %>

This is the plain text output when using environment a

<% case 'b' %>

Similarly, use only this text for the case of environment b

<% else %>

Maybe you need some default text when the environment is not matching?

<% end >%

Here is more common text that is environment agnostic.

Note that the <%= %> tag put in the return of the enclosed ruby, whereas the <% %> is non-inserted ruby that generally acts as flow control.

Warmest Regards,

Dan-Joe Lopez