简体   繁体   English

如何在Puppet类创建中添加if语句

[英]How to add an if statement inside a Puppet class creation

I have a Puppet manifest which creates a class, with another class inside it: 我有一个Puppet清单,该清单创建一个类,其中包含另一个类:

class profiles::common::wlsdomains {
    class {"wlsdomain":
            applications      => hiera('wlsapplications', ""),
            crontab           => hiera_hash('crontab', ""),
            generateWlsdomain => hiera('generatewlsdomain'), # This line needs changing
    }
}

So far so good. 到现在为止还挺好。 Now, the problem is the last line. 现在,问题出在最后一行。 I only want to set "generateWlsdomain" to "hiera('generatewlsdomain')" under certain conditions . 在某些情况下,我只想将“ generateWlsdomain”设置为“ hiera('generatewlsdomain')”。 This is where the need for an "if" statement comes in. I'd like to replace that line with something like this: 这是需要“ if”语句的地方。我想用以下内容替换该行:

if $::generate == "true" {
    generatewlsdomain => "true"
}
elsif $::generate == "false" {
    # Do nothing; leave generatewlsdomain unset
}
else { # If $::generate is neither "true" nor "false", then get a value from Hiera
    generateWlsdomain => hiera('generatewlsdomain'),
}

In other words, I want to primarily look at $::generate to decide what to do, but use a value from Hiera as backup if $::generate isn't set. 换句话说,我主要考虑$ :: generate来决定要做什么,但是如果未设置$ :: generate,则使用Hiera中的值作为备份。

However, something tells me I can't just replace the existing line with this code block, because the existing line is part of a comma-separated list , it's not just a generic code line of its own. 但是,有些事情告诉我,我不能只用该代码块替换现有行,因为现有行是逗号分隔列表的一部分 ,而不仅仅是它自己的通用代码行。

In case it matters, this is running on a RedHat machine. 如果很重要,它将在RedHat计算机上运行。

The undef keyword can be used as the value if you don't want to set it, which will cause Puppet to use the default value for generateWlsdomain . 如果您不想设置undef关键字,则可以将其用作值,这将导致Puppet对generateWlsdomain使用默认值。

Using this, set a variable for the value of generateWlsdomain : 使用此方法,为generateWlsdomain的值设置一个变量:

if $::generate == "true" {
    $generatewlsdomain = "true"
}
elsif $::generate == "false" {
    $generatewlsdomain = undef # Do nothing; leave generatewlsdomain unset
}
else { # If $::generate is neither "true" nor "false", then get a value from Hiera
    $generatewlsdomain = hiera('generatewlsdomain')
}

or a bit more compact, using a selector to test $::generate values and set $generatewlsdomain with the result: 或更紧凑一点,使用选择器测试$::generate值并使用结果设置$generatewlsdomain

$generatewlsdomain = $::generate ? {
  "true"  => "true",
  "false" => undef, # Do nothing; leave generatewlsdomain unset
  default => hiera('generatewlsdomain'), # If neither "true" nor "false", then get a value from Hiera
}

and pass it into the existing class declaration: 并将其传递到现有的类声明中:

class {"wlsdomain":
  applications      => hiera('wlsapplications', ""),
  crontab           => hiera_hash('crontab', ""),
  generateWlsdomain => $generatewlsdomain,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM