[Puppet Users] Replace some text

2,198 views
Skip to first unread message

Marley Bacelar

unread,
May 21, 2010, 4:22:28 PM5/21/10
to puppet...@googlegroups.com
I need to replace some text in a file... there is some type, that i can pass a value and to be replace and the destination file? or anyone here have some class who does that?

--
Marley Bacelar
Project Fedora Ambassador
VCP, VSP. VTSP., ITILF, IBM 000-076, IBM 000-330, IBM 000-331
marley...@gmail.com

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Patrick

unread,
May 21, 2010, 7:48:33 PM5/21/10
to puppet...@googlegroups.com
You might want to check out Augeas.  It might be easier to use.

I've seen a simple define that does that, but I can't find it.  It had a grep statement in unless, and used sed to make the change.  A much more complicated example can be found at http://projects.puppetlabs.com/projects/puppet/wiki/Simple_Text_Patterns

Daniel Pittman

unread,
May 21, 2010, 9:08:11 PM5/21/10
to puppet...@googlegroups.com
Marley Bacelar <marley...@gmail.com> writes:

> I need to replace some text in a file... there is some type, that i can pass
> a value and to be replace and the destination file?

I know this sounds like a great idea, but decades of experience with puppet
and proceeding tools tell us that editing the content of files is very, very
hard to get right.

You would almost certainly be better off copying the right file content into
place, so that you don't have the extra complexity.


As an alternative, building the file from fragments with one of the
'concatenated files' classes is a pretty sound idea; it makes it easy have
variable sections inside the file without needing to edit-in-place.

> or anyone here have some class who does that?

There are a couple in the common module collections; check out the wiki for
the links off to 'em. Their current state may have changed since I last
looked, so I don't have a specific recommendation for you.

Daniel

Y'see, I learned that lesson way back when cfengine provided really good tools
for doing this sort of editing, and it *always* tool much longer and was much
less robust than I expected...

--
✣ Daniel Pittman ✉ dan...@rimspace.net+61 401 155 707
♽ made with 100 percent post-consumer electrons

Nigel Kersten

unread,
May 21, 2010, 9:23:25 PM5/21/10
to puppet...@googlegroups.com
On Fri, May 21, 2010 at 6:08 PM, Daniel Pittman <dan...@rimspace.net> wrote:
Marley Bacelar <marley...@gmail.com> writes:

> I need to replace some text in a file... there is some type, that i can pass
> a value and to be replace and the destination file?

I know this sounds like a great idea, but decades of experience with puppet
and proceeding tools tell us that editing the content of files is very, very
hard to get right.

You would almost certainly be better off copying the right file content into
place, so that you don't have the extra complexity.


As an alternative, building the file from fragments with one of the
'concatenated files' classes is a pretty sound idea; it makes it easy have
variable sections inside the file without needing to edit-in-place.

> or anyone here have some class who does that?

There are a couple in the common module collections; check out the wiki for
the links off to 'em.  Their current state may have changed since I last
looked, so I don't have a specific recommendation for you.

       Daniel

Y'see, I learned that lesson way back when cfengine provided really good tools
for doing this sort of editing, and it *always* tool much longer and was much
less robust than I expected...


The vast majority of our text replacement work we do is for files that have simple key/value pairs with an assignment operator.

foo=bar
foo: bar

etc.

We occasionally stray outside this with a regexp replacer, but I totally agree with Daniel here, it's not the most robust thing in the world.

Generally we do this because we want to allow people to customize extra parts of their config files, and we've switched daemons entirely for some services, simply based upon their ability to cope with a parts.d directory or to have "include" directives of some kind.

That allows you to ship an absolute config with a default include that people are free to modify. 

R.I.Pienaar

unread,
May 21, 2010, 9:57:55 PM5/21/10
to puppet...@googlegroups.com
> The vast majority of our text replacement work we do is for files that
> have simple key/value pairs with an assignment operator.
>
>
> foo=bar
> foo: bar
>
>
> etc.
>
>
> We occasionally stray outside this with a regexp replacer, but I
> totally agree with Daniel here, it's not the most robust thing in the
> world.
>
>
> Generally we do this because we want to allow people to customize
> extra parts of their config files, and we've switched daemons entirely
> for some services, simply based upon their ability to cope with a
> parts.d directory or to have "include" directives of some kind.
>
>
> That allows you to ship an absolute config with a default include that
> people are free to modify.
>


fwiw, the newest version of my concat module supports symlinking into a concat file, so if you have a config file that you would like users to drop settings it and you want them to only do so in a very specific place in a file you can now achieve that by building your config file and including a user editable file right where you want it.

Very nice feature to give users some rights without loosing control of the file or its structure.

It wouldn't be too hard to extend it to make arbitrary user supplied .d directories for daemons that dont support those :)

http://github.com/ripienaar/puppet-concat


--
R.I.Pienaar

Marley Bacelar

unread,
May 21, 2010, 10:28:58 PM5/21/10
to puppet...@googlegroups.com
Nice... I solved my probleman using the:
 
define replace($file, $pattern, $replacement) {
  exec { "/usr/bin/perl -pi -e 's/$pattern/$replacement/' '$file'":
      onlyif => "/usr/bin/perl -ne 'BEGIN { \$ret = 1; } \$ret = 0 if /$pattern/ && ! /$replacement/ ; END { exit \$ret; }' '$file'",
   }
}
 
