Microsoft's Internet Explorer browser has no built-in vector graphics machinery required for "loss-free" gradient background themes.

Please upgrade to a better browser such as Firefox, Opera, Chrome, Safari or others with built-in vector graphics machinery and much more. (Learn more or post questions or comments at the Slide Show (S9) project site. Thanks!)

Puppet によるインフラ管理入門

by Luke Kanies

Reductive Labs, Inc.

Japanese Translated by Naoya Nakazawa

Original Presentation available at git://github.com/reductivelabs/velocity_puppet_workshop_2009.git

目標

Puppet 入門

ヘルプの入手

リソース

   1  ralsh <パッケージ名>
   2  ralsh user luke
   3  ralsh user naoya
   4  ralsh user naoya ensure=present shell=/opt/local/bin/zsh home=/Users/naoya
   5  ralsh user naoya shell=/opt/local/bin/zsh
   6  ralsh user naoya ensure=absent
examples/ralsh.sh

ralsh package を試してみよう

言語

   1  file { "/tmp/example_file":
   2      ensure => present,
   3      content => "This is a test\n",
   4      mode => 640,
   5  }
examples/language.pp

実行プログラム

他の実行プログラムもあるが、今回はすべてを対象にしていない

最初の Puppet スクリプト

   1  puppet -e 'file { "/tmp/eh": ensure => present }'

実演:

   1  notice: //File[/tmp/eh]/ensure: created 

2回試してみよう。--noop オプションをつけて試してみよう。

その次の Puppet スクリプト

   1  puppet -e 'file { "/tmp/eh": ensure => absent }'

実演:

   1  notice: //File[/tmp/eh]: Filebucketed to  with sum d41d8cd98f00b204e9800998ecf8427e
   2  notice: //File[/tmp/eh]/ensure: removed 

Puppet クラス

   1  class foo {
   2      file { "/tmp/foo": ensure => present }
   3  }
examples/class_but_no_include.pp

実演:

   1   

クラスを使う

   1  class foo {
   2      file { "/tmp/foo": ensure => present }
   3  }
   4  include foo
examples/class_with_include.pp

実演:

   1  notice: //foo/File[/tmp/foo]/ensure: created 

Puppet リポジトリ

基本のリポジトリ構造:

   1  manifests/site.pp
   2  modules/
   3  modules/mymod/manifests/init.pp
   4  modules/mymod/templates/mytemplate.erb
   5  modules/mymod/plugins/puppet/parser/functions/myfunction.rb

最初のモジュール

   1  mkdir repo
   2  mkdir -p repo/manifests repo/modules/foo/manifests
   3  cp examples/class_but_no_include.pp repo/modules/foo/manifests/init.pp

使ってみよう:

   1  puppet --modulepath repo/modules -e 'include foo'

最初のノード

   1  node default {
   2      include foo
   3  }
repo/manifests/site.pp

一緒に組み合わせてみよう

   1  puppet --modulepath repo/modules repo/manifests/site.pp

これで「完全な」 Puppet リポジトリになり、拡張していく準備が整いました。

サーバを開始する

   1  puppetmasterd --verbose --no-daemonize --modulepath $PWD/repo/modules \
   2  --confdir /tmp/server --vardir /tmp/server \
   3  --manifest $PWD/repo/manifests/site.pp --certdnsnames localhost

実演:

   1  info: Starting server for Puppet version 0.24.8
   2  info: Creating a new certificate request for phage.local
   3  info: Creating a new SSL key at /tmp/server/ssl/private_keys/phage.local.pem
   4  info: Autosign is enabled but /tmp/server/autosign.conf is missing
   5  info: Signing certificate for CA server
   6  info: Signing certificate for phage.local
   7  info: Listening on port 8140
   8  notice: Starting Puppet server version 0.24.8

通常は 'puppet' ユーザで実行しますが、'puppet' ユーザは必須ではありません。

サーバの引数

デフォルトのサーバ引数

   1  puppetmasterd --configprint modulepath
   2  puppetmasterd --configprint confdir
   3  puppetmasterd --configprint vardir
examples/default_server_arguments.sh

実演:

   1  /opt/rl/dist/apps/puppet/modules:/usr/share/puppet/modules
   2  /etc/puppet
   3  /var/puppet 

エージェントを動かす

   1  sudo puppetd --test --confdir /tmp/server --vardir /tmp/server --server localhost

実演:

   1  info: Caching catalog at /tmp/server/state/localconfig.yaml
   2  notice: Starting catalog run
   3  notice: //Node[default]/foo/File[/tmp/foo]/ensure: created
   4  notice: Finished catalog run in 0.01 seconds

