Augeas lens to change an arbitrary line in a file?

337 views
Skip to first unread message

Joachim Schrod

unread,
Apr 21, 2014, 6:25:13 PM4/21/14
to puppet...@googlegroups.com
Hi,

I'm new to Puppet, and also new to Augeas.

I want to change a line in a file. In particular, the file is
/usr/share/ghostscript/${gs_version}/Resource/Init/gs_init.ps
and the line is
% /DEFAULTPAPERSIZE (a4) def
that I want to change into
/DEFAULTPAPERSIZE (a4) def

Is Augeas the right tool for such a task? I looked through its
available lenses, but there ain't no Postscript lens.

Of course, the other approach would be to use an exec resource to
change that line.

What is best practice?

Joachim

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Joachim Schrod, Roedermark, Germany
Email: jsc...@acm.org

Mike Langhorst

unread,
Apr 22, 2014, 12:32:14 AM4/22/14
to puppet...@googlegroups.com, jsc...@acm.org
There's also the file_line resource that's available with the std lib module, that might work for you as well.

Kenton Brede

unread,
Apr 22, 2014, 12:14:10 PM4/22/14
to puppet...@googlegroups.com
I'm no expert with augeas but after looking at the gs_init.ps file, I don't know of any current lens that could be easily modified to work with it.  I agree with Mike, I'd either use file_line or an exec.

File_line is part of stdlib.  Click on the "types" tab on this page for an example.

https://forge.puppetlabs.com/puppetlabs/stdlib

Kent


--
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/6b6b743b-3a6b-45a0-bc40-eec291688374%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Kent Brede




Joachim Schrod

unread,
Apr 22, 2014, 8:20:39 PM4/22/14
to puppet...@googlegroups.com
On 04/22/14 18:14, Kenton Brede wrote:
> I'm no expert with augeas but after looking at the gs_init.ps
> <http://gs_init.ps> file, I don't know of any current lens that
> could be easily modified to work with it. I agree with Mike, I'd
> either use file_line or an exec.
>
> File_line is part of stdlib. Click on the "types" tab on this page
> for an example.
>
> https://forge.puppetlabs.com/puppetlabs/stdlib

Thanks to Mike and to you for that hint.
Well, but it doesn't work as I expected.

I use the following resource declaration:

# Ghostscript default paper size
file_line { 'Ghostscript A4' :
path => '/usr/share/ghostscript/9.05/Resource/Init/gs_init.ps',
match => '^% /DEFAULTPAPERSIZE \(a4\)',
line => '/DEFAULTPAPERSIZE (a4) def'
}

The error message is

Error: Failed to apply catalog: Validation of File_line[Ghostscript A4] failed: When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter at /etc/puppet/modules/npc/manifests/commons.pp:30
Wrapped exception:
When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter


According to https://forge.puppetlabs.com/puppetlabs/stdlib; tab Types, it's

match

An optional regular expression to run against existing lines in the file;\nif a match is found, we replace that line rather than adding a new line.
line


I don't understand that error message.

Hopefully, my intent is clear: As long as the default GS paper size
is commented out, I want a replacement of that line with the new line.

According to (my understanding) of stdlib::file_line, that should be the way it works:
-- If the commented-out line is matched, it's substituted by the
new line.
-- If it's not matched, the new line is added unless it's present.

That's my interpretation of the documentation. AFAICS, reality beats me.
match wants a line as a result that's the outcome of the change that match
is the solicitor to.

I'd appreciate any hints or explanations what I'm doing wrong.

Best,
Joachim

FWIW: puppet 3.3.1 on openSUSE. (open)SUSE is abandanoned by / has abandonded
Puppet. The so-called maintainer announced that he was forced to take over the
packages due to a leaving collegue and is looking for a new community-based
maintainer. Nobody stood up. (Not too suprising, deployment management is not
really the realm of laptop-minded developers like many people that we find at
openSUSE. Data-center topics like Puppet doesn't seem to be important there.)
Tickets (even with patches; e.g., by me) are not worked at / acted upon any more.

Thus, I would prefer a hint or a direction that works with an older version
of Puppet (3.3.x) as well. If that's not possible, the statement that it works
with 3.5.x is fine as well -- that simply means that we have to check if our
customer's plan to use Puppet is still ok with (open)SUSE, if they will fund
it.

Gabriel Filion

unread,
Apr 23, 2014, 1:54:34 AM4/23/14
to puppet...@googlegroups.com
On 22/04/14 08:20 PM, Joachim Schrod wrote:
> # Ghostscript default paper size
> file_line { 'Ghostscript A4' :
> path => '/usr/share/ghostscript/9.05/Resource/Init/gs_init.ps',
> match => '^% /DEFAULTPAPERSIZE \(a4\)',
> line => '/DEFAULTPAPERSIZE (a4) def'
> }
>
> The error message is
>
> Error: Failed to apply catalog: Validation of File_line[Ghostscript A4] failed: When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter at /etc/puppet/modules/npc/manifests/commons.pp:30
> Wrapped exception:
> When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter

maybe by "regex" it means that you need to enclose it in / characters:

http://www.ruby-doc.org/core-2.1.1/Regexp.html

e.g. : match => /^% /DEFAULTPAPERSIZE \(a4\)/,

I'm not sure whether you need the quotes, too.. I'd try without first..

--
Gabriel Filion

signature.asc

Joachim Schrod

unread,
Apr 23, 2014, 4:29:00 AM4/23/14
to puppet...@googlegroups.com
Without quotes, the error message is

Syntax error at '/'; expected '}'

With quotes and slashes, the error message is the same as before,
quoted above.

I don't understand why the match regex must match the line value.

Joachim

Ian Mortimer

unread,
Apr 24, 2014, 12:37:29 AM4/24/14
to puppet...@googlegroups.com
On 04/23/14 18:29, Joachim Schrod wrote:

> I don't understand why the match regex must match the line value.

Your match needs to be:

match => '^(% )*/DEFAULTPAPERSIZE \(a4\)',

It has to match the line value, otherwise on the next puppet run
your modified line won't match and puppet will insert a new
(unwanted) line at the end of the file .


--
Ian

Joachim Schrod

unread,
Apr 24, 2014, 12:55:27 PM4/24/14
to puppet...@googlegroups.com
Ah, match replaces the base functionality that "matches the full
line, including whitespace at the beginning and end. If the line is
not contained in the given file, Puppet will add the line to ensure
the desired state."

I thought the match regex would be an _additional_ test, and that
the check for existance of the line would be done nevertheless.

If anybody here has write access to the file_line docs: It would be
nice to note that in the description of "match". For this newbie,
this was not clear.

Thanks for the explanation,
Reply all
Reply to author
Forward
0 new messages