Then i my class used:
 
replace { "/etc/bashrc":
   file => "/etc/bashrc",
   pattern => "PS1",
   replacement => "PS1 DOMAINA.COM"
}
Worked perfectly... Know i will see the other options proposed here. Thank you evry much guys
 
--
Marley Bacelar
Project Fedora Ambassador
VCP, VSP. VTSP., ITILF, IBM 000-076, IBM 000-330, IBM 000-331
marley...@gmail.com

2010/5/21 R.I.Pienaar <r...@devco.net>

KRouth Clinipace

unread,
Apr 8, 2019, 1:23:58 PM4/8/19
to Puppet Users
Question - is it possible to use the puppet regsubst function in place of the exec used here (to save spawning a new shell process) ?
kevin
2010/5/21 R.I.Pienaar <r...@devco.net>
To unsubscribe from this group, send email to puppet...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.





--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet...@googlegroups.com.

Henrik Lindberg

unread,
Apr 8, 2019, 2:29:43 PM4/8/19
to puppet...@googlegroups.com
On 2019-04-07 14:47, KRouth Clinipace wrote:
> Question - is it possible to use the puppet regsubst function in place
> of the exec used here (to save spawning a new shell process) ?
> kevin
>

Well, functions are ordinary on the compiling side. If you can figure
out what to do when compiling that is preferable.

Alternatively, you can call functions on the agent side by using a
Deferred value that delays the call and makes it on the agent side.
You need Puppet 6 to be able to do that. (And if you need custom logic
you can write your own function in Ruby using the function API).

To answer if there is a better way we need to understand what you are
actually doing (I did not try to make sense of what your exec and the
perl logic actually does...

Best
- henrik

> On Friday, May 21, 2010 at 10:28:58 PM UTC-4, Marley Bacelar wrote:
>
> Nice... I solved my probleman using the:
> define replace($file, $pattern, $replacement) {
>   exec { "/usr/bin/perl -pi -e 's/$pattern/$replacement/' '$file'":
>       onlyif => "/usr/bin/perl -ne 'BEGIN { \$ret = 1; } \$ret = 0
> if /$pattern/ && ! /$replacement/ ; END { exit \$ret; }' '$file'",
>    }
> }
> Then i my class used:
> replace { "/etc/bashrc":
>    file => "/etc/bashrc",
>    pattern => "PS1",
>    replacement => "PS1 DOMAINA.COM <http://DOMAINA.COM>"
> }
> Worked perfectly... Know i will see the other options proposed here.
> Thank you evry much guys
> --
> Marley Bacelar
> Project Fedora Ambassador
> VCP, VSP. VTSP., ITILF, IBM 000-076, IBM 000-330, IBM 000-331
> marley...@gmail.com <javascript:>
>
> 2010/5/21 R.I.Pienaar <r...@devco.net <javascript:>>
> <javascript:>.
> To unsubscribe from this group, send email to
> puppet...@googlegroups.com <javascript:>.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en
> <http://groups.google.com/group/puppet-users?hl=en>.
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To post to this group, send email to puppet...@googlegroups.com
> <javascript:>.
> To unsubscribe from this group, send email to
> puppet...@googlegroups.com <javascript:>.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en
> <http://groups.google.com/group/puppet-users?hl=en>.
>
> --
> 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
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/efa23924-e6eb-4338-bdb6-648d01923cee%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/efa23924-e6eb-4338-bdb6-648d01923cee%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Ben Ford

unread,
Apr 8, 2019, 2:57:13 PM4/8/19
to puppet...@googlegroups.com
It looks like what you're trying to do is inline editing of /etc/bashrc, but still let people poke at it themselves. That's a fragile position to be in. For example, given your regex solution, what's to stop someone from doing something like innocently defining their $PS1 iteratively? Something like this made up example:

PS1=$(run some command)
PS1="$(run some other command)/${PS1}"
export PS1="(${PS1}) \u@\h: \W: "

Or maybe they want to set another variable, maybe something like:

export UPS1_STATE="some such or other"

You'll end up with garbled variables at best, or a crashed script and then complaints that you broke their system. (maybe you'll even get lucky and they'll be tickets! lol)

Instead, a far far far more maintainable pattern is for you to flat out own /etc/bashrc, but to source a second  /etc/bashrc.local user editable file from within. That lets you manage it as a template which will give you a known and repeatable output, and let the users do their own customization in a place that they own, meaning that they also own any failures they create.


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/efa23924-e6eb-4338-bdb6-648d01923cee%40googlegroups.com.

Dirk Heinrichs

unread,
Apr 9, 2019, 1:28:59 AM4/9/19
to puppet...@googlegroups.com
Am Sonntag, den 07.04.2019, 05:47 -0700 schrieb KRouth Clinipace:

Know i will see the other options proposed here

puppetlabs::stdlib -> file_line

Why reinvent the wheel?

Bye...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer Gordon Davies, Madhu Ranganathan, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

KRouth Clinipace

unread,
Apr 9, 2019, 6:27:39 AM4/9/19
to puppet...@googlegroups.com
My experience with using file_line (i use it elsewhere) is that it replaces the entire line. (maybe this behavior has been changed, we are running Puppet 3.8). I am looking for a way to substitute a part of the line, leaving the rest intact.

thanks :)
kevin
Reply all
Reply to author
Forward
0 new messages