Home Ask Login Register

Developers Planet

Your answer is one click away!

Geoff H February 2016

Chef File Ruby search_file_replace_line in php.ini regex

I'm trying to search for the existence of the following line in a file;

default_mimetype = "text/html"

and then if found uncommented-out as above, then comment it out like so;

;default_mimetype = "text/html"

I currently have the following Chef recipe;

ruby_block "php_ini" do
    block do
        file = Chef::Util::FileEdit.new("/etc/php.ini")
        file.search_file_replace_line(/default_mimetype = "text/html"/, ';default_mimetype = "text/html"')

service "apache2" do
    action :restart

but the file_search_replace_line line is causing a syntax error (probably due to the "s & the ; ) How do I need to write this line so that it does what I want?


coderanger February 2016

Managing a file like this is not recommended, as it allows all kinds of non-convergent behavior and is generally very hard to reason about. A better approach is to use a template resource to completely control the content of the file, which means you always know what will be in it rather than depending on what happened to be there before Chef ran (usually a distro-provided file).

As to the specifics, the issue is the / in text/html, make that text\/html.

Post Status

Asked in February 2016
Viewed 2,880 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life