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


Rodrigo de Campos
Unix System A.

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

On Nov 14, 2013, at 3:27 AM, Rodrigo de Campos [email protected]
wrote:

Rodrigo de Campos
Unix System A.

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