How to apply template based on a string in a file

44 views
Skip to first unread message

Ray

unread,
Feb 13, 2014, 6:18:10 PM2/13/14
to puppet...@googlegroups.com
Newbie question:  
I need to be able to apply a specific template based on whether a string is found in an 
existing file on the client or not. I tried the following (found an example somewhere... it does not work.. always falls into the first condition)

What would be the correct way to check for the value in the file?


 if "grep STANDARD /var/adm/buildIT/ServerBuild.dat 2>/dev/null"
    {
      file { '/etc/openssh/sshd_config':
        ensure  => file,
        backup  => false,
        content => template("sshdconfig/sshdconfigsol.erb"),
        notify => Service['ssh']
      }

      service { 'ssh':
        ensure => running,
        enable => true,
        hasstatus => true,
        hasrestart => true,
      }
    }
    else
    {
      file { '/etc/openssh/sshd_config':
        ensure  => file,
        backup  => false,
        content => template("sshdconfig/sshdconfigsolpm.erb"),
        notify => Service['ssh']
      }

      service { 'ssh':
        ensure => running,
        enable => true,
        hasstatus => true,
        hasrestart => true,
      }
  }

Johan De Wit

unread,
Feb 14, 2014, 8:12:44 AM2/14/14
to puppet...@googlegroups.com
If you need information from the client, then you should think 'facter'.

Or you could write a custom fact, or use external facts.

http://puppetlabs.com/blog/facter-1-7-introduces-external-facts
http://docs.puppetlabs.com/guides/custom_facts.html

HTH

Jo
--
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/e502a7ac-cebc-4454-900e-d504f2a89956%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


-- 
Johan De Wit

Open Source Consultant

Red Hat Certified Engineer         (805008667232363)
Puppet Certified Professional 2013 (PCP0000006)
_________________________________________________________
 
Open-Future                 Phone     +32 (0)2/255 70 70
Zavelstraat 72              Fax       +32 (0)2/255 70 71
3071 KORTENBERG             Mobile    +32 (0)474/42 40 73
BELGIUM                     http://www.open-future.be
_________________________________________________________
 

Upcoming Events:

Zabbix Certified Training | http://www.open-future.be/zabbix-certified-training-10-till-12th-march

Zabbix for Large Environments Training | http://www.open-future.be/zabbix-large-environments-training-13-till-14th-march

Puppet Introduction Course | http://www.open-future.be/puppet-introduction-course-14th-april

Puppet Advanced Training | http://www.open-future.be/puppet-advanced-training-15-till-17th-april

Subscribe to our newsletter: http://eepurl.com/BUG8H


Denmat

unread,
Feb 15, 2014, 12:32:46 AM2/15/14
to puppet...@googlegroups.com
Or place those values in hiera. 

José Luis Ledesma

unread,
Feb 15, 2014, 6:54:35 AM2/15/14
to puppet...@googlegroups.com
Hi,

I think that the problem here  is a big misunderstood about how puppet works. Puppet DSL is a declarative language, you do not have to tell what to do (grep "xxx" on file "yyy"), you have to tell which final state do you want. Obviously this final state may depend on data (facts/hiera) but never on an action (grep)

Moreover, puppet manifests are compiled in the puppetmaster (so  if it would be possible to do a grep or any other action, it would be made on the master).

The problem you are facing is that the code

if "any string that you may want"

is always true (at least the string is empty) that's why always falls in the first condition.

regards,


--
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/e502a7ac-cebc-4454-900e-d504f2a89956%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
José Luis Ledesma
Reply all
Reply to author
Forward
0 new messages