Using a Chef recipe to append multiple lines to a config file
As you said yourself, the recommended Chef pattern is to manage the whole file.
If you're using Chef 11 you could probably make use of partials for what you're trying to achieve.
There's more info here and on this example cookbook.
As long as you have access to the original config template, just append <%= render "original_config.erb" %>
to the top of your parms_to_append.conf
template.
Use a Chef recipe to modify a single line in a config file
I never figured out how to modify a single line in a config file using Chef, but here's the recipe I ended up using to disable THP settings for MongoDB.
Recipe: Install MongoDB
# Install MongoDB on Amazon Linux
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-amazon/
# 1: configure the package management system (yum)
# 2: install mongodb
# 3: configure mongodb settings
# 3.A: give mongod permission to files
# data & log directories (everything in /srv/mongodb)
# http://stackoverflow.com/questions/7948789/mongodb-mongod-complains-that-there-is-no-data-db-folder
execute "mongod_permission" do
command "sudo chown -R mongod:mongod /srv/mongodb"
#command "sudo chown mongod:mongod /var/run/mongodb/mongod.pid"
#command "sudo chown -R $USER /srv/mongodb"
end
# 3.B: edit Transparent Huge Pages (THP) Settings
# get rid of mongod startup warning
# http://docs.mongodb.org/manual/reference/transparent-huge-pages/#transparent-huge-pages-thp-settings
# 3.B.1: disable
execute "disable_thp_khugepaged_defrag" do
command "echo 0 | sudo tee /sys/kernel/mm/transparent_hugepage/khugepaged/defrag" # different b/c file doesn't have options list
end
execute "disable_thp_hugepage_defrag" do
command "echo 'never > /sys/kernel/mm/transparent_hugepage/defrag' | sudo tee --append /sys/kernel/mm/transparent_hugepage/defrag"
end
execute "disable_thp_hugepage_enables" do
command "echo 'never > /sys/kernel/mm/transparent_hugepage/enabled' | sudo tee --append /sys/kernel/mm/transparent_hugepage/enabled"
end
# 3.B.2: verify disabled on reboot
template "/etc/rc.local" do
source "init-rc.local.erb"
owner 'root'
group 'root'
mode '0775'
end
# 4: use upstart & monit to keep mongod alive
Template: init-rc.local.erb
touch /var/lock/subsys/local
if test -f /sys/kernel/mm/transparent_hugepage/khugepaged/defrag; then
echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
How can I create a simple text file that contains more than one line of text with CHEF?
You would use either the file
resource or template
resource, depending on if you wanted simple inline content, or a more complex Erb template:
file '/foo' do
content "line one\nline two\n"
end
Which Chef resource can I execute multiple lines of BASH code using Chef's sensititve property?
Answered in comments, true
and false
are lowercase in Ruby. Using True
makes Ruby try to look for a const that doesn't exist.
Maintain the file content using chef-client
This isn't what Chef does. It manages the entire content of a file with the file resource. You will need to create your own custom resource if you want a file handling resource that does this. However, given how file manipulation like that would normally work this is going to be pretty specific to the file contents. You really need to encourage that everyone editing a file does so the same way. You shouldn't be partially managing a file with Chef and then letting others manage another part of it a different way. They should update the Chef code appropriately.
If you want people to manage parts individually you can look at partial templates. However, this still requires they manage their portion in a way Chef can import and understand and they aren't manipulating directly on the server.
EDIT: See comments below, this is for the sudoers file and in that case the sudo
resource should be used.
Related Topics
Bash Script That Creates a Directory Structure
Cannot Change The Maximum Open Files Per Process with Sysctl
How to Curl Using Ipv6 Address
Linux - Check If There Is an Empty Line at The End of a File
What Is The Maximum Allowed Depth of Sub-Folders
In Linux, How to Find Find Directory with The Most Subdirectories or Files
Using Output of Previous Commands in Bash
What Is Eof!! in The Bash Script
Bash Arrays and Negative Subscripts, Yes or No
Copy and Overwrite a File in Shell Script
Check If All Lines from One File Are Present Somewhere in Another File
Securing Udp - Openssl or Gnutls or ...
In Linux, How to Create a File Descriptor for a Memory Region
Adjust Audio Volume Level with Cli Omxplayer - Raspberry Pi
How to Get a Bash Script Working on Freebsd, Openbsd and Linux Without Modifying It
How Does The Os Know The Real Size of The Physical Memory
How to Connect to Amazon Linux Instance Using Remote Desktop from Windows 7