LoginSignup
16
1

More than 1 year has passed since last update.

ラベルプリンタのネットワーク設定を管理する

Posted at

今年は CloudFormation と Terraform に触れました。ツールの機能上、向き不向きはありますが、設定値をテキストで管理できることは強みです。

設定の向き不向き、もしくは好みの問題かもしれませんが、LAN 内におけるネットワーク機器の IP アドレスというのも煩雑になりがちではないでしょうか。
そんな問題を SATO 製ラベルプリンタに限って解決する Terraform のカスタムプロバイダを作りました。

SATO 製のラベルプリンタは Terraform の実行ホストと同一のネットワークにいる必要があります。DHCP、もしくは静的 IP のどちらかに設定を適用できます。
↓の設定にある XXXXXX は MAC アドレスの末尾 3 オクテットで書くと識別子として使いやすいと思います。

terraform {
  required_providers {

    sato = {
      version = "0.2"
      source = "karonori.com/personal/sato"
    }
  }
}

provider "sato" {}

// for DHCP
resource "sato_network" "pXXXXXX" {
  hardware_address = "00:19:98:XX:XX:XX"
  dhcp = true
  rarp = true
}

// for static IP
resource "sato_network" "pXXXXXX" {
  hardware_address = "0019.98XX.XXXX"
  dhcp = false
  rarp = false
  ip_address = "192.168.0.10"
  subnet_mask = "255.255.255.0"
  gateway_address = "192.168.0.1"
}

terraform apply の出力例です。ただし設定を変更してもラベルプリンタの電源を手動で入れ直さないと反映されません。

$ terraform apply --auto-approve
module.sato.data.sato_networks.all: Reading...
module.sato.data.sato_networks.all: Read complete after 1s [id=1670078080]

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # sato_network.pXXXXXX will be created
  + resource "sato_network" "pXXXXXX" {
      + dhcp             = false
      + gateway_address  = "192.168.0.1"
      + hardware_address = "00:19:98:XX:XX:XX"
      + id               = (known after apply)
      + ip_address       = "192.168.0.10"
      + last_updated     = (known after apply)
      + rarp             = false
      + subnet_mask      = "255.255.255.0"
    }

Plan: 1 to add, 0 to change, 0 to destroy.
sato_network.pXXXXXX: Creating...
sato_network.pXXXXXX: Creation complete after 1s [id=00:19:98:XX:XX:XX]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
$

terraform import も動きます。

$ terraform import sato_network.pffffff 00:19:98:XX:XX:XX
sato_network.pffffff: Importing from ID "00:19:98:XX:XX:XX"...
module.sato.data.sato_networks.all: Reading...
sato_network.pffffff: Import prepared!
  Prepared sato_network for import
sato_network.pffffff: Refreshing state... [id=00:19:98:XX:XX:XX]
module.sato.data.sato_networks.all: Read complete after 1s [id=1670077885]

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.

$

これで SATO 製ラベルプリンタがたくさんあってもネットワーク設定に困りませんね。

16
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
1