情報畑でつかまえてロゴ
本サイトは NTTテクノクロスが旬の IT をキーワードに
IT 部門が今知っておきたい最新テクノロジーに関する情報をお届けするサイトです

Terraform連載 第8回:Terraformの管理スコープとしてiDRACをimportしてみた

Terraform連載企画!今回はTerraform Provider for RedFishを用いて、iDRACをTerraformから操作してみました

terraform_banner.pngはじめに

こんにちは、NTTテクノクロスの平川です。

今回はTerraformでiDRACを管理してみようと思います。

関連記事:
Terraform連載 第1回:いまさら聞けない、IaCってなに?~Terraform、IaSQLの紹介~
Terraform連載 第2回:Terraform v1.5の紹介&活用方法について考えてみた
Terraform連載 第3回:Terraform v1.6のtestコマンドについてご紹介
Terraform連載 第4回:for_eachの使い方
Terraform連載 第5回:module(モジュール)の紹介
Terraform連載 第6回:Terraform v1.7 removedブロックの紹介
Terraform連載 第7回:importブロックはmoduleのリソースも取り込めるか?
「Terraform連載 第8回:Terraformの管理スコープとしてiDRACをimportしてみた」

モチベーションは2つあります。

  • ・一風変わったTerraformのProviderを使ってみたかった。
  • ・同時に10台以上のiDRACのコンフィグレーションの機会があって自動化したくなった。

用語説明

iDRAC

Dell PowerEdgeサーバの導入、更新、監視、保守、管理を目的として設計されているIPMIの一種です。

IPMI

IPMI (Intelligent Platform Management Interface)とは、サーバ管理のためのインターフェース規格であり、様々なサーバベンダがIPMIを提供しています。

Terraform Provider for RedFish

Terraform Provider for RedFishはTerraformからiDRACのAPIを制御するためのTerraform Providerのことです。 iDRACにはRedFishと呼ばれるREST APIを利用する形でTerraformからのiDRAC操作を実現しています。

RedFish自体は複数のベンダがサポートしているためiDRAC以外のIPMIも操作できる可能性はありますが今回は検証していません。

Terraform環境の構築

以下の手順でterraformコマンドが実行できるようにします。

$ sudo apt-get install -y zip
$ git clone https://github.com/tfutils/tfenv.git ~/.tfenv
$ touch .bash_profile
$ echo export PATH='$HOME/.tfenv/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile
$ tfenv install 1.7.3
$ tfenv use 1.7.3
$ terraform --version
Terraform v1.7.3
on linux_amd64

既存リソースのimport検証

iDRACに対してWebブラウザからアクセスし、任意のユーザを手動で作成した後に、そのユーザがTerraformでimportできるかを確認します。

前提

前提として接続先のiDRACは以下の接続情報とします。このケースではRedfish APIの操作を行うのはrootユーザとなります。

  • ・URL:https://192.168.100.105/
  • ・login user:root
  • ・login pass:root2024
  • ・import user id: 10

以下の10番目の「tf_test」ユーザを今回のimportの確認対象とします。

tf_idrac1.pngのサムネイル画像

環境準備

backend.tfを作成します。以下は作成済みのbackend.tfをcatした結果となります。

$ ~/iac_tf/import$ cat backend.tf
terraform {
 required_providers {
  redfish = {
   version = "1.1.0"
   source  = "registry.terraform.io/dell/redfish"
  }
 }
}
provider "redfish" {
 
}

backend.tfが存在するディレクトリでterraform initを実行します。

$ ~/iac_tf/import$ terraform init
 
Initializing the backend...
 
Initializing provider plugins...
- Reusing previous version of dell/redfish from the dependency lock file
- Using previously-installed dell/redfish v1.1.0
 
Terraform has been successfully initialized!
 
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
 
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

作成したユーザをimportするための空の定義ファイルを作成します。以下は作成済みのuser_import.tfをcatした結果となります。

~/iac_tf/import $ cat user_import.tf
resource "redfish_user_account" "rr" {
}

既存ユーザのimportとTerraformからのユーザ削除

