Advertisement
alexs77

Chef - adding text to BEGINNING of file - how?

Aug 27th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.39 KB | None | 0 0
  1. #############################################
  2. # SSH Parameter
  3. {"Protocol" => "2", "LogLevel" => "INFO", "MaxAuthTries" => "4", "IgnoreRhosts" => "yes", "HostbasedAuthentication" => "no", "PermitRootLogin" => "no", "PermitEmptyPasswords" => "no", "Banner" => "/etc/issue.net"}.each do |param, value|
  4.     cfg_file = "/etc/ssh/sshd_config"
  5.  
  6.     replace_line_re = /^#{param}.*/
  7.     insert_line_text = replace_line_text = "#{param} #{value}"
  8.     insert_line_re = /^#{replace_line_text}/
  9.  
  10.     ruby_block "CIS: SSHd Parameter modifizieren. " + param + " => " + value do
  11.         block do
  12.             fe = Chef::Util::FileEdit.new(cfg_file)
  13.  
  14.             # Alte Werte durch neue Werte ersetzen
  15.             fe.search_file_replace_line(replace_line_re, replace_line_text)
  16.  
  17.             # Zeile mit neuem Wert am Ende der Datei einfügen - falls nicht schon vorhanden
  18.             fe.insert_line_if_no_match(insert_line_re, insert_line_text)
  19.  
  20.             # Datei schreiben
  21.             fe.write_file
  22.         end # of block do
  23.         notifies :reload, "service[ssh]", :delayed
  24.     end # of ruby_block "CIS: SSHd Parameter modifizieren. " + param + " => " + value do
  25. end # of {"Protocol" => "2", "LogLevel" => "INFO", "MaxAuthTries" => "4", "IgnoreRhosts" => "yes", "HostbasedAuthentication" => "no", "PermitRootLogin" => "no", "PermitEmptyPasswords" => "no", "Banner" => "/etc/issue.net"}.each do |param, value|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement