if then statement within file resource

7,228 views
Skip to first unread message

CraftyTech

unread,
Dec 17, 2010, 3:51:26 PM12/17/10
to puppet...@googlegroups.com
Hello All,

     I'm not able to do an if/then statement within a file resource declaration.  I'm basically trying to distinguish between OS release, so that I can assign an appropriate template.  This is what I have:

class basic_dev::files {
        file { "/etc/sudoers":
        owner => root,
        group => root,
        mode => 0440,
        if $operatingsystemrelease <= 5.4 then {
        content => template("system/sudoers_V54.erb")},
        else {
        content => template("system/sudoers.erb")},
       backup => ".bak"
       }
For some reason is not working.  I keep on getting error: "Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at 'if'; expected '}' "

Any ideas?

Thanks,


Disconnect

unread,
Dec 17, 2010, 3:58:43 PM12/17/10
to puppet...@googlegroups.com
You can do it magically with source, not sure about template though.
 source => ["puppet:///system/sudoers_$operatingsysstemrelease","puppet:///system/sudoers"]
(it will look for them in order. We have several that go hostname, $lsbdistcodename, generic.)

If it doesn't work for template, you can do it the hard way:

  if $operatingsystemrelease <= 5.4 {
    file { "/etc/sudoers":....etc
  } else {
    file { "/etc/sudoers":.....
  }


--
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.

Mark Stanislav

unread,
Dec 17, 2010, 3:57:50 PM12/17/10
to puppet...@googlegroups.com
What about just doing:

if ($operatingsystemrelease <= 5.4) {
$sudo_template = "system/sudoers_V54.erb"
} else {
$sudo_template = "system/sudoers.erb"
}

class basic_dev::files {
file { "/etc/sudoers":
owner => root,
group => root,
mode => 0440,

content => template($sudo_template),
backup => ".bak"
}

Seems legit at least ;)

Eric Sorenson

unread,
Dec 17, 2010, 4:04:14 PM12/17/10
to puppet...@googlegroups.com

On Dec 17, 2010, at 12:51 PM, CraftyTech wrote:

    I'm not able to do an if/then statement within a file resource declaration.  I'm basically trying to distinguish between OS release, so that I can assign an appropriate template.  This is what I have:

Yes the language tutorial on conditionals explains this.  You can use a selector inside the resource or the if/else syntax outside the resource to set a variable. Selector is less nice because you can't do the numeric comparison, you have to fake it with a regexp.

ie.

file {"/etc/sudoers":
 content => $operatingsystemrelease ?
 /^5.[01234]/ => template("system/sudoers_V54.erb"),
 default => template("system/sudeors.erb")
 }

or as other people described, move your if/else outside the resource to set a local variable that you then expand.


 - Eric Sorenson - N37 17.255 W121 55.738  - http://twitter.com/ahpook  -

CraftyTech

unread,
Dec 17, 2010, 4:09:52 PM12/17/10
to puppet...@googlegroups.com
That worked out.... Wondering why it didn't inside the file resource...  but it the workaround is certainly is "legit" enough....

Thanks,


svrs...@gmail.com

unread,
Sep 5, 2013, 8:04:44 PM9/5/13
to puppet...@googlegroups.com
Hi
i am trying the same it is not working can u explain me how it worked

file { "/etc/inittab":

        owner => root,
        group => root,
        mode => 0644,
        if $manufacturer=IBM {
        content => template("${module_name}/ibm.inittab.erb")},
        else {
        content => template("${module_name}/sun.inittab.erb")},
       backup => ".bak"

Frederiko Costa

unread,
Sep 5, 2013, 8:57:23 PM9/5/13
to puppet...@googlegroups.com
Hi,

I believe in that case you should use selectors to make it work.


Or you can use the 'if' outside the file resource.  Something like this:

$template_name = $manufacter ? {
  'ibm' => "${module_name}/ibm.inittab.erb",
  default => "${module_name}/sun.inittab.erb",
}

Then your file resource
file { "/etc/inittab":
        owner => root,
        group => root,
        mode => 0644,
        content => template($template_name),
       backup => ".bak",
}

It's also best practice to use selectors outside resource block per http://puppet-lint.com/checks/selector_inside_resource/

I hope it helps.


-frederiko



--
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 post to this group, send email to puppet...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages