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

时间:2018-06-24 12:06:25

标签: puppet

我对Puppet编程很陌生。

我有下面的类可正确配置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,
  }
}

我创建了一个自定义事实default_gateway,该事实应找到客户端的默认网关,并据此我想分配其他名称服务器。

我试图在dnsclient类的开头输入条件:

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

但是我在客户端遇到语法错误,请告知。如果默认网关不同,我应该在哪里放置if语句来分配不同的名称服务器。

1 个答案:

答案 0 :(得分:0)

我认为您还需要删除$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'],
  }

...