title: Using a Chef recipe to append multiple lines to a config file
body: <p>I'm trying to create a Chef recipe to append multiple lines (20-30) to a specific config file. </p>
  
  <p>I'm aware the recommended pattern is to <a href="https://stackoverflow.com/questions/14885267/why-config-files-shouldt-be-changed-line-by-line-with-chef-puppet">change entire config files</a> rather than just appending to a file, but I dislike this approach for multiple reasons.</p>
  
  <p>So far the only solution I found was to use a <code>cookbook_file</code> and then use a bash resource to do: </p>
  
  <p><code>cat lines_to_append &gt;&gt; /path/configfile</code></p>
  
  <p>Obviously this wouldn't work properly, as it'd append the file over and over, each time you run chef-client. I'd have to create a small bash script to check for a specific string first, and, <strong>if not found</strong>, append to the file. </p>
  
  <p>But this seems to defeat the purpose of using Chef. There must be a better way.</p>
  
  <p>One promising solution was the <a href="http://community.opscode.com/cookbooks/line" rel="nofollow noreferrer">line cookbook</a> from OpsCode Community. It aimed to solve this exact problem. Unfortunately the functionality is incomplete, buggy, and the code is just a quick hack. Far from being a solid solution.</p>
  
  <p>Another option I evaluated was <a href="http://augeas.net/" rel="nofollow noreferrer">augeas</a>. Seems pretty powerful, but it'd add yet-another layer of abstraction to the system. Overkill, in my case.</p>
  
  <p>Given that this is one of the most obvious tasks for any sysadmin, is there any easy and beautiful solution with Chef that I'm not seeing?</p>
  
  <hr>
  
  <p>EDIT: here's how I'm solving it so far:</p>
  
  <pre><code> cookbook_file "/tmp/parms_to_append.conf" do
     source "parms_to_append.conf"
   end
  
   bash "append_to_config" do
     user "root"
     code &lt;&lt;-EOF
        cat /tmp/parms_to_append.conf &gt;&gt; /etc/config
        rm /tmp/parms_to_append.conf
     EOF
     not_if "grep -q MY_IDENTIFIER /etc/config"
   end
  </code></pre>
  
  <p>It works, but not sure this is the recommended Chef pattern.</p>