puppet exec to test if pattern exist in file

3,089 views
Skip to first unread message

Andrew Morgan

unread,
Jun 29, 2016, 12:19:02 PM6/29/16
to Puppet Users
I want to check if pattern exist in a file and then if it doesn;t echo  the pattern in the file.

class environment {

        exec{'one_run':
                path=>'/usr/bin',
                command => "echo  'PATH=$PATH:/opt/logstash/bin' >> /etc/profile",
                onlyif => "grep -qFx 'PATH=$PATH:/opt/logstash/bin' /etc/profile",
                }
}

The above doesn't work,can anyone help with this please? I want to check if PATH=$PATH:/opt/logstash/bin' exists in /etc/profile and if not input it only once.

Peter Kristolaitis

unread,
Jun 29, 2016, 12:38:35 PM6/29/16
to puppet...@googlegroups.com
First of all, I will say that this is probably a sledgehammer approach to accomplishing your goal.  Many modern platforms have, in their default /etc/profile, support for /etc/profile.d/ that works like a run-parts for building the environment.  The Puppet Agent package installs its own $PATH entry in this way on both CentOS and Ubuntu.     So you're probably better off managing a file in that location so that it gets auto-included by /etc/profile.

Secondly, your problem is variable interpolation.   Because you're using double quotes around the command and onlyif statments, Puppet is interpolating $PATH to be (probably) an empty string, rather than a literal "$PATH".  You need to either change to single quotes (and then adjust the quoting around your grep match string) or escape the $ with \$.

Finally, if you were to use the above solution, it's still a bit of a sledgehammer because you're not really modeling state.  The best solution would actually be to use something like Augeas to examine the file and modify as necessary.  "exec / onlyif" and "exec / unless" are kind of measures-of-last-resort in the Puppet world (now, having said that, I must admit that I'm completely guilty of using it myself on occasion  :p  ).
--
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/1b36ab52-29ee-455f-8efb-8d647fbd6c9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Morgan

unread,
Jun 29, 2016, 1:06:32 PM6/29/16
to Puppet Users
Thank you very much for your prompt reply...

Will this work??

class environment {

        exec{'oe_run':
                path=>'/usr/bin',
                 command => 'echo  PATH=\$PATH:/opt/logstash/bin >> /etc/profile',
                  unless => "grep -qFx 'PATH=\$PATH:/opt/logstash/bin' /etc/profile",

Martin Alfke

unread,
Jun 30, 2016, 6:49:09 AM6/30/16
to puppet...@googlegroups.com
Hi Alonso,

On 29 Jun 2016, at 19:06, Andrew Morgan <alonso...@gmail.com> wrote:

> Thank you very much for your prompt reply...
>
> Will this work??
>
> class environment {
>
> exec{'oe_run':
> path=>'/usr/bin',
> command => 'echo PATH=\$PATH:/opt/logstash/bin >> /etc/profile',
> unless => "grep -qFx 'PATH=\$PATH:/opt/logstash/bin' /etc/profile",
> }
>
as Peter already mentioned:
this is not a Puppet style approach on declaring your system state.
You do workflow based configuration.

It would be better to either use the file_line or augeas resource type.

With file_line you would do the following:

file_line { 'logstash path to profile’:
ensure => present,
path => ‘/etc/profile’,
line => ‘PATH=$PATH:/opt/logstash/bin’,
}

By using the file_line resource type you will make use of puppet built in idempotency (checking whether a change is required - change only in case that the actual state does not comply with desired state).
When using an exec resource type you are required to:
- verify behavior in case of errors and
- perform idempotency checks

Best,
Martin


> On Wednesday, 29 June 2016 12:38:35 UTC-4, Peter Kristolaitis wrote:
> First of all, I will say that this is probably a sledgehammer approach to accomplishing your goal. Many modern platforms have, in their default /etc/profile, support for /etc/profile.d/ that works like a run-parts for building the environment. The Puppet Agent package installs its own $PATH entry in this way on both CentOS and Ubuntu. So you're probably better off managing a file in that location so that it gets auto-included by /etc/profile.
>
> Secondly, your problem is variable interpolation. Because you're using double quotes around the command and onlyif statments, Puppet is interpolating $PATH to be (probably) an empty string, rather than a literal "$PATH". You need to either change to single quotes (and then adjust the quoting around your grep match string) or escape the $ with \$.
>
> Finally, if you were to use the above solution, it's still a bit of a sledgehammer because you're not really modeling state. The best solution would actually be to use something like Augeas to examine the file and modify as necessary. "exec / onlyif" and "exec / unless" are kind of measures-of-last-resort in the Puppet world (now, having said that, I must admit that I'm completely guilty of using it myself on occasion :p ).
>
>
> On 2016-06-29 12:19 PM, Andrew Morgan wrote:
>> I want to check if pattern exist in a file and then if it doesn;t echo the pattern in the file.
>>
>> class environment {
>>
>> exec{'one_run':
>> path=>'/usr/bin',
>> command => "echo 'PATH=$PATH:/opt/logstash/bin' >> /etc/profile",
>> onlyif => "grep -qFx 'PATH=$PATH:/opt/logstash/bin' /etc/profile",
>> }
>> }
>>
>> The above doesn't work,can anyone help with this please? I want to check if PATH=$PATH:/opt/logstash/bin' exists in /etc/profile and if not input it only once.
>> --
>> 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/1b36ab52-29ee-455f-8efb-8d647fbd6c9c%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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/8d014b64-242d-4ef5-bc27-dfad20418c23%40googlegroups.com.

Andrew Morgan

unread,
Jun 30, 2016, 10:23:03 AM6/30/16
to Puppet Users
Thank you , my solution works, but will use yours as it is best practice.
Reply all
Reply to author
Forward
0 new messages