learning puppet 2--Variables and Facts

1)Variables

  • $variables always start with a dollar sign. You assign to variables with the = operator.
  • Variables can hold strings, numbers, booleans, arrays, hashes, and the special undef value. See the data types chapter of the Puppet reference manual for more information.
  • If you’ve never assigned a variable, you can actually still use it — its value will be undef.
  • You can use variables as the value for any resource attribute, or as the title of a resource.
  • You can also interpolate variables inside double-quoted strings. To distinguish a variable from the surrounding text, you can wrap its name in curly braces. ("This is the ${variable} name.") This isn’t mandatory, but it is recommended.

Every variable has two names:

  • A short local name
  • A long fully-qualified name

Fully qualified variables look like $scope::variable. Top scope variables are the same, but their scope is nameless. (For example: $::top_scope_variable.)

  • If you reference a variable with its short name and it isn't present in the local scope, Puppet will also check the global top scope; this means you can almost always refer to global variables with just their short names. You can see more about this in the scope chapter of the Puppet reference manual: scope in Puppet Enterprise 2.x and Puppet 2.7, scope in Puppet 3
  • You can only assign the same variable once in a given scope. In this way, they’re more like constants from other programming languages.

2)Facts

Puppet has a bunch of built-in, pre-assigned variables that you can use. Check it out:
# /root/examples/motd.pp
file {'motd':
ensure => file,
path => '/etc/motd',
mode => 0644,
content => "This Learning Puppet VM's IP address is ${ipaddress}",
}
output:

# cat /etc/motd
This Learning Puppet VM's IP address is 172.16.52.135.

$ipaddress is one of "facts". Puppet uses a tool called Facter, which discovers some system information, normalizes it into a set of variables, and passes them off to Puppet. Puppet’s compiler then has access to those facts when it's reading a manifest.

  • See here for a list of all of the "core" facts built into Facter. Most of them are always available to Puppet, although some of them are only present on certain system types.
  • You can see what Facter knows about a given system by running facter at the command line.
  • You can also see all of the facts for any node in your Puppet Enterprise deployment by browsing to that node’s page in the console and scrolling down to the inventory information.
  • You can also add new custom facts to Puppet; see the custom facts guide for more information.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值