简体   繁体   English

将陈述添加到人偶类清单中

[英]add if statment into puppet class manifest

I am very new to Puppet programming. 我是Puppet编程的新手。

I have the following class which configures the resolv.conf file correctly: 我有以下用于正确配置resolv.conf文件的类:

class dnsclient                 (
  $nameservers                 = [ 'ns1 ip',
                                   'ns2 ip' ,
                                   'ns3 ip' ],
  $options                     = [ 'rotate',
                                   'timeout:5',
                                   'attempts:3'     ],
  $search                      = [ 'example.com',
                                   'example2.com'   ],
  $domain                      = 'UNSET',
  $sortlist                    = ['UNSET'],
  $resolver_config_file        = '/etc/resolv.conf',
  $resolver_config_file_ensure = 'file',
  $resolver_config_file_owner  = 'root',
  $resolver_config_file_group  = 'root',
  $resolver_config_file_mode   = '0644',
) {

  # Validates domain
  if is_domain_name($domain) != true {
    fail("Domain name, ${domain}, is invalid.")
  }

  # Validates $resolver_config_file_ensure
  case $resolver_config_file_ensure {
    'file', 'present', 'absent': {
      # noop, these values are valid
    }
    default: {
      fail("Valid values for \$resolver_config_file_ensure are \'absent\', \'file\', or \'present\'. Specified value is ${resolver_config_file_ensure}")
    }
  }

  file { 'dnsclient_resolver_config_file':
    ensure  => $resolver_config_file_ensure,
    content => template('dnsclient/resolv.conf.erb'),
    path    => $resolver_config_file,
    owner   => $resolver_config_file_owner,
    group   => $resolver_config_file_group,
    mode    => $resolver_config_file_mode,
  }
}

I created a custom fact default_gateway which should find the client default gateway, and according to that I want to then assign different name servers. 我创建了一个自定义事实default_gateway ,它应该找到客户端的默认网关,并据此我想分配不同的名称服务器。

I tried to enter the condition at the beginning of the dnsclient class: 我试图在dnsclient类的开头输入条件:

class dnsclient 
    if $default_gateway == 'DG ip' {
      $nameservers                 = [ 'ns1 ip',
                                       'ns2 ip' ,
                                       'ns3 ip' ],
  }

But I am getting a syntax error on the client, please advise. 但是我在客户端遇到语法错误,请告知。 Where do I place the if statement to assign different name servers if the default gateway is different. 如果默认网关不同,则在何处放置if语句以分配不同的名称服务器。

I assume you need to also remove the $nameservers class parameter, so you would change the beginning of the file to: 我假设您还需要删除$nameservers类参数,因此您可以将文件的开头更改为:

class dnsclient                 (

  # This parameter is no longer required?
  # $nameservers                 = [ 'ns1 ip',
  #                                  'ns2 ip' ,
  #                                  'ns3 ip' ],

  $options                     = [ 'rotate',
                                   'timeout:5',
                                   'attempts:3'     ],
  $search                      = [ 'example.com',
                                   'example2.com'   ],
  $domain                      = 'UNSET',
  $sortlist                    = ['UNSET'],
  $resolver_config_file        = '/etc/resolv.conf',
  $resolver_config_file_ensure = 'file',
  $resolver_config_file_owner  = 'root',
  $resolver_config_file_group  = 'root',
  $resolver_config_file_mode   = '0644',
) {

  # Your code goes here:
  if $default_gateway == 'DG ip' {
    $nameservers = ['ns1 ip', 'ns2 ip', 'ns3 ip'],
  }

...

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

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