运行 Junos OS 的设备的 Puppet 清单
使用 netdev 资源创建 Puppet 清单
Puppet 清单是用 Puppet 语言编写的文件,用于描述所需的系统配置。Puppet 主设备将清单编译为目录。代理节点会定期下载目录并进行所需的更改,以便生成的系统配置与所需的配置匹配。
Puppet 清单文件由 .pp 扩展标识。在清单中,您使用 Puppet 语言描述在每个代理节点上管理的资源。
netdev_stdlib模块定义了为各种网络资源建模属性的资源类型。该模块包括网络设备、物理接口、第 2 层交换服务、VLAN 和链路聚合组 (LAG) 的资源定义。有关可用资源类型的列表,请参阅 Puppet netdev 资源。
在管理运行 Junos OS 的设备时,您安装在 Puppet 主设备上的瞻博网络 netdev_stdlib_junos 模块包含特定于 Junos OS 的 Puppet 提供商代码,该代码可实施 netdev_stdlib 模块中定义的资源类型。该模块从 netdev_stdlib_junos 模块 2.0.2 版开始,还提供了 apply_group
定义的资源类型,使您能够管理 netdev_stdlib模块中 没有类型规格的网络资源。有关详细信息,请参阅 适用于 Junos OS apply_group 定义资源类型的 Puppet。
以下示例 Puppet 清单适用于带有主机名 jd.example.com 的交换机。该清单定义了三个 VLAN(粉红、绿色和红色),分别带有 VLAN ID 105、101 和 103。该清单定义 ge-0/0/20 中继接口接受粉红和绿色 VLAN 的标记数据包。默认情况下,ge-0/0/19 接口将配置为接受未标记数据包的接入端口。红色 VLAN 是 ge-0/0/19 和 ge-0/0/20 的本征 VLAN。
node "jd.example.com" { netdev_device { $hostname: } netdev_vlan { "Pink": vlan_id => 105, description => "This is a pink vlan", } netdev_vlan { "Green": vlan_id => 101, } netdev_vlan { "Red": vlan_id => 103, description => "This is the native vlan", } netdev_l2_interface { 'ge-0/0/19': untagged_vlan => Red, } netdev_l2_interface { 'ge-0/0/20': description => "connected to R1-central", untagged_vlan => Red, tagged_vlans => [ Green, Pink ], } }
示例:为运行 Junos OS 的设备创建 Puppet 清单
此示例说明如何创建示例 Puppet 清单以管理运行 Junos OS 的 Puppet 代理节点上的 VLAN 和第 2 层接口。该清单利用 Puppet 语言中的类定义和变量来创建更灵活和可扩展的清单文件。
要求
运行 Junos OS 版本 12.3R2 或更高版本 12.3 版本的 EX 系列交换机,安装了 jpuppet 软件包和 Puppet 的 Junos OS 用户帐户。
安装有瞻博网络 NETCONF Ruby 宝石和 瞻博网络/netdev_stdlib_junos Puppet 模块的 Puppet 主设备。
概述
在此示例中,您创建了一个 Puppet 清单,用于管理在“数据库”Pod 中运行 Junos OS 的交换机上的 VLAN 和第 2 层接口。netdev_stdlib模块定义netdev_device
netdev_vlan
了此示例中用于对运行 Junos OS 的设备上的连接属性、VLAN 和第 2 层 接口进行建模的、和netdev_l2_interface
资源类型。
Puppet 类定义 database_switch
包含作为“数据库”Pod 成员的交换机的设置。在类定义中,您必须定义为 netdev_device
目标交换机的连接属性建立模型的资源。争论 netdev_device
是 $hostname
变量,由 Facter 提供。在类定义中,您还可以为交换机创建 netdev_vlan
和 netdev_l2_interface
资源。
为了创建必要的资源,此示例使用 Puppet 功能 create_resources
,将散列转换为指定类型的一组资源。该功能有两个必需参数,即资源类型和一个描述资源标题和参数的散列表。可选的第三个论点包含应用于每个新资源的默认参数哈希表。如果在两个散列参数中指定相同参数,则强制论点中的参数值将替代可选参数中的默认值。
在此示例中,您会构建变量$vlans
$db_ports
,并且$db_port_settings
包含用于描述代理节点上的 VLAN 和第 2 层接口资源的散热。散列值必须是该资源类型的 netdev 模块中定义的属性。您可使用哈希作为 Puppet 功能的create_resources
参数来创建添加到目录中的资源。
该 $vlans
变量是定义在 100 到 104 范围内跨越 VLAN ID 的五个 VLAN 资源的散列。每个散列条目将资源名称(VLAN 名称)定义为散列密钥,并将资源属性(vlan_id和说明)定义为散列值。例如:
$vlans = { 'Blue' => { vlan_id => 100, description => "This is a Blue VLAN, just updated" }, ... }
变 $db_ports
量是定义将管理哪些交换机接口的散列, $db_port_settings
而变量则定义了这些端口的默认设置。默认设置将接口配置为中继接口,接受来自蓝色、绿色和黄色 VLAN 的标记数据包,并将红色 VLAN 作为本机 VLAN。
$db_ports = { "ge-0/0/0" => { description => "${db_port_desc} ge0" }, ... }
$db_port_settings = { untagged_vlan => Red, tagged_vlans => [Blue, Green, Yellow] }
构建定义资源的散热之后,使用该create_resources
功能创建资源。您可以使用以资源类型和$vlan
散列为参数的 create_resources
Puppet 功能netdev_vlan
来创建 VLAN 资源。您可使用以资源类型和$db_ports
散列为参数的 create_resources
Puppet 功能netdev_l2_interface
来创建第 2 层接口资源。此外,还包括$db_port_settings
哈希作为包含这些端口默认设置的可选第三个论点。
配置
逐步过程
要创建示例 Puppet 清单以管理运行 Junos OS 的 Puppet 代理节点上的 VLAN 和第 2 层接口:
-
创建名为 database_switch.pp 的文件。
-
定义 Puppet 代理将在运行 Junos OS 的代理节点上创建的 VLAN。
### Define a list of VLANS to create $vlans = { 'Blue' => { vlan_id => 100, description => "This is a Blue VLAN, just updated" }, 'Green' => { vlan_id => 101, description => "This is a Green VLAN" }, 'Purple' => { vlan_id => 102, description => "This is a Purple VLAN" }, 'Red' => { vlan_id => 103, description => "This is a Red VLAN" }, 'Yellow' => { vlan_id => 104, description => "This is a Yellow VLAN" } }
-
为
database_switch
该类创建代码块,该代码块将包含“数据库”Pod 中交换机的设置。### Define a class for all Switches in the 'database' pod ### Start class definition class database_switch { } ### End class definition
-
在
database_switch
类定义中,定义netdev_device
交换机的资源。netdev_device { $hostname: }
-
在
database_switch
类定义中,使用以资源类型和$vlans
散列为参数的create_resources
Puppet 功能netdev_vlan
来创建 VLAN 资源。# Create all the VLANs on the switch create_resources( netdev_vlan, $vlans )
-
在
database_switch
类定义中,定义 成员交换机上的第 2 层接口和端口设置。# Set up ports to use a selected list of VLANS $db_port_desc = "This is for database" $db_ports = { "ge-0/0/0" => { description => "${db_port_desc} ge0" }, "ge-0/0/1" => { description => "${db_port_desc} ge1" }, "ge-0/0/2" => { description => 'this is ge2' }, "ge-0/0/10" => { description => 'this is ge10' }, "ge-0/0/11" => { description => 'this is ge11' }, "ge-0/0/12" => { description => 'this is ge12' } } $db_port_settings = { untagged_vlan => Red, tagged_vlans => [Blue, Green, Yellow] }
-
在
database_switch
类定义中,使用以资源类型、$db_ports
散列和$db_port_settings
散列为参数的create_resources
Puppet 功能netdev_l2_interface
来创建第 2 层接口资源。create_resources( netdev_l2_interface, $db_ports, $db_port_settings )
-
使用该节点的类定义。
### Use the class definition for this node node "jd.example.com" { include database_switch }
结果
在 Puppet 主设备上,查看完成的 database_switch.pp 清单文件。如果文件未显示预期的代码,请重复此示例中的说明,以更正清单。
### Define a list of VLANS to create $vlans = { 'Blue' => { vlan_id => 100, description => "This is a Blue VLAN, just updated" }, 'Green' => { vlan_id => 101, description => "This is a Green VLAN" }, 'Purple' => { vlan_id => 102, description => "This is a Purple VLAN" }, 'Red' => { vlan_id => 103, description => "This is a Red VLAN" }, 'Yellow' => { vlan_id => 104, description => "This is a Yellow VLAN" } } ### Define a class for all Switches in the 'database' POD ### Start class definition class database_switch { netdev_device { $hostname: } # Create all the VLANs on the switch create_resources( netdev_vlan, $vlans ) # Set up ports to use a selected list of VLANS $db_port_desc = "This is for database" $db_ports = { "ge-0/0/0" => { description => "${db_port_desc} ge0" }, "ge-0/0/1" => { description => "${db_port_desc} ge1" }, "ge-0/0/2" => { description => 'this is ge2' }, "ge-0/0/10" => { description => 'this is ge10' }, "ge-0/0/11" => { description => 'this is ge11' }, "ge-0/0/12" => { description => 'this is ge12' } } $db_port_settings = { untagged_vlan => Red, tagged_vlans => [Blue, Green, Yellow] } create_resources( netdev_l2_interface, $db_ports, $db_port_settings ) } ### End class definition ### Use the class definition for this node node "jd.example.com" { include database_switch }
在 Puppet 代理应用配置更改之后,生成的配置更新包括:
[edit] interfaces { ge-0/0/0 { unit 0 { description "This is for database ge0"; family ethernet-switching { port-mode trunk; vlan { members [ Blue Green Yellow ]; } native-vlan-id Red; } } } ge-0/0/1 { unit 0 { description "This is for database ge1"; family ethernet-switching { port-mode trunk; vlan { members [ Blue Green Yellow ]; } native-vlan-id Red; } } } ge-0/0/2 { unit 0 { description "this is ge2"; family ethernet-switching { port-mode trunk; vlan { members [ Blue Green Yellow ]; } native-vlan-id Red; } } } ge-0/0/10 { unit 0 { description "this is ge10"; family ethernet-switching { port-mode trunk; vlan { members [ Blue Green Yellow ]; } native-vlan-id Red; } } } ge-0/0/11 { unit 0 { description "this is ge11"; family ethernet-switching { port-mode trunk; vlan { members [ Blue Green Yellow ]; } native-vlan-id Red; } } } ge-0/0/12 { unit 0 { description "this is ge12"; family ethernet-switching { port-mode trunk; vlan { members [ Blue Green Yellow ]; } native-vlan-id Red; } } } } vlans { Blue { description "This is a Blue vlan, just updated"; vlan-id 100; } Green { description "This is a Green vlan"; vlan-id 101; } Purple { description "This is a Purple vlan"; vlan-id 102; } Red { description "This is a Red vlan"; vlan-id 103; } Yellow { description "This is a Yellow vlan"; vlan-id 104; } }
验证
故障 排除
配置问题故障排除
问题
代理节点上的配置不会反映清单中请求的更改。
如果未看到配置的任何更新,则交换机可能不包含在托管代理节点中,或者 Puppet 代理可能没有下载最新目录并执行配置更新。如果您看到配置更新,但未正确,则 Puppet 清单可能包含不正确的信息。
解决 方案
确保 Puppet 主设备配置正确,以便为该节点创建目录。如果 Puppet 主设备配置正确,请查看 Puppet 清单文件,确保它包含正确的配置更改,并在必要时更正清单。
如果已启用报告,还可查看 Puppet 主设备上的日志文件,以验证代理节点是否下载了最新目录并提交配置更改。如果 Puppet 代理无法锁定配置数据库,无法因语法错误而上传配置更改,或者无法在设备上提交配置,配置将保持不变。