【3】puppet笔记 - 变量、if、

发布时间:2019-09-02 07:40:41编辑:auto阅读(1729)

    变量:http://docs.puppetlabs.com/facter/latest/core_facts.html

    http://docs.puppetlabs.com/puppet/latest/reference/lang_variables.html

    语句:http://docs.puppetlabs.com/puppet/latest/reference/lang_conditional.html

         http://docs.puppetlabs.com/puppet/latest/reference/lang_expressions.html

    1、变量


    定义变量:以$开头,如:$system、$flag

    赋值:=  ,如:$one = "first one"

    引用变量:有三种方法如下

    $var = "Hello World!"
    notice "1.$var"
    notice "2.${var}"
    notice "3.$::var"

    输出:

    [root@pclient test]# puppet apply 3.pp
    Notice: Scope(Class[main]): 1.Hello World!
    Notice: Scope(Class[main]): 2.Hello World!
    Notice: Scope(Class[main]): 3.Hello World!
    Notice: Compiled catalog for pclient.onepc.com in environment production in 0.06 seconds
    Notice: Finished catalog run in 0.05 seconds


    puppet代码中可以直接使用的变量:


    http://docs.puppetlabs.com/facter/latest/core_facts.html


    Summary
    architecture
    augeasversion
    blockdevice_{devicename}_size
    blockdevice_{devicename}_vendor
    blockdevice_{devicename}_model
    blockdevices
    boardmanufacturer
    boardproductname
    boardserialnumber
    cfkey
    domain
    ec2_{EC2 INSTANCE DATA}
    ec2_userdata
    facterversion
    filesystems
    ldom
    fqdn
    gid
    hardwareisa
    hardwaremodel
    hostname
    id
    interfaces
    ipaddress
    ipaddress_{NETWORK INTERFACE}
    ipaddress6
    ipaddress6_{NETWORK INTERFACE}
    iphostnumber
    is_virtual
    kernel
    kernelmajversion
    kernelrelease
    kernelversion
    lsbdistcodename
    lsbdistdescription
    lsbdistid
    lsbdistrelease
    lsbmajdistrelease
    lsbminordistrelease
    lsbrelease
    macaddress
    macaddress_{NETWORK INTERFACE}
    macosx_buildversion
    macosx_productname
    macosx_productversion
    macosx_productversion_major
    macosx_productversion_minor
    manufacturer
    memoryfree
    memorysize
    netmask
    netmask_{NETWORK INTERFACE}
    network_{NETWORK INTERFACE}
    operatingsystem
    operatingsystemmajrelease
    operatingsystemrelease
    osfamily
    path
    physicalprocessorcount
    processor
    processor{NUMBER}
    processorcount
    productname
    ps
    puppetversion
    rubysitedir
    rubyversion
    selinux
    selinux_config_mode
    selinux_config_policy
    selinux_current_mode
    selinux_enforced
    selinux_policyversion
    serialnumber
    sp_{SYSTEM PROFILER DATA}
    sshdsakey
    sshecdsakey
    sshrsakey
    swapencrypted
    swapfree
    swapsize
    timezone
    type
    uniqueid
    uptime
    uptime_days
    uptime_hours
    uptime_seconds
    uuid
    virtual
    vlans
    xendomains
    zfs_version
    zonename
    zones
    zpool_version



    2、if语句


    if 条件 {

     语句

    }


    条件:是true执行语句,是false则跳过语句


    $varif = 20
    if $varif > 10 {
      notice "$varif > 10"
    } else {
      notice "$varif < 10"
    }


    注意:elsif 不是 elseif
    if $varif == 30 {
      notice "varif=30" } elsif $varif > 30 {notice "varif>30" }
    else { notice "varif < 30" }




    3、unless语句


    unless 条件 {

    语句

    }

    条件:当false时,执行语句。当true时跳过。

    $varless = 100
    unless $varless > 1000 {
      notice "$varless > 1000 is false"
    }


    Notice: Scope(Class[main]): 100 > 1000 is false


    4、case语句


    case 变量/表达式 {

    值1 :{语句1}

    值2 : {语句2}

    default : {不是上面的值时,执行这条语句}

    }


    $varcase = 100
    case $varcase {
       10: {notice "varcase = 10"}
       20: {notice "varcase = 20"}
       100: {notice "varcase = 100"}
       default: {notice "varcase is match"}
    }


    Notice: Scope(Class[main]): varcase = 100


    5、selectors语句


    未知变量 = 可知变量 ? {

    值1 => 赋值1,

    值2 => 赋值2,

    default => 赋值3,

    }

    可以用于系统版本之类的判断


    $varselectors = "puppet"
    $varsel = $varselectors ? {
     "centos" => "this is centos",
     "puppet" => "this is puppet",
     default => "this is default",
    }
    notice "varsel is value = \"$varsel\""


    Notice: Scope(Class[main]): varsel is value = "this is puppet"


    官网例子:

    $rootgroup = $osfamily ? {
        'Solaris'          => 'wheel',
        /(Darwin|FreeBSD)/ => 'wheel',
        default            => 'root',
    }
    file { '/etc/passwd':
      ensure => file,
      owner  => 'root',
      group  => $rootgroup,
    }





关键字