puppet 配置 6 变量的特殊用法

发布于:2024-12-19 ⋅ 阅读:(12) ⋅ 点赞:(0)

目的

  • 变量常用于 template 中
  • 本文用于介绍一些变量在 template 中的用法

常见变量使用

变量定义

mon_osd_down_out_interval: "172800"

直接调用方法

mon_osd_down_out_interval    = <%= @mon_osd_down_out_interval %>

组数使用方法1

    initial_ceph_mon_info:
      - mon_name:  "ns-ceph-208217"
        mon_addr:  "10.5.8.123"
      - mon_name:  "ns-ceph-208218"
        mon_addr:  "10.5.8.131"
      - mon_name:  "ns-ceph-208219"
        mon_addr:  "10.5.8.147"

希望获得效果

mon_initial_members = ns-ceph-208217,ns-ceph-208218,ns-ceph-208219

调用方法

mon_initial_members = <%= @initial_ceph_mon_info.map { |server| server["mon_name"] } .join(",") %>

组数使用方法2

    initial_ceph_mon_info:
      - mon_name:  "ns-ceph-208217"
        mon_addr:  "10.5.8.123"
      - mon_name:  "ns-ceph-208218"
        mon_addr:  "10.5.8.131"
      - mon_name:  "ns-ceph-208219"
        mon_addr:  "10.5.8.147"

希望获得效果

[mon.ns-ceph-208217]
  host = ns-ceph-208217
  mon addr = 10.5.8.123

[mon.ns-ceph-208218]
  host = ns-ceph-208218
  mon addr = 10.5.8.131

[mon.ns-ceph-208219]
  host = ns-ceph-208219
  mon addr = 10.5.8.147

调用方法

<% if @initial_ceph_mon_info -%>
<% @initial_ceph_mon_info.each do |key| -%>
[mon.<%= key["mon_name"] %>]
  host = <%= key["mon_name"] %>
  mon addr = <%= key["mon_addr"] %>

<% end -%>

组数使用方法3

    ceph_osd_group:
      - journal_disk: "/dev/sdl"
        osd_disk:
          - "/dev/sdb"
          - "/dev/sdc"
          - "/dev/sdd"
          - "/dev/sde"
          - "/dev/sdf"
      - journal_disk: "/dev/sdm"
        osd_disk:
          - "/dev/sdg"
          - "/dev/sdh"
          - "/dev/sdi"
          - "/dev/sdj"
          - "/dev/sdk"

期望得到结果

jdisks = /dev/sdl  /dev/sdm 

puppet 语法

jdisks="<%= @ceph_osd_group.map { |server| server["journal_disk"] } .join(" ") %>"

组数使用方法4

    ceph_osd_group:
      - journal_disk: "/dev/sdl"
        osd_disk:
          - "/dev/sdb"
          - "/dev/sdc"
          - "/dev/sdd"
          - "/dev/sde"
          - "/dev/sdf"
      - journal_disk: "/dev/sdm"
        osd_disk:
          - "/dev/sdg"
          - "/dev/sdh"
          - "/dev/sdi"
          - "/dev/sdj"
          - "/dev/sdk"

期望得到结果

/dev/sdb  /dev/sdl1
/dev/sdc  /dev/sdl2
/dev/sdd  /dev/sdl3
/dev/sde  /dev/sdl4
/dev/sdf  /dev/sdl5
/dev/sdg  /dev/sdm1
/dev/sdh  /dev/sdm2
/dev/sdi  /dev/sdm3
/dev/sdj  /dev/sdm4
/dev/sdk  /dev/sdm5

语法

<% @ceph_osd_group.each do  |serverinfo| -%>
<% serverinfo["osd_disk"].each_index do |index|  -%>
 <%= serverinfo["osd_disk"][index]  %>  <%= serverinfo["journal_disk"] %><%= index +1 %>
<% end -%>
<% end -%>