LoginSignup
1

More than 5 years have passed since last update.

exec - refreshonly の使い所

Posted at

Puppetアドベントカレンダー4日目です。

長丁場なので軽めのtipsで恐縮です。あと21日...

execタイプのrefreshonly オプション

The exec has refreshonly => true, which only allows Puppet to run the command when some other resource is changed. (See the notes on refreshing below.)

とのことです。このオプション、冪等性の担保のために非常に便利に使えます。よくやっているパターンを二つ紹介します。

あるファイルの中身に変更があったら展開したい

class sample_install {

  file { '/usr/local/src/some-binary.zip':
    source  => 'puppet:///modules/sample_install/some-binary.zip',
    notify  => Exec['unzip some-binary.zip'],
  }

  exec { 'unzip some-binary.zip':
    command     => 'unzip -o -u /usr/local/src/some-binary.zip -d /usr/local/bin && chmod 775 /usr/local/bin/some-binary',
    refreshonly => true,
  }

}

こうすることで

  • 毎回ファイルを展開することを防ぐ
  • しかし、ソースファイルに変更があったら、確実に解凍させる

が実現可能です。zipball、tarballを直接つっこんでる場合に便利でしょう。hashicorp tools系とか。

あるファイルの更新に伴い、そのファイルの内容を反映させるようなコマンドをキックする

class sysctl::reload {

  exec { '/usr/sbin/sysctl -p /etc/sysctl.d/*.conf':
    refreshonly => true,
  }

}

class log::sysctl {
  include ::sysctl::reload

  file {
    '/etc/sysctl.d/td-agent.conf':
      content => template('log/sysctl.conf'),
      notify  => Class['::sysctl::reload'];
  }
}

典型的には、 systemctl daemon-reload とか、 sysctl -p をフックさせると便利ではないでしょうか。

親のClassにnotifyを仕掛ければ、そこに所属する exec タイプが全部キックされるようです。

こんな感じで、Puppet初心者の脱却に是非 refreshonly を活用していきましょう。


ということで明日は id:kijibato さんとなります。支援に感謝。

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
1