root ユーザが実行することに注意しよう

エージェントの引数

証明書

標準の SSL(tm) になっている。

  1. CSR(Certificate Singing Request) を生成する
  2. サーバに CSR を送信する
  3. サーバは CSR に署名して、証明書を提供する
  4. クライアントは、証明書を受信する

証明書認証へようこそ

   1  puppetca --confdir /tmp/server/ --vardir /tmp/server --list

実演:

   1  No certificates to sign

クライアント証明書に署名する

/tmp/client ディレクトリに移動する:

   1  sudo puppetd --test --confdir /tmp/client --vardir /tmp/client \
   2  --server localhost --certname other.madstop.com

サーバ上の表示:

   1  notice: Host other.madstop.com has a waiting certificate request

   1  puppetca --confdir /tmp/server/ --vardir /tmp/server --list
   2  puppetca --confdir /tmp/server/ --vardir /tmp/server --sign other.madstop.com

もう一度クライアント上で実行してみよう

役に立つ Tips

私は、いつも、sudo から始める。

repo/modules/sudo/manifests/init.pp の中身:

   1  
   2  class sudo {
   3      file { "/usr/bin/sudo":
   4          owner => root,
   5          group => wheel,
   6          mode => 4111
   7      }
   8  }

site.pp のデフォルトノードに sudo を追加する。

ファイルを管理する

repo/modules/sudo/files/sudoers に自分の sudoers を作成したら、init.pp に追加してみよう:

   1  file { "/etc/sudoers":
   2      owner => root,
   3      group => wheel,
   4      mode => 440,
   5      source => "puppet:///sudo/sudoers"
   6  }

プラットフォームが異なるものを管理しよう

   1  file { sudo:
   2      path => $operatingsystem ? {
   3          darwin => "/usr/bin/sudo",
   4          default => "/usr/sbin/sudo",
   5      },
   6      owner => root,
   7      group => 0,
   8      mode => 4111
   9  }

Facter

   1  $ facter | wc -l
   2        55
   3  $ facter | grep name
   4  hostname => phage
   5  macosx_productname => Mac OS X
   6  sp_local_host_name => phage
   7  sp_machine_name => MacBook
   8  sp_user_name => Luke Kanies (luke)
   9  $ 

その他の言語関数

wiki にあるチュートリアルに解説されています。

Language Tutorial

Type Reference

Function Reference

関連

   1  class rels {
   2      file { "/tmp/reltest": ensure => present }
   3      exec { "/bin/echo got notified":
   4          subscribe => File["/tmp/reltest"],
   5          refreshonly => true
   6      }
   7  }
repo/modules/rels/manifests/init.pp

実演:

   1  notice: //rels/File[/tmp/reltest]/ensure: created
   2  notice: //rels/Exec[/bin/echo got notified]: Triggering 'refresh' from 1 dependencies 

定義

Apache の仮想ホスト

   1  define apache::vhost($docroot, $ensure = present) {
   2      file { "/tmp/apache/sites/$name":
   3          content => template("apache/vhost.erb"),
   4          ensure => $ensure
   5      }
   6  }
repo/modules/apache/manifests/vhost.pp

条件付き定義名に注意しよう。この名前は正しく自動ロードするときに必要なものです。

関数を使ってみよう

   1  class apache {
   2      apache::vhost { 'reductivelabs.com':
   3          docroot => "/var/www/reductivelabs.com"
   4      }
   5      apache::vhost { 'foo.com':
   6          docroot => "/var/www/foo.com",
   7          ensure => absent
   8      }
   9  }
repo/modules/apache/manifests/init.pp

複雑な仮想ホスト

   1  define apache::vhost2($docroot, $ensure = present) {
   2      $available => $ensure ? { enabled => present, default => $ensure }
   3      $enabled => $ensure ? { enabled => symlink, default => absent }
   4      file { "/tmp/apache/sites-available/$name":
   5          content => template("apache/vhost.erb"),
   6          ensure => $available
   7      }
   8      file { "/tmp/apache/sites-enabled/$name":
   9          target => "/tmp/apache/sites-available/$name",
  10          ensure => $enabled
  11      }
  12  }
repo/modules/apache/manifests/vhost2.pp

スキップした項目

お決まりの宣伝

Reductive Labs はサポート契約を提供、トレーニング、カスタム開発を提供しています。そして、これらを Puppet の開発、講演、食べることための資金としています。もちろんほとんどすべてです。

Special Thanks to Andrew Clay Shafer and Luke Kanies!