avoiding duplicate string writing

42 views
Skip to first unread message

Rodrigo de Campos

unread,
Nov 14, 2013, 4:27:37 AM11/14/13
to rubyonra...@googlegroups.com
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 Administrator

Joel Pearson

unread,
Nov 14, 2013, 9:42:29 AM11/14/13
to rubyonra...@googlegroups.com
You shouldn't need a Regex for that, how about String#include?

--
Posted via http://www.ruby-forum.com/.

Tamara Temple

unread,
Nov 14, 2013, 12:28:11 PM11/14/13
to rubyonra...@googlegroups.com
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.

Reply all
Reply to author
Forward
0 new messages