How to check a text file for a certain line?

409 views
Skip to first unread message

Dan Pasacrita

unread,
Apr 21, 2014, 11:18:41 AM4/21/14
to puppet...@googlegroups.com
Hi everyone, I'm pretty new to puppet, and I was wondering if you guys to help me with something pretty basic. I'm attempting to write a module that will check a text file for a certain line of text, and if it doesn't find it, to add a that content in a new line.

I'm pretty sure I can add the new line using exec to run echo "content" >> file.txt, but checking the file for said content has me completely stumped. What can I do here?

Kenton Brede

unread,
Apr 21, 2014, 1:42:41 PM4/21/14
to puppet...@googlegroups.com
Look into using "file_line," it's part of stdlib.

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

If you want to check for an existing line, using an "exec" you can use "onlyif" or "unless."

Something like:

unless => "grep myline /path/to/file",
Kent


On Mon, Apr 21, 2014 at 10:18 AM, Dan Pasacrita <whitew...@gmail.com> wrote:
Hi everyone, I'm pretty new to puppet, and I was wondering if you guys to help me with something pretty basic. I'm attempting to write a module that will check a text file for a certain line of text, and if it doesn't find it, to add a that content in a new line.

I'm pretty sure I can add the new line using exec to run echo "content" >> file.txt, but checking the file for said content has me completely stumped. What can I do here?

--
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/ff5d0baa-7adc-4162-983a-67bb010a3fb1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Kent Brede




Steven Kurylo

unread,
Apr 21, 2014, 1:41:30 PM4/21/14
to puppet...@googlegroups.com
On Mon, Apr 21, 2014 at 8:18 AM, Dan Pasacrita <whitew...@gmail.com> wrote:
Hi everyone, I'm pretty new to puppet, and I was wondering if you guys to help me with something pretty basic. I'm attempting to write a module that will check a text file for a certain line of text, and if it doesn't find it, to add a that content in a new line.

 
Here are some example of what you're trying to accomplish:

http://projects.puppetlabs.com/projects/puppet/wiki/Simple_Text_Patterns/5

exec { "/bin/echo '${line}' >> '${file}'":
                unless => "/bin/grep -qFx '${line}' '${file}'"
            }

--
Steven Kuryło

Joachim Schrod

unread,
Apr 21, 2014, 6:10:24 PM4/21/14
to puppet...@googlegroups.com
On 04/21/14 19:42, Kenton Brede wrote:
> Look into using "file_line," it's part of stdlib.
>
> https://forge.puppetlabs.com/puppetlabs/stdlib

Hmm, I don't detect any documentation of file_line at that URL.

Could you post another link with documentation?

Thanks,
Joachim

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

Andreas Ntaflos

unread,
Apr 21, 2014, 6:53:53 PM4/21/14
to puppet...@googlegroups.com
On 2014-04-22 00:10, Joachim Schrod wrote:
> On 04/21/14 19:42, Kenton Brede wrote:
>> Look into using "file_line," it's part of stdlib.
>>
>> https://forge.puppetlabs.com/puppetlabs/stdlib
>
> Hmm, I don't detect any documentation of file_line at that URL.
>
> Could you post another link with documentation?

Funny that it doesn't seem to be mentioned in the README anymore, but
here is the code and the documentation:

https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/lib/puppet/type/file_line.rb

Andreas

signature.asc

Kenton Brede

unread,
Apr 21, 2014, 7:41:38 PM4/21/14
to puppet...@googlegroups.com
Click on the "types" tab on that page and you'll see the documentation for "file_line."
Kent
--
Kent Brede




Joachim Schrod

unread,
Apr 22, 2014, 4:48:07 AM4/22/14
to puppet...@googlegroups.com
On 04/22/14 01:41, Kenton Brede wrote:
> On Mon, Apr 21, 2014 at 5:53 PM, Andreas Ntaflos
> <da...@pseudoterminal.org <mailto:da...@pseudoterminal.org>> wrote:
>
> On 2014-04-22 00:10, Joachim Schrod wrote:
> > On 04/21/14 19:42, Kenton Brede wrote:
> >> Look into using "file_line," it's part of stdlib.
> >>
> >> https://forge.puppetlabs.com/puppetlabs/stdlib
> >
> > Hmm, I don't detect any documentation of file_line at that URL.
> >
> > Could you post another link with documentation?
>
> Funny that it doesn't seem to be mentioned in the README
> anymore, but
> here is the code and the documentation:
>
> https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/lib/puppet/type/file_line.rb
>
> Click on the "types" tab on that page and you'll see the
> documentation for "file_line."

Thanks to both of you for that pointers; I'm still new to puppet
and need to find my way around.
Reply all
Reply to author
Forward
0 new messages