Ask Your Question
3

file_line cannot delete lines by regexp?

asked 2016-01-26 13:03:50 -0500

wsanders1 gravatar image

There seems to be a misleading example in the stdlib documentation:

file_line { 'bashrc_proxy':
  ensure            => absent,
  path              => '/etc/bashrc',
  line              => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
  match             => '^export\ HTTP_PROXY\=',
  match_for_absence => true,
}

That snippet is supposed to find lines matching the regexp in "match" and replace them with the line in "line". However, it does not seem possible to delete a line by regular expression. I would like to process a zabbix_agentd.conf file and remove all lines starting with "UserParameter=mysql". The following snippet does not work:

  file_line { 'UserParameter=mysql':
    ensure => absent,
    path => '/etc/zabbix/zabbix_agentd.conf',
    line => '^UserParameter=mysql*',
  }

It seems from the source code snippet in http://superuser.com/questions/758915... that file_line does not delete lines unless they are an exact match.

Perhaps the documentation could be updated to reflect this?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2016-05-04 23:43:07 -0500

aaronjcopley gravatar image

If you run in debug mode you may see the message;

Debug: /Stage[main]/Main/Htpasswd[remove]/File_line[remove_line]: Nothing to manage: no ensure and the resource doesn't exist

(Substitute your classes & resources.)

If this is the case you are failing to remove the line because Puppet does not think the resource exists in the first place.

Looking at the Ruby code for file_line we can see this;

Puppet::Type.type(:file_line).provide(:ruby) do
  def exists?
    if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
      true
    else
      lines.find do |line|
        line.chomp == resource[:line].chomp
      end
    end
  end

Basically, in order for a resource to exist you must set replace to not true AND meet the RegEx match with 1 or more instances. Or, your entire line must be matched. (What most people in this thread are experiencing.)

So, in order to get the desired behavior, you must set replace => false. This may cause undesired behavior for ensure => present, though. (It is true by default.) So, you can parameterize $replace and set it conditionally based on the value of $ensure.

# For some reason, if we want to remove an entry based on 'match', 'replace'
# must be false.
if $ensure == 'absent' {
  $replace = false
}

file_line { "${user}_htpasswd":
  ensure            => $ensure,
  line              => "${user}:${real_hash}",
  path              => '/var/www/.htpasswd',
  match             => "^${user}:",
  match_for_absence => true,
  replace           => $replace,
}
edit flag offensive delete link more
0

answered 2016-01-26 13:18:37 -0500

lavaman gravatar image

File_line does not support regex in the line parameter. The example to which you refer is using the match parameter to remove the lines, not the line parameter. You probably need something like the following:

file_line { 'UserParameter=mysql':
  ensure => absent,
  path => '/etc/zabbix/zabbix_agentd.conf',
  match => '^UserParameter=mysql*',
  match_for_absence => true,
  multiple => true,
}
edit flag offensive delete link more

Comments

I am having the same problem as the OP. This example does not work. First, it does not provide the required "line" parameter. But further, I cannot get "ensure=>absent" to work without putting the exact line to be removed in the "line" parameter.

erichymowitz gravatar imageerichymowitz ( 2016-02-03 11:31:04 -0500 )edit
0

answered 2016-01-26 13:39:47 -0500

wsanders1 gravatar image

However, the documentation states the "line" parameter is required, so your example is not supposed to work. (I have not actually tried it, though.)

The best one can do is substitute an empty line or comment in the "line" parameter:

  file_line { 'UserParameter=mysql':
    ensure => present,
    path => '/etc/zabbix/zabbix_agentd.conf',
    line => '# UserParameter=mysql lines are removed',
    match => '^UserParameter=mysql*',
    multiple => true,
  }
edit flag offensive delete link more

Comments

You may have to provide the line parameter, but it isn't used when you specify match_for_absence => true. You will need that parameter for this to work correctly.

lavaman gravatar imagelavaman ( 2016-01-26 15:34:15 -0500 )edit

I am having the same problem. Only the "line" parameter is used/matched when ensure => absent., even when match_for_absence => true.

erichymowitz gravatar imageerichymowitz ( 2016-02-03 11:31:46 -0500 )edit

The documentation also states "match_for_absence: An optional value to determine if match should be applied when ensure => absent. If set to true and match is set, the line that matches match will be deleted. " This is not happening.

erichymowitz gravatar imageerichymowitz ( 2016-02-03 11:32:59 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2016-01-26 13:03:50 -0500

Seen: 2,505 times

Last updated: May 04 '16