Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created March 29, 2015 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahlz/9fafbeec2d86c5fd65fe to your computer and use it in GitHub Desktop.
Save noahlz/9fafbeec2d86c5fd65fe to your computer and use it in GitHub Desktop.
## Simple extension of the Chef tutorial for appending line(s) to the end of a cookbook
## Alternative to copying an entire cookbook just to add one line to a config file.
## (Apparently, partials are an alternative in Chef 11?
## http://stackoverflow.com/a/19167106/7507
file 'motd' do
content "hello chef\n"
end
ruby_block "ensure sig to end of file" do
block do
fe = Chef::Util::FileEdit.new("motd")
fe.insert_line_if_no_match(/@noahlz/, "@noahlz was here")
fe.write_file
end
end
[nzucker: motd]$ echo "hello chef" > motd
[nzucker: motd]$ cat motd
hello chef
[nzucker: motd]$ sudo chef-apply hello.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* file[motd] action create (up to date)
* ruby_block[ensure sig to end of file] action run
- execute the ruby block ensure sig to end of file
[nzucker: motd]$ cat motd
hello chef
@noahlz was here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment