puppet augeas 使用示例

puppet augeas 资源使用

使用augeas前提需要安装augeas 包。

  • augtool可执行命令路径:

puppet 3 版本      /usr/bin/augtool

puppet 4 版本  /opt/puppetlabs/puppet/bin/augtool

  • augtool 使用示例
    [root@puppetmaster ~]# augtool
    augtool> ls /augeas/load  #显示所有可用的资源
    Libvirtd_qemu/ = (none)
    Libvirtd_lxc/ = (none)
    Libvirtd/ = (none)
    VMware_Config/ = (none)
    Login_defs/ = (none)
    Networks/ = (none)
    Splunk/ = (none)
    Thttpd/ = (none)
    Odbc/ = (none)
    Nsswitch/ = (none)
    Netmasks/ = (none)
    ...

改变特定文件里参数值

augtool> set /files/etc/ssh/sshd_config/PermitrootLogin no
augtool> save

以树形结构来展示数据

cat /etc/hosts
/etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.181 puppet puppetmaster
192.168.200.181 files.puppetfans.cn
192.168.200.182 Xkefu
192.168.200.187 xkefu187

augtool> ls /files/etc/hosts
1/ = (none)
2/ = (none)
3/ = (none)
4/ = (none)
5/ = (none)
6/ = (none)

augtool> ls /files/etc/hosts/6
ipaddr = 192.168.200.187
canonical = xkefu187

查看augeas版本

augtool> ls /augeas/
root = /
context = /files
variables = (none)
version/ = 1.0.0
save = overwrite
span = disable
load/ = (none)
files/ = (none)

augeas 更多使用方法

Admin commands:
help - print help
load - (re)load files under /files
quit - exit the program
retrieve - transform tree into text
save - save all pending changes
store - parse text into tree
transform - add a file transform

Read commands:
dump-xml - print a subtree as XML
get - get the value of a node
label - get the label of a node
ls - list children of a node
match - print matches for a path expression
print - print a subtree
span - print position in input file corresponding to tree

Write commands:
clear - clear the value of a node
clearm - clear the value of multiple nodes
ins - insert new node
insert - insert new node (alias of 'ins')
mv - move a subtree
move - move a subtree (alias of 'mv')
rename - rename a subtree label
rm - delete nodes and subtrees
set - set the value of a node
setm - set the value of multiple nodes
touch - create a new node

Path expression commands:
defnode - set a variable, possibly creating a new node
defvar - set a variable

Type 'help <command>' for more information on a command

继续阅读“puppet augeas 使用示例”

puppet 节点继承与类继承用法

puppet 节点继承用法:

node $name [inherits $parent_node]
{ [ Puppet code, resources and classes applied to the node ] }

puppet  node  inherits 用法示例:

node  'www.puppetfans.com' inherits basenode {}

puppet 类继承用法, puppet class inherits 用法示例:

class puppet {
file { '/etc/puppet/puppet.conf':
content => template('puppet/client/puppet.conf'),
}
}
class puppet::server inherits puppet {
File['/etc/puppet/puppet.conf'] {
content => template('puppet/server/puppet.conf'),
}
}

puppet 语法学习变量

变量类型

字符串

 $a_string = 'This is a string value'

数组

 $an_array = [ 'This', 'forms', 'an', 'array' ] 

哈希

 $a_hash = { 'subject' => 'Hashes',
'predicate' => 'are written',
'object' => 'like this',
'note' => 'not actual grammar!',
'also note' => [ 'nesting is', { 'allowed' => 'of course' } ],
}

使用变量示例:

$x = $a_string
$y = $an_array[1]
$z = $a_hash['object']

$download_server = 'img2.puppetfans.com'
$url = "https://${download_server}/pkg/puppet-ebook.tar.gz"

$packages = [ 'apache2', 'libapache2-mod-php5', 'libapache2-mod-passenger', ]
package { $packages:
ensure => 'installed'
}

puppet 语法学习

  1. 条件语句
    • case 语句
case $operatingsystem {
redhat: { service { "httpd":   ensure => running }}
debian:  { service { "apache":  ensure => running }}
default: { service { "apache2": ensure => running }}
} 
  •  if 条件语句
 if $is_virtual {
warning('target is virtual.')
}
elsif $operatingsystem == 'CentOS' {
warning('module doesnt work on this OS.')
}
else {
include roles::base
}
  • 选择语句
$rootgroup = $osfamily ? {
 'Solaris' => 'wheel',
 /(Darwin|FreeBSD)/ => 'wheel',
 default => 'root',
}
  • 除非语句
 unless $memorysize > 2048 {
$maxclient = 1000
}
  • in语句
if $monitortool in [ 'zabbix','nagios','cacti' ]

 

puppet resouces 使用示例

  • user 使用示例:
user {"nginx":
ensure => present,
shell => "/sbin/nologin",
mangehome => true,
comment => "nginx user",
}
  • group 使用示例:
group { "nginx":
        ensure => present,
        gid => 1000,
       }
  • package 使用示例:
 packages { "nginx":
           ensure =>present,
        } 
  • service 使用示例:
service { "nginx": 
        ensure => running,
        enable => true, 
     hasstatus => true,
    hasrestart => true,
       pattern => nginx,
       require => Package['nginx'],
    }
  • cron 使用示例:
 cron { "clean-files":
ensure => present,
user => 'root',
command => '/usr/local/bin/clean-files',
minute => '1',
hour => '3',
weekday => [ '2', '6' ],
environment => 'MAILTO=puppetfans@163.com',
} 
  • exec 使用示例:

exec { 'perl -MCPAN -e "install YAML"':
path => '/bin:/usr/bin',
unless => 'cpan -l | grep -qP ^YAML\\b',
}
  • file 使用示例:
 file { "nginx.conf":
 ensure  => present,
 path    => "/etc/nginx/nginx.conf",
 mode    => 644,
 owner   => root,
 group   => root,
 require => Package['nginx'],
 notify  => Service['nginx'],
 source  => "puppet:///modules/hotpu/nginx/nginx.conf",
 }
  • mount 使用示例:
mount { '/media/gluster-data':
ensure => 'mounted',
device => 'gluster01:/data',
fstype => 'glusterfs',
options => 'defaults,_netdev',
dump => 0,
pass => 0,
}
  • 虚拟资源使用示例:
@@user { "nginx":
 ensure => present,
 tag => nginx,
 }
  • 实现虚拟资源使用示例:
 realize User["nginx"]
User <| tag == nginx |>