ワンライナーのterraform importコマンドを実行します。

$ terraform import redfish_user_account.rr "{\"id\":\"10\", \"username\":\"root\",\"password\":\"root2024\",\"endpoint\":\"https://192.168.100.105\",\"ssl_insecure\":true}"

importしたいユーザ(今回は10番目のユーザ)がiDRAC上に存在する場合は以下のようにimportできます。

redfish_user_account.rr: Importing from ID "{\"id\":\"10\", \"username\":\"root\",\"password\":\"root2024\",\"endpoint\":\"https://192.168.100.105\",\"ssl_insecure\":true}"...
redfish_user_account.rr: Import prepared!
 Prepared redfish_user_account for import
redfish_user_account.rr: Refreshing state... [id=10]
 
Import successful!
 
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

terraform destoryすることでimportしたユーザが削除できるかを確認します。

ユーザ削除するためにuser_import.tfを更新します。user_import.tfを記述する際はterraform importにより更新されたterraform.tfstateファイルを参考にするとよいです。この時、tf_testユーザのpasswordについては実際のパスワードと一致していなくてもdestoryに成功することを確認しているため"null"としています。

$ cat user_import.tf
 
resource "redfish_user_account" "rr" {
 username = "tf_test"
 enabled = true
 password = "null"
 redfish_server {
  endpoint =  "https://192.168.100.105"
  user = "root"
  password =  "root2024"
  ssl_insecure =  true
 }
 role_id = "None"
 user_id = "10"
}

terraform destoryコマンドを実行します。

$ terraform destroy
redfish_user_account.rr: Refreshing state... [id=10]
 
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
 - destroy
 
Terraform will perform the following actions:
 
 # redfish_user_account.rr will be destroyed
 - resource "redfish_user_account" "rr" {
   - enabled  = true -> null
   - id       = "10" -> null
   - role_id  = "None" -> null
   - user_id  = "10" -> null
   - username = "tf_test" -> null
 
   - redfish_server {
     - endpoint     = "https://192.168.100.105" -> null
     - password     = (sensitive value) -> null
     - ssl_insecure = true -> null
     - user         = "root" -> null
    }
  }
 
Plan: 0 to add, 0 to change, 1 to destroy.
 
Do you really want to destroy all resources?
 Terraform will destroy all your managed infrastructure, as shown above.
 There is no undo. Only 'yes' will be accepted to confirm.
 
 Enter a value: yes
 
redfish_user_account.rr: Destroying... [id=10]
redfish_user_account.rr: Still destroying... [id=10, 10s elapsed]
redfish_user_account.rr: Still destroying... [id=10, 20s elapsed]
redfish_user_account.rr: Destruction complete after 23s
 
Destroy complete! Resources: 1 destroyed.

無事にtf_testユーザが削除できました。

tf_idrac2.png

一方でユーザの状況が「無効」ではなく「有効」のままになってしまっている問題が確認できます。ただ、ユーザ自体は削除できていますので今回は許容することとします。
今後の動きとしてIssueを立てるなどは検討してみます。

まとめ

Terraform Provider for RedFishを用いてiDRACをTerraformから操作できることを確認できました。今回はimportのみの簡単な確認ですがTerraform側からの「新規ユーザ作成」や「複数のサーバを対象にサーバ再起動、次回ブート時のUEFI起動」など様々な機能があるようなのでちょっとしたところで活用できそうであると考えられます。Terraform Provider for RedFishは様々な機能を有するので今後は他の機能の検証であったり、iDRAC以外の別のベンダのIPMIについても検証スコープに含めたいです。

IOWNデジタルツイン事業部では、Terraform EnterpriseおよびTerraform Cloud Plusに関する、日本語による保守サポートをご提供しております。Terraformの導入にあたってお悩みの方は、ぜひお気軽にご相談ください。

HashiCorp製品サポートサービス(弊社ホームページ)

連載シリーズ
著者プロフィール
平川 裕弥
平川 裕弥

NTTテクノクロス株式会社
ⅠOWNデジタルツイン事業部
第三ビジネスユニット