avoiding duplicate string writing

Hi all,

I'm trying to run the code below. Basically I want to include the

users “user” and “user2” after the line “Authentication:”. I’m
running this ruby_block inside a Chef recipe and in a first moment
it works as expected, however, after I run “chef-solo” again the
recipe is applied again and two more lines are created in the
destination file. I need a way for my script to check for the
existence of “user” and “user2” lines and then stop the script and
write nothing.

I suspect I should use some kind of regular expression but I am not

sure how.

Any ideas? Below follows the code.

Thanks for your help.

**ruby_block "insert_line_to_sshd" do** **block do** **file = Chef::Util::FileEdit.new("/tmp/testconf")** **      file.insert_line_after_match("Authentication:",

“AllowUsers/1 user”)** ** file.insert_line_after_match(“Authentication:”,
“AllowUsers/1 user2”)** file.write_file end end

You shouldn't need a Regex for that, how about String#include?

Chef’s FileEdit is useful, but does lack some functionality. What you could do is call search_file_delete_line for the two lines you’re adding. Hacky, but it should work every time.