delete line in file with substring xyz

1,189 views
Skip to first unread message

Andreas Dvorak

unread,
Feb 25, 2015, 7:58:00 AM2/25/15
to puppet...@googlegroups.com
Dear all

I would like to use the stdlib Modul with file_line to delete a line that matches *xyz*.
But the resource

file_line {'delete line':
  ensure => absent, 
  path => '/etc/file',

  line => '*xyz*',
}


or

file_line {'delete line':
  ensure => absent, 
  path => '/etc/file',

  match => '*xyz*',
}


does not work.

My current solution is with an exec resource but I don't like it.

Does anymbody know how to do it with file_line?

Andreas

Martin Alfke

unread,
Feb 25, 2015, 10:56:22 AM2/25/15
to puppet...@googlegroups.com
The single quotes take the argument as it is.
In this case you are looking for a line with *xyz*
Maybe you want to try double quotes.

hth,

Martin
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/bd72d951-bcb7-4091-879e-047d65d20407%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Garrett Honeycutt

unread,
Feb 25, 2015, 12:12:57 PM2/25/15
to puppet...@googlegroups.com
On 2/25/15 10:56 AM, Martin Alfke wrote:
> The single quotes take the argument as it is.
> In this case you are looking for a line with *xyz*
> Maybe you want to try double quotes.
>
> hth,
>
> Martin
>
> On 25 Feb 2015, at 13:58, Andreas Dvorak <andreas...@gmail.com> wrote:
>
>> Dear all
>>
>> I would like to use the stdlib Modul with file_line to delete a line that matches *xyz*.
>> But the resource
>>
>> file_line {'delete line':
>> ensure => absent,
>> path => '/etc/file',
>> line => '*xyz*',
>> }
>>
>> or
>>
>> file_line {'delete line':
>> ensure => absent,
>> path => '/etc/file',
>> match => '*xyz*',
>> }
>>
>> does not work.
>>
>> My current solution is with an exec resource but I don't like it.
>>
>> Does anymbody know how to do it with file_line?
>>
>> Andreas
>>

Hi,

The single quotes are good. The issue is with your regex. You are using
wildcards '*' which is not a valid regex. Try just 'xyz' without the
stars[1].

Recommend checking out Rubular[2] to test your regex's.

[1] - http://rubular.com/r/RjAnADs5x3
[2] - http://rubular.com/

Best regards,
-g

--
Garrett Honeycutt
@learnpuppet
Puppet Training with LearnPuppet.com
Mobile: +1.206.414.8658

Andreas Dvorak

unread,
Feb 26, 2015, 3:21:02 AM2/26/15
to puppet...@googlegroups.com
Dear all

now I tried

  file_line {'test':
    ensure => 'absent',
    path   => '/root/.ssh/authorized_keys',
    line   => 'test',
  }
Line ssh-rsa SNNwKzN/c1JmSlYOy4hK1yYsOJKOK+n5Vivt3DJGwxL3VpU= test@abc is still there

With
  file_line {'test':
    ensure => 'absent',
    path   => '/root/.ssh/authorized_keys',
    match  => 'test',
  }

I get the error Error: Failed to apply catalog: Validation of File_line[test] failed: Both line and path are required attributes at /data/git/test/modules/accounts/manifests/del_key_in_root.pp:204

With this the line is not removed.
  file_line {'test':
    ensure => 'absent',
    path   => '/root/.ssh/authorized_keys',
    line   => 'test',
    match  => 'test',
  }

Best regards
Andreas

Andreas Dvorak

unread,
Feb 26, 2015, 3:39:51 AM2/26/15
to puppet...@googlegroups.com
Hi,

I have just tried the stdlib version
4.5.1.
Acc
ording to the change log with version 4.5.0
"Don't require line to match the match parameter"
But my test does not work

  file_line {'test':
    ensure => 'absent',
    path   => '/root/.ssh/authorized_keys',
    match  => 'test',
  }

Error: Failed to apply catalog: Validation of File_line[test] failed: Both line and path are required attributes at /data/git/test/modules/accounts/manifests/del_key_in_root.pp:204
Wrapped exception:

Both line and path are required attributes

Andreas

jcbollinger

unread,
Feb 26, 2015, 9:52:22 AM2/26/15
to puppet...@googlegroups.com
You could solve that issue by providing a 'line' property with a dummy value, as in some of your previous attempts.  Alas, although that should prevent the exception, it will not get you to where you want to be.  File_line doesn't do that.

The key thing to understand here is that File_line represents a specific line in a file.  The 'match' parameter is not about recognizing that line, it is about how to introduce it into the file if that is necessary.  When you ensure a File_line 'absent', you are saying that the file identified by it's 'path' should not contain any line that is an exact, character-for-character match to the 'line' parameter (because that is what the resource represents).

For SSH authorized keys in particular, you should look into the built-in Ssh_authorized_key resource type.  More generally, if there is not already a module or built-in resource type specific to the file you're trying to manage, and you don't want to create one, then the more-or-less universal fallback is an Exec resource.


John

Andreas Dvorak

unread,
Feb 27, 2015, 6:11:45 AM2/27/15
to puppet...@googlegroups.com
Hi,

it is a shame, that file_line is not able to delete line on the basis of a substing.
Currently I use this to delete a line
  exec { "/usr/bin/perl -ni -e '/${line}/ or print' '${file}'":
    onlyif => "${grep} -qF '${line}' '${file}'"
  }


Best regards
Andreas
Reply all
Reply to author
Forward
0 new messages