@ü.li:Listing 1: resource-Kommando zeigt Ist-Zustand @li:puppet resource user root user { 'root': ensure => 'present', comment => 'root', gid => '0', home => '/root', password => '$6$qdl.zUqw$UmKgdhCIte2s3HwTEGKW6v0ZLEygRzqw0.9Ey6sZEJ40OVXwLHrktN3g6RyaOISGdT9H5IkqL3Nk7bJtWO6VJ1', password_max_age => '99999', password_min_age => '0', shell => '/bin/bash', uid => '0', } puppet resource user notthere user { 'notthere': ensure => 'absent', } @ü.li:Listing 2: Ressourcentypen package und service @li:puppet resource package puppetserver package { 'puppetserver': ensure => '2.1.1-1puppetlabs1', } puppet resource service puppetserver service { 'puppetserver': ensure => 'stopped', enable => 'false', } @ü.li:Listing 3: Klassendefinition für SSH @li:class my_ssh { package { 'openssh': ensure => present, } file { '/etc/ssh/sshd_config': ensure => file, owner => 'root', group => 'root', mode => '0644', } service { 'ssh': ensure => running, enable => true, } } @ü.li:Listing 4: Zwei Beispiele für Klassendefinition mit Abhängigkeit @li:class my_ssh { package { 'openssh-server': ensure => present, } file { '/etc/ssh/known_hosts': ensure => file, require => Package['openssh-server'], } service { 'sshd': ensure => running, subscribe => File['/etc/ssh/known_hosts'], } } class my_ssh { package { 'openssh-server': ensure => present, before => File['/etc/ssh/known_hosts'], } file { '/etc/ssh/known_hosts': ensure => file, notify => Service['sshd'], } service { 'sshd': ensure => running, } } @ü.li:Listing 5: Verzeichnisse und Dateinamen für Module @li:my_ssh/ |- examples | `- init.pp |- files | |- ssh_config | `- sshd_config `- manifests `- init.pp my_ssh/ `- manifests `- init.pp # class my_ssh { ... } my_ntp/ `- manifests `- init.pp # class my_ntp { ... } my_nginx/ `- manifests `- init.pp # class my_nginx { ... } @ü.li:Listing 6: Namenskonventionen bei Subklassen @li:my_ssh/ `-- manifests |-- init.pp # class my_ssh { ... } |-- install.pp # class my_ssh::install { ... } |-- config.pp # class my_ssh::config { ... } `-- service.pp # class my_ssh::service { ... } apache/ `-- manifests |-- init.pp # class apache { ... } |-- mod | |-- auth.pp # class apache::mod::auth { ... } | |-- proxy.pp # class apache::mod::proxy { ... } | `-- ssl.pp # class apache::mod::ssl { ... } `-- mod.pp # class apache::mod { ... } @ü.li:Listing 7: Standard-Environment production @li:tree -d -L 3 /etc/puppetlabs/code/ /etc/puppetlabs/code/ |- environments | |- development | | |- hieradata | | |- manifests | | `- modules | `- production | |- hieradata | |- manifests | `- modules `- modules @ü.li:Listing 8: Verwenden von Variablen und Text @li:class my_apache { $conf_dir = '/etc/apache2' notify { "Die Apache Konfiguration liegt in ${conf_dir}": } notify { 'Dieser Text wird \n genauso ausgedruckt ${conf_dir}': } } @ü.li:Listing 9: Variablen in einer Klasse mit lokalem Namensraum @li:class my_ssh { $conf_dir = '/etc/ssh' file { $conf_dir: ensure => directory, } } class my_apache { $conf_dir = '/etc/apache2' file { $conf_dir: ensure => directory, } } @ü.li:Listing 10: Mehrmaliges Deklarieren einer Variablen in einem Namensraum @li:class my_error { $local_variable = 'foo' package { $local_variable: ensure => present, } $local_variable = 'bar' # Fehler!! service { $local_variable: ensure => running, } } @ü.li:Listing 11: Klasenübergreifendes Zugreifen auf Variablen @li:class my_server { contain my_ssh contain my_apache file { "${my_ssh::conf_dir}/known_hosts": ensure => file } file { "${my_apache::conf_dir}/my.cnf": ensure => file, } } @ü.li:Listing 12: Informationen sammeln mit Facter @li:# Beispiel Facter 2: class my_os { notify { "Betriebssystem: ${::osfamily}": } } # Beispiel Facter 3 (Hash) class my_os { notify { "Betriebssystem: ${::facts['os']['family']}": } } @ü.li:Listing 13: Beispiel einer case-Anweisung @li:case $::operatingsystem { 'Debian': { $pkg_name = 'apache2' $cfg_dir = '/etc/apache2' } 'RedHat': { $pkg_name = 'httpd' $cfg_dir = '/etc/httpd' } default: { notify { "Betriebssystem (${::operatingsystem}) wird nicht unterstützt": } } } @ü.li:Listing 14: Apache-Klasse für mehrere Betriebssysteme erweitern @li:class my_apache { case $::osfamily { 'Debian': { $pkgname = 'apache2' $cfgdir = '/etc/apache2' $cfgfile = 'apache2.conf' $svcname = 'apache2' } 'RedHat': { $pkgname = 'httpd' $cfgdir = '/etc/httpd/conf' $cfgfile = 'httpd.conf' $svcname = 'httpd' } 'SLES': { $pkgname = 'httpd' $cfgdir = '/etc/httpd/conf' $cfgfile = 'httpd.conf' $svcname = 'httpd' } default: { fail("Das Betriebssystem ${::operatingsystem} wird nicht unterstützt") } } @ü.li:Listing 15: Variablen im Apache-Package benutzen @li: package { 'apache': ensure => present, name => $pkgname, } file { 'apache_cfg': ensure => file, path => "${cfgdir}/${cfgfile}", owner => 'root', group => 'root', mode => '0644', require => Package['apache'], } service { 'apache': ensure => running, enable => true, name => $svcname, subscribe => File['apache_cfg'], } }