Puppet-live-project

提供:Dev Guides
移動先:案内検索

Puppet-ライブプロジェクト

Puppetノードに構成とマニフェストを適用するライブテストを実行するために、ライブで動作するデモを使用します。 これを直接コピーして貼り付けて、構成の動作をテストできます。 ユーザーが同じコードセットを使用する場合は、次のコードスニペットに示されているのと同じ命名規則を使用する必要があります。

新しいモジュールの作成から始めましょう。

新しいモジュールの作成

httpd構成のテストと適用の最初のステップは、モジュールを作成することです。 これを行うには、ユーザーは自分の作業ディレクトリをPuppetモジュールディレクトリに変更し、基本的なモジュール構造を作成する必要があります。 構造の作成は、手動で、またはPuppetを使用してモジュールのボイラープレートを作成することで実行できます。

# cd/etc/puppet/modules
# puppet module generate Live-module

-Puppet module generateコマンドでは、Puppet forgeの仕様に準拠するために、モジュール名が[ユーザー名]-[モジュール]の形式を取る必要があります。

新しいモジュールには、マニフェストディレクトリを含むいくつかの基本的なファイルが含まれています。 ディレクトリには、モジュールのメインマニフェストファイルであるinit.ppという名前のマニフェストが既に含まれています。 これは、モジュールの空のクラス宣言です。

class live-module {
}

モジュールには、 init.pp というマニフェストを含むテストディレクトリも含まれています。 このテストマニフェストには、manifest/init.pp内のlive-moduleクラスへの参照が含まれています。

include live-module

Puppetはこのテストモジュールを使用してマニフェストをテストします。 これで、構成をモジュールに追加する準備ができました。

HTTPサーバーのインストール

Puppetモジュールは、httpサーバーを実行するために必要なパッケージをインストールします。 これには、httpdパッケージの構成を定義するリソース定義が必要です。

モジュールのマニフェストディレクトリで、httpd.ppという新しいマニフェストファイルを作成します

# touch test-module/manifests/httpd.pp

このマニフェストには、モジュールのすべてのHTTP構成が含まれます。 分離のために、httpd.ppファイルをinit.ppマニフェストファイルとは別にします

httpd.ppマニフェストファイルに次のコードを配置する必要があります。

class test-module::httpd {
   package { 'httpd':
      ensure => installed,
   }
}

このコードは、httpdというtest-moduleのサブクラスを定義してから、httpdパッケージのパッケージリソース宣言を定義します。 ensure ⇒ installed属性は、必要なパッケージがインストールされているかどうかを確認します。 インストールされていない場合、Puppetはyumユーティリティを使用してインストールします。 次に、このサブクラスをメインマニフェストファイルに含めます。 init.ppマニフェストを編集する必要があります。

class test-module {
   include test-module::httpd
}

次は、モジュールをテストする時間です。

# puppet apply test-module/tests/init.pp --noop

puppet applyコマンドは、ターゲットシステムのマニフェストファイルに存在する構成を適用します。 ここでは、メインのinit.ppを参照するテストinit.ppを使用しています。 -noopは、構成の予行演習を実行します。これは、出力のみを表示しますが、実際には何もしません。

出力は以下のとおりです。

Notice: Compiled catalog for puppet.example.com in environment
production in 0.59 seconds

Notice:/Stage[main]/test-module::Httpd/Package[httpd]/ensure:
current_value absent, should be present (noop)

Notice: Class[test-module::Httpd]: Would have triggered 'refresh' from 1
events

Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.67 seconds

ハイライト行は、== installed属性の結果です。 current_value不在は、Puppetがhttpdパッケージがインストールされていることを検出したことを意味します。 –noopオプションを指定しないと、Puppetはhttpdパッケージをインストールします。

httpdサーバーの実行

httpdサーバーをインストールした後、他のリソース減速を使用してサービスを開始する必要があります:サービス

httpd.ppマニフェストファイルを編集し、次のコンテンツを編集する必要があります。

class test-module::httpd {
   package { 'httpd':
      ensure => installed,
   }
   service { 'httpd':
      ensure => running,
      enable => true,
      require => Package["httpd"],
   }
}

以下は、上記のコードから達成した目標のリストです。

  • ensure ⇒ running statusは、サービスが実行されているかどうかを確認し、実行されていない場合は有効にします。
  • enable ⇒ true属性は、システムの起動時に実行するサービスを設定します。
  • require ⇒ Package ["httpd"] 属性は、あるリソースの減速と他のリソースの減速の順序関係を定義します。 上記の場合、httpパッケージのインストール後にhttpdサービスが確実に開始されます。 これにより、サービスと各パッケージの間に依存関係が作成されます。

puppet applyコマンドを実行して、変更を再度テストします。

# puppet apply test-module/tests/init.pp --noop
Notice: Compiled catalog for puppet.example.com in environment
production in 0.56 seconds

Notice:/Stage[main]/test-module::Httpd/Package[httpd]/ensure:
current_value absent, should be present (noop)

Notice:/Stage[main]/test-module::Httpd/Service[httpd]/ensure:
current_value stopped, should be running (noop)

Notice: Class[test-module::Httpd]: Would have triggered 'refresh' from 2
events

Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.41 seconds

httpdサーバーの構成

上記の手順が完了したら、HTTPサーバーをインストールして有効にします。 次のステップは、サーバーに何らかの構成を提供することです。 デフォルトでは、httpdはWebホストポート80を提供する/etc/httpd/conf/httpd.confでいくつかのデフォルト設定を提供します。 いくつかの追加のホストを追加して、ユーザー固有の機能をWebホストに提供します。

可変入力が必要なため、テンプレートを使用して追加のポートを提供します。 templateというディレクトリを作成し、test-server.config.erbというファイルを新しいディレクタに追加して、次のコンテンツを追加します。

Listen <%= @httpd_port %>
NameVirtualHost *:<% = @httpd_port %>

<VirtualHost *:<% = @httpd_port %>>
   DocumentRoot/var/www/testserver/
   ServerName <% = @fqdn %>

   <Directory "/var/www/testserver/">
      Options All Indexes FollowSymLinks
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>

上記のテンプレートは、標準のapache-tomcatサーバー構成フォーマットに従います。 唯一の違いは、Rubyエスケープ文字を使用してモジュールから変数を注入することです。 システムの完全修飾ドメイン名を保存するFQDNがあります。 これは*システムファクト*として知られています。

システムファクトは、各システムのパペットカタログを生成する前に各システムから収集されます。 Puppetはfacterコマンドを使用してこの情報を取得し、facterを使用してシステムに関するその他の詳細を取得できます。 httpd.ppマニフェストファイルにハイライト行を追加する必要があります。

class test-module::httpd {
   package { 'httpd':
      ensure => installed,
   }
   service { 'httpd':
      ensure => running,
      enable => true,
      require => Package["httpd"],
   }
   file {'/etc/httpd/conf.d/testserver.conf':
      notify => Service["httpd"],
      ensure => file,
      require => Package["httpd"],
      content => template("test-module/testserver.conf.erb"),
   }
   file { "/var/www/myserver":
      ensure => "directory",
   }
}

これは、次のことを達成するのに役立ちます-

  • これにより、サーバー構成ファイル(/etc/httpd/conf.d/test-server.conf)のファイルリソース宣言が追加されます。 このファイルの内容は、以前に作成されたtest-serverconf.erbテンプレートです。 このファイルを追加する前に、インストールされているhttpdパッケージも確認します。
  • これは、Webサーバーのディレクトリ(/var/www/test-server)を作成する2番目のファイルリソース宣言を追加します。
  • 次に、 notify ⇒ Service ["httpd"] attribute を使用して、構成ファイルとhttpsサービスの関係を追加します。 これにより、構成ファイルに変更があるかどうかがチェックされます。 存在する場合、Puppetはサービスを再起動します。

次に、メインマニフェストファイルにhttpd_portを含めます。 そのためには、メインのinit.ppマニフェストファイルを終了し、次のコンテンツを含める必要があります。

class test-module (
   $http_port = 80
) {
   include test-module::httpd
}

これにより、httpdポートがデフォルト値の80に設定されます。 次に、Puppet applyコマンドを実行します。

出力は次のようになります。

# puppet apply test-module/tests/init.pp --noop
Warning: Config file/etc/puppet/hiera.yaml not found, using Hiera
defaults

Notice: Compiled catalog for puppet.example.com in environment
production in 0.84 seconds

Notice:/Stage[main]/test-module::Httpd/File[/var/www/myserver]/ensure:
current_value absent, should be directory (noop)

Notice:/Stage[main]/test-module::Httpd/Package[httpd]/ensure:
current_value absent, should be present (noop)

Notice:
/Stage[main]/test-module::Httpd/File[/etc/httpd/conf.d/myserver.conf]/ensure:
current_value absent, should be file (noop)

Notice:/Stage[main]/test-module::Httpd/Service[httpd]/ensure:
current_value stopped, should be running (noop)

Notice: Class[test-module::Httpd]: Would have triggered 'refresh' from 4
events

Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.51 seconds

ファイアウォールの構成

サーバーと通信するには、ポートを開く必要があります。 ここでの問題は、異なる種類のオペレーティングシステムが異なる方法でファイアウォールを制御することです。 Linuxの場合、6より下のバージョンはiptablesを使用し、バージョン7はfirewalldを使用します。

適切なサービスを使用するというこの決定は、システムファクトとそのロジックを使用してPuppetによってある程度処理されます。 そのためには、まずOSを確認してから、適切なファイアウォールコマンドを実行する必要があります。

これを実現するには、testmodule
httpクラス内に次のコードスニペットを追加する必要があります。
if $operatingsystemmajrelease <= 6 {
   exec { 'iptables':
      command => "iptables -I INPUT 1 -p tcp -m multiport --ports
      ${httpd_port} -m comment --comment 'Custom HTTP Web Host' -j ACCEPT &&
      iptables-save >/etc/sysconfig/iptables",
      path => "/sbin",
      refreshonly => true,
      subscribe => Package['httpd'],
   }
   service { 'iptables':
      ensure => running,
      enable => true,
      hasrestart => true,
      subscribe => Exec['iptables'],
   }
}  elsif $operatingsystemmajrelease == 7 {
   exec { 'firewall-cmd':
      command => "firewall-cmd --zone=public --addport = $ {
      httpd_port}/tcp --permanent",
      path => "/usr/bin/",
      refreshonly => true,
      subscribe => Package['httpd'],
   }
   service { 'firewalld':
      ensure => running,
      enable => true,
      hasrestart => true,
      subscribe => Exec['firewall-cmd'],
   }
}

上記のコードは次を実行します-

  • operatingsystemmajrelease を使用すると、使用されているOSがバージョン6か7かが決まります。
  • バージョンが6の場合、必要なすべての構成コマンドを実行して、Linux 6バージョンを構成します。
  • OSバージョンが7の場合、ファイアウォールの構成に必要なすべてのコマンドが実行されます。
  • 両方のOSのコードスニペットには、httpパッケージのインストール後にのみ構成が実行されるようにするロジックが含まれています。

最後に、Puppet applyコマンドを実行します。

# puppet apply test-module/tests/init.pp --noop
Warning: Config file/etc/puppet/hiera.yaml not found, using Hiera
defaults

Notice: Compiled catalog for puppet.example.com in environment
production in 0.82 seconds

Notice:/Stage[main]/test-module::Httpd/Exec[iptables]/returns:
current_value notrun, should be 0 (noop)

Notice:/Stage[main]/test-module::Httpd/Service[iptables]: Would have
triggered 'refresh' from 1 events

SELinuxの構成

バージョン7以降のLinuxマシンで作業しているため、http通信を行うように設定する必要があります。 SELinuxは、デフォルトでHTTPサーバーへの非標準アクセスを制限します。 カスタムポートを定義する場合、SELinuxを構成してそのポートへのアクセスを提供する必要があります。

Puppetには、ブール値やモジュールなどのSELinux機能を管理するためのリソースタイプが含まれています。 ここでは、ポート設定を管理するためにsemanageコマンドを実行する必要があります。 このツールはpolicycoreutils-pythonパッケージの一部であり、デフォルトではred-hatサーバーにはインストールされません。 上記を実現するには、test-module
httpクラス内に次のコードを追加する必要があります。
exec { 'semanage-port':
   command => "semanage port -a -t http_port_t -p tcp ${httpd_port}",
   path => "/usr/sbin",
   require => Package['policycoreutils-python'],
   before => Service ['httpd'],
   subscribe => Package['httpd'],
   refreshonly => true,
}

package { 'policycoreutils-python':
   ensure => installed,
}

上記のコードは次を実行します-

  • require ⇒ Package ['policycoreutils-python']は、必要なpythonモジュールがインストールされていることを確認します。
  • Puppetはsemanageを使用して、httpd_portを検証可能として使用してポートを開きます。
  • before ⇒サービスは、httpdサービスが開始する前にこのコマンドを実行するようにします。 SELinuxコマンドの前にHTTPDが開始された場合、SELinuxはサービスリクエストとサービスリクエストを失敗させます。

最後に、Puppet applyコマンドを実行します

# puppet apply test-module/tests/init.pp --noop
...
Notice:/Stage[main]/test-module::Httpd/Package[policycoreutilspython]/
ensure: current_value absent, should be present (noop)
...
Notice:/Stage[main]/test-module::Httpd/Exec[semanage-port]/returns:
current_value notrun, should be 0 (noop)
...
Notice:/Stage[main]/test-module::Httpd/Service[httpd]/ensure:
current_value stopped, should be running (noop)

Puppetは最初にpythonモジュールをインストールしてからポートアクセスを構成し、最後にhttpdサービスを開始します。

WebホストでのHTMLファイルのコピー

上記の手順で、httpサーバーの構成が完了しました。 これで、Puppetが設定できるWebベースのアプリケーションをインストールする準備ができたプラットフォームができました。 テストするために、いくつかのサンプルhtmlインデックスWebページをサーバーにコピーします。

ファイルディレクトリ内にindexlファイルを作成します。

<html>
   <head>
      <title>Congratulations</title>
   <head>

   <body>
      <h1>Congratulations</h1>
      <p>Your puppet module has correctly applied your configuration.</p>
   </body>
</html>

マニフェストディレクトリ内にマニフェストapp.ppを作成し、次のコンテンツを追加します。

class test-module::app {
   file { "/var/www/test-server/indexl":
      ensure => file,
      mode => 755,
      owner => root,
      group => root,
      source => "puppet:///modules/test-module/indexl",
      require => Class["test-module::httpd"],
   }
}
この新しいクラスには、単一のリソース減速が含まれています。 これにより、モジュールのファイルディレクトリからウェブサーバーにファイルがコピーされ、その権限が設定されます。 必須属性により、test-module
httpクラスがtest-module :: appを適用する前に構成を正常に完了することが保証されます。

最後に、メインのinit.ppマニフェストに新しいマニフェストを含める必要があります。

class test-module (
   $http_port = 80
) {
   include test-module::httpd
   include test-module::app
}

次に、applyコマンドを実行して、実際に何が起こっているかをテストします。 出力は次のようになります。

# puppet apply test-module/tests/init.pp --noop
Warning: Config file/etc/puppet/hiera.yaml not found, using Hiera
defaults

Notice: Compiled catalog for brcelprod001.brcle.com in environment
production in 0.66 seconds

Notice:/Stage[main]/Test-module::Httpd/Exec[iptables]/returns:
current_value notrun, should be 0 (noop)

Notice:/Stage[main]/Test-module::Httpd/Package[policycoreutilspython]/
ensure: current_value absent, should be present (noop)

Notice:/Stage[main]/Test-module::Httpd/Service[iptables]: Would have
triggered 'refresh' from 1 events

Notice:/Stage[main]/Test-module::Httpd/File[/var/www/myserver]/ensure:
current_value absent, should be directory (noop)

Notice:/Stage[main]/Test-module::Httpd/Package[httpd]/ensure:
current_value absent, should be present (noop)

Notice:
/Stage[main]/Test-module::Httpd/File[/etc/httpd/conf.d/myserver.conf]/ensur
e: current_value absent, should be file (noop)

Notice:/Stage[main]/Test-module::Httpd/Exec[semanage-port]/returns:
current_value notrun, should be 0 (noop)

Notice:/Stage[main]/Test-module::Httpd/Service[httpd]/ensure:
current_value stopped, should be running (noop)

Notice: Class[test-module::Httpd]: Would have triggered 'refresh' from 8
Notice:
/Stage[main]/test-module::App/File[/var/www/myserver/indexl]/ensur:
current_value absent, should be file (noop)

Notice: Class[test-module::App]: Would have triggered 'refresh' from 1
Notice: Stage[main]: Would have triggered 'refresh' from 2 events Notice:
Finished catalog run in 0.74 seconds

強調表示された行は、indexlファイルがWebホストにコピーされた結果を示しています。

モジュールの完成

上記のすべての手順で、作成した新しいモジュールを使用する準備が整いました。 モジュールのアーカイブを作成する場合は、次のコマンドを使用して実行できます。

# puppet module build test-module