Puppet-facter-facts

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

人形-事実と事実

Puppetは、環境変数として複数の値を保持することをサポートしています。 この機能は、 facter を使用してPuppetでサポートされています。 Puppetでは、ファクトは環境レベル変数を保持するスタンドアロンツールです。 Inは、BashまたはLinuxのenv変数に似ていると考えることができます。 ファクトに保存されている情報とマシンの環境変数が重複している場合があります。 Puppetでは、キーと値のペアは「ファクト」として知られています。 各リソースには独自のファクトがあり、Puppetでは、ユーザーは独自のカスタムファクトを構築するために活用できます。

# facter
  • Facterコマンド*を使用して、すべての異なる環境変数とそれに関連する値をリストできます。 これらのファクトのコレクションには、すぐに使えるファクトが付属しており、コアファクトと呼ばれます。 コレクションにカスタムファクトを追加できます。

1つの変数のみを表示する場合。 次のコマンドを使用して実行できます。

# facter {Variable Name}

Example
[root@puppetmaster ~]# facter virtual
virtualbox

ファクターがPuppetにとって重要である理由は、ファクターとファクトがPuppetコード全体で*「グローバル変数」*として利用可能であるためです。

テストする例

[root@puppetmaster modules]# tree brcle_account
brcle_account
└── manifests  └── init.pp [root@puppetmaster modules]# cat brcle_account/manifests/init.pp
class brcle_account {
   user { 'G01063908':
      ensure => 'present',
      uid => '121',
      shell => '/bin/bash',
      home => '/home/G01063908',
   }

   file {'/tmp/userfile.txt':
      ensure => file,
      content => "the value for the 'OperatingSystem' fact is: $OperatingSystem \n",
   }
}

それをテストする

[root@puppetmaster modules]# puppet agent --test
Notice:/Stage[main]/Activemq::Service/Service[activemq]/ensure:
ensure changed 'stopped' to 'running'
Info:/Stage[main]/Activemq::Service/Service[activemq]:
Unscheduling refresh on Service[activemq]

Notice: Finished catalog run in 4.09 seconds
[root@puppetmaster modules]# cat/tmp/testfile.txt
the value for the 'OperatingSystem' fact is: Linux

[root@puppetmaster modules]# facter OperatingSystem
Linux

上記のコードスニペットでわかるように、 OperatingSystem を定義していません。 値を通常の変数としてソフトコード値 $ OperatingSystem に置き換えました。

Puppetでは、使用および定義できる事実には3つのタイプがあります-

  • コアファクト
  • カスタムファクト
  • 外部の事実

コアファクトはトップレベルで定義され、コード内のすべてのポイントでアクセスできます。

人形の事実

エージェントがマスターにカタログを要求する直前に、エージェントはまず、キー値ペアの形式でそれ自体で利用可能な情報の完全なリストをコンパイルします。 エージェントに関する情報は、ファクターと呼ばれるツールによって収集され、各キーと値のペアはファクトと呼ばれます。 以下は、エージェントに関するファクトの一般的な出力です。

[root@puppetagent1 ~]# facter
architecture => x86_64
augeasversion => 1.0.0
bios_release_date => 13/09/2012
bios_vendor => innotek GmbH
bios_version => VirtualBox
blockdevice_sda_model => VBOX HARDDISK
blockdevice_sda_size => 22020587520
blockdevice_sda_vendor => ATA
blockdevice_sr0_model => CD-ROM
blockdevice_sr0_size => 1073741312
blockdevice_sr0_vendor => VBOX
blockdevices => sda,sr0
boardmanufacturer => Oracle Corporation
boardproductname => VirtualBox
boardserialnumber => 0

domain => codingbee.dyndns.org
facterversion => 2.1.0
filesystems => ext4,iso9660
fqdn => puppetagent1.codingbee.dyndns.org
hardwareisa => x86_64
hardwaremodel => x86_64
hostname => puppetagent1
id => root
interfaces => eth0,lo
ipaddress => 172.228.24.01
ipaddress_eth0 => 172.228.24.01
ipaddress_lo => 127.0.0.1
is_virtual => true
kernel => Linux
kernelmajversion => 2.6
kernelrelease => 2.6.32-431.23.3.el6.x86_64
kernelversion => 2.6.32
lsbdistcodename => Final
lsbdistdescription => CentOS release 6.5 (Final)
lsbdistid => CentOS
lsbdistrelease => 6.5
lsbmajdistrelease => 6
lsbrelease => :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0noarch:graphics-4.0-amd64:
graphics-4.0-noarch:printing-4.0-amd64:printing-4.0noarch
macaddress => 05:00:22:47:H9:77
macaddress_eth0 => 05:00:22:47:H9:77
manufacturer => innotek GmbH
memoryfree => 125.86 GB
memoryfree_mb => 805.86
memorysize => 500 GB
memorysize_mb => 996.14
mtu_eth0 => 1500
mtu_lo => 16436
netmask => 255.255.255.0
netmask_eth0 => 255.255.255.0

network_lo => 127.0.0.0
operatingsystem => CentOS
operatingsystemmajrelease => 6
operatingsystemrelease => 6.5
osfamily => RedHat
partitions => {"sda1"=>{
"uuid"=>"d74a4fa8-0883-4873-8db0-b09d91e2ee8d", "size" =>"1024000",
"mount" => "/boot", "filesystem" => "ext4"}, "sda2"=>{"size" => "41981952",
"filesystem" => "LVM2_member"}
}
path =>/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
physicalprocessorcount => 1
processor0 => Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz
processor1 => Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz
processor2 => Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz
processorcount => 3
productname => VirtualBox
ps => ps -ef
puppetversion => 3.6.2
rubysitedir =>/usr/lib/ruby/site_ruby/1.8
rubyversion => 1.8.7
selinux => true
selinux_config_mode => enforcing
selinux_config_policy => targeted
selinux_current_mode => enforcing
selinux_enforced => true
selinux_policyversion => 24
serialnumber => 0
sshdsakey => AAAAB3NzaC1kc3MAAACBAK5fYwRM3UtOs8zBCtRTjuHLw56p94X/E0UZBZwFR3q7
WH0x5+MNsjfmdCxKvpY/WlIIUcFJzvlfjXm4qDaTYalbzSZJMT266njNbw5WwLJcJ74KdW92ds76pjgm
CsjAh+R9YnyKCEE35GsYjGH7whw0gl/rZVrjvWYKQDOmJA2dAAAAFQCoYABgjpv3EkTWgjLIMnxA0Gfud
QAAAIBM4U6/nerfn6Qvt43FC2iybvwVo8ufixJl5YSEhs92uzsW6jiw68aaZ32q095/gEqYzeF7a2knr
OpASgO9xXqStYKg8ExWQVaVGFTR1NwqhZvz0oRSbrN3h3tHgknoKETRAg/imZQ2P6tppAoQZ8wpuLrXU
CyhgJGZ04Phv8hinAAAAIBN4xaycuK0mdH/YdcgcLiSn8cjgtiETVzDYa+jF
swapfree => 3.55 GB
swapfree_mb => 2015.99
swapsize => 3.55 GB
swapsize_mb => 2015.99
timezone => GMT
type => Other
uniqueid => a8c0af01
uptime => 45:012 hours
uptime_days => 0
uptime_hours => 6
uptime_seconds => 21865
uuid => BD8B9D85-1BFD-4015-A633-BF71D9A6A741
virtual => virtualbox

上記のコードでは、bashの「env」変数で使用可能な情報の一部とデータの一部が重複していることがわかります。 Puppetはデータを直接使用せず、代わりにファクターデータを使用します。Facterデータはグローバル変数として扱われます。

ファクトはトップレベル変数として利用可能になり、Puppetマスターはそれらを使用して、要求元エージェントのPuppetカタログをコンパイルできます。 係数は、$プレフィックス付きの通常の変数としてマニフェストで呼び出されます。

if ($OperatingSystem == "Linux") {
   $message = "This machine OS is of the type $OperatingSystem \n"
} else {
   $message = "This machine is unknown \n"
}

file { "/tmp/machineOperatingSystem.txt":
   ensure => file,
   content => "$message"
}

上記のマニフェストファイルは、 machineOperatingSystem.txt という単一のファイルについてのみ気にします。このファイルの内容は、 OperatingSystem という事実によって差し引かれます。

[root@puppetagent1/]# facter OperatingSystem
Linux

[root@puppetagent1/]# puppet apply/tmp/ostype.pp
Notice: Compiled catalog for puppetagent1.codingbee.dyndns.org
in environment production in 0.07 seconds
Notice:/Stage[main]/Main/File[/tmp/machineOperatingSystem.txt]/ensure:
defined content as '{md5}f59dc5797d5402b1122c28c6da54d073'
Notice: Finished catalog run in 0.04 seconds

[root@puppetagent1/]# cat/tmp/machinetype.txt
This machine OS is of the type Linux

カスタムファクト

私たちが見た上記の事実はすべて、マシンの中核的な事実です。 次の方法でノードにこのカスタムファクトを追加できます-

  • 「export FACTER…構文」を使用する
  • $ LOAD_PATH設定を使用する
  • ファクトライブラリ
  • Pluginsync

「export FACTER」構文の使用

export FACTER _ \ {fact’s name}構文を使用して、ファクトを手動で追加できます。

[root@puppetagent1 facter]# export FACTER_tallest_mountain="Everest"
[root@puppetagent1 facter]# facter tallest_mountain Everest

$ LOAD_PATH設定の使用

Rubyでは、$ LOAD_PATHはBashの特殊パラメーターと同等です。 bash $ PATH変数に似ていますが、実際には$ LOAD_PATHは環境変数ではなく、事前定義された変数です。

$ LOAD_PATHには同義語「$:」があります。 この変数は、値を検索してロードする配列です。

[root@puppetagent1 ~]# ruby -e 'puts $LOAD_PATH'
# note you have to use single quotes.
/usr/lib/ruby/site_ruby/1.6
/usr/lib64/ruby/site_ruby/1.6
/usr/lib64/ruby/site_ruby/1.6/x86_64-linux
/usr/lib/ruby/site_ruby
/usr/lib64/ruby/site_ruby
/usr/lib64/site_ruby/1.6
/usr/lib64/site_ruby/1.6/x86_64-linux
/usr/lib64/site_ruby
/usr/lib/ruby/1.6
/usr/lib64/ruby/1.6
/usr/lib64/ruby/1.6/x86_64-linux

ディレクトリファクタを作成し、*。pp *ファイルを追加し、それにコンテンツを追加する例を見てみましょう。

[root@puppetagent1 ~]# cd/usr/lib/ruby/site_ruby/
[root@puppetagent1 site_ruby]# mkdir facter
[root@puppetagent1 site_ruby]# cd facter/
[root@puppetagent1 facter]# ls
[root@puppetagent1 facter]# touch newadded_facts.rb

次のコンテンツをcustom_facts.rbファイルに追加します。

[root@puppetagent1 facter]# cat newadded_facts.rb
Facter.add('tallest_mountain') do
   setcode "echo Everest"
end

Facterは、$ LOAD_PATHにリストされているすべてのフォルダーをスキャンする方法で動作し、facterと呼ばれるディレクターを探します。 特定のフォルダーが見つかると、フォルダー構造内の任意の場所にロードされます。 このフォルダーを見つけると、そのファクターフォルダーでRubyファイルを探し、メモリ内の特定の構成に関するすべての定義済みファクトを読み込みます。

FACTERLIBを使用する

Puppetでは、FACTERLIBは$ LOAD_PATHと非常によく似ていますが、Rubyの特殊変数ではなく、OSレベルの環境パラメーターであるという重要な違いが1つあります。 デフォルトでは、環境変数は設定されていません。

[root@puppetagent1 facter]# env | grep "FACTERLIB"
[root@puppetagent1 facter]#

FACTERLIBをテストするには、次の手順を実行する必要があります。

次の構造にtest_factsというフォルダーを作成します。

[root@puppetagent1 tmp]# tree/tmp/test_facts/
/tmp/some_facts/
├── vipin
│   └── longest_river.rb
└── testing
   └── longest_wall.rb

rbファイルに次の内容を追加します。.

[root@puppetagent1 vipin]# cat longest_river.rb
Facter.add('longest_river') do
   setcode "echo Nile"
end

[root@puppetagent1 testing]# cat longest_wall.rb
Facter.add('longest_wall') do
   setcode "echo 'China Wall'"
end

exportステートメントを使用します。

[root@puppetagent1/]# export
FACTERLIB = "/tmp/some_facts/river:/tmp/some_facts/wall"
[root@puppetagent1/]# env | grep "FACTERLIB"
FACTERLIB =/tmp/some_facts/river:/tmp/some_facts/wall

新しいファクターをテストします。

[root@puppetagent1/]# facter longest_river
Nile
[root@puppetagent1/]# facter longest_wall
China Wall

外部の事実

ユーザーがプロビジョニング時に作成されたいくつかの新しいファクトを適用したい場合、外部ファクトは非常に役立ちます。 外部ファクトは、プロビジョニング段階でVMにメタデータを適用する重要な方法の1つです(例: vSphere、OpenStack、AWSなどを使用)

作成されたすべてのメタデータとその詳細をPuppetが使用して、カタログにどの詳細を適用するかを決定できます。

外部ファクトの作成

エージェントマシンで、以下に示すようにディレクトリを作成する必要があります。

$ mkdir -p/etc/facter/facts.d

ディレクトリに次の内容のシェルスクリプトを作成します。

$ ls -l/etc/facter/facts.d
total 4
-rwxrwxrwx. 1 root root 65 Sep 18 13:11 external-factstest.sh
$ cat/etc/facter/facts.d/external-factstest.sh
#!/bin/bash
echo "hostgroup = dev"
echo "environment = development"

スクリプトファイルの権限を変更します。

$ chmod u+x/etc/facter/facts.d/external-facts.sh

完了すると、キーと値のペアに存在する変数を見ることができます。

$ facter hostgroup
dev
$ facter environment
development

Puppetでカスタムファクトを作成できます。 参考として、Puppetサイトから次のリンクを使用してください。

https://docs.puppet.com/facter/latest/fact_overviewl#writing-structured-facts