Puppet 从数组创建参数?

Puppet 从数组创建参数?,puppet,theforeman,Puppet,Theforeman,我是ruby新手,正在编写一个傀儡模块,可以通过Foreman访问 我编写它供Foreman的智能类参数使用,以便可以从Foreman web控制台配置它 我想看看如何为一台设备可能拥有的48个端口创建一个参数。我不想手动输入端口,而是想知道是否可以动态输入端口 例如,与此相反: class ciscobaseconfig ( $interface_description_lan = 'A LAN interface' ) { interface { 'FastEthernet 0

我是ruby新手,正在编写一个傀儡模块,可以通过Foreman访问

我编写它供Foreman的智能类参数使用,以便可以从Foreman web控制台配置它

我想看看如何为一台设备可能拥有的48个端口创建一个参数。我不想手动输入端口,而是想知道是否可以动态输入端口

例如,与此相反:

class ciscobaseconfig (
  $interface_description_lan = 'A LAN interface'
) {
  interface {
   'FastEthernet 0/1':
      description => $interface_description_lan
  }
  interface {
   'FastEthernet 0/2':
      description => $interface_description_lan
  }
}
我想这样做:

class ciscobaseconfig (
  $interface_description_lan = 'A LAN interface',
) {
  interface {
    (0..48).each do |i|
    "FastEthernet 0/#{i}":
      description => $interface_description_lan
    end
  }
}
根据一位评论者的建议,我尝试了这个方法,但没有效果:

class ciscobaseconfig (
  $interface_description_lan = 'A LAN interface',
) {

  arrInterfaces = Array(1..48)

  arrInterfaces.each{
    interface {
      |intNum| puts "FastEthernet 0/#{intNum}":
        description => $interface_description_lan
    }
  }
}

据我所知,您需要声明48个资源,使用基于资源索引的标题,并且所有资源都具有相同的参数值。当然,这必须在Puppet DSL中实现,尽管这与Ruby有一些相似之处,但它不是Ruby。这似乎造成了一些混乱

为此,安装模块非常有用,它提供了多种有用的扩展功能。在这里帮助我们的是
range()
。如果安装了stdlib,类似这样的功能应该可以实现:

class ciscobaseconfig (
    $interface_description_lan = 'A LAN interface',
  ) {

  each(range('1', '48')) |portnum| {
    interface { "FastEthernet 0/${portnum}":
      description => $interface_description_lan
    }
  }
}
这确实假设您使用的是Puppet 4,或者Puppet 3和未来的解析器。也可以使用标准的Puppet 3解析器完成,但没有那么干净:

class ciscobaseconfig (
    $interface_description_lan = 'A LAN interface',
  ) {

  $portnums = split(inline_template("<%= (1..48).to_a.join(',') %>"), ',')
  $ifc_names = regsubst($portnums, '.*', 'FastEthernet 0/\0')

  interface { $ifc_names:
      description => $interface_description_lan
  }
}
类ciscobaseconfig(
$interface\u description\u lan='A lan interface',
) {
$portnums=split(内联_模板(“”,“,”)
$ifc_names=regsubst($portnums,'%1!'','FastEthernet 0/\0')
接口{$ifc_名称:
description=>$interface\u description\u lan
}
}

请特别注意,当数组作为资源标题给出时,这意味着您要为数组的每个元素声明一个资源,所有元素都具有相同的参数。

据我所知,您要声明48个资源,使用基于资源索引的标题,并且所有元素都具有相同的参数值。当然,这必须在Puppet DSL中实现,尽管这与Ruby有一些相似之处,但它不是Ruby。这似乎造成了一些混乱

为此,安装模块非常有用,它提供了多种有用的扩展功能。在这里帮助我们的是
range()
。如果安装了stdlib,类似这样的功能应该可以实现:

class ciscobaseconfig (
    $interface_description_lan = 'A LAN interface',
  ) {

  each(range('1', '48')) |portnum| {
    interface { "FastEthernet 0/${portnum}":
      description => $interface_description_lan
    }
  }
}
这确实假设您使用的是Puppet 4,或者Puppet 3和未来的解析器。也可以使用标准的Puppet 3解析器完成,但没有那么干净:

class ciscobaseconfig (
    $interface_description_lan = 'A LAN interface',
  ) {

  $portnums = split(inline_template("<%= (1..48).to_a.join(',') %>"), ',')
  $ifc_names = regsubst($portnums, '.*', 'FastEthernet 0/\0')

  interface { $ifc_names:
      description => $interface_description_lan
  }
}
类ciscobaseconfig(
$interface\u description\u lan='A lan interface',
) {
$portnums=split(内联_模板(“”,“,”)
$ifc_names=regsubst($portnums,'%1!'','FastEthernet 0/\0')
接口{$ifc_名称:
description=>$interface\u description\u lan
}
}

请特别注意,当一个数组作为资源标题给出时,这意味着您要为数组的每个元素声明一个资源,所有元素都具有相同的参数。

==>>arr=array(1..10)=then=>arr.each{x | put“wadupp{x}这不是我正在做的事情吗?一个完整的例子会是什么样的?删除了[ruby]标记,因为它比这个问题更容易混淆。对不起,我对ruby和puppet非常陌生,所以不确定是ruby问题还是puppet DSL问题==>>arr=Array(1..10)=then=>arr.each{x|puts“wadupp{x}”,这不是我在做的同一件事吗?一个完整的例子会是什么样的?删除了[ruby]标记,因为它与这个问题相比更容易混淆。对不起,我对ruby和puppet非常陌生,所以不确定是ruby问题还是puppet DSL问题。我在我的模块文件夹中有stdlib,我可以在Foreman的puppet类下看到它,所以我猜它已经安装了?但是,当我用您的代码更新这个模块的init.pp文件并从Foreman运行import时,它没有看到它(表示“未检测到任何更改…”)。为了清楚,如果在本模块的init.pp中使用我的第一个示例中的代码,Foreman将毫无问题地上载它。@red888,您的第一个示例中的代码格式不正确,因此特定代码肯定没有您描述的结果。您以前使用的版本是否为所有48个端口声明了资源?如果您现在断言的状态与当前状态匹配,那么确实不应该有任何更改。您的权利是,它的格式不正确!对不起,我刚刚修好了。第一个示例中的代码现在起作用了(Foreman导入它时没有问题)。看起来Foreman还不支持Puppet 4。我已经添加了一个应该与Puppet 3一起使用的示例。我的模块文件夹中有stdlib,我可以在Foreman的Puppet类中看到它,所以我猜它已经安装了?但是,当我用您的代码更新这个模块的init.pp文件并从Foreman运行import时,它没有看到它(表示“未检测到任何更改…”)。为了清楚,如果在本模块的init.pp中使用我的第一个示例中的代码,Foreman将毫无问题地上载它。@red888,您的第一个示例中的代码格式不正确,因此特定代码肯定没有您描述的结果。您以前使用的版本是否为所有48个端口声明了资源?如果您现在断言的状态与当前状态匹配,那么确实不应该有任何更改。您的权利是,它的格式不正确!对不起,我刚刚修好了。第一个示例中的代码现在起作用了(Foreman导入它时没有问题)。看起来Foreman还不支持Puppet 4。我添加了一个应该与Puppet 3一起使用的示例。