LoginSignup
16
18

More than 5 years have passed since last update.

ansible でファイルの中身matchで実行したいなら when + lookupがオススメ!

Last updated at Posted at 2016-01-27

コントロール側にあるファイル(設定ファイル等)の中身を正規表現で match したときのみ実行したい場合のベストプラクティスを考えてみました。

ファイルの中身が正規表現でmatchした場合のみ実行
- debug: msg="foo"  # 実行したいコマンド
  when: lookup("file", ファイルのパス) | match("正規表現")

ファイルが無くてもエラーにならないようにしたいなら、これ と組み合わせて:

ファイルが無くてもエラーにならない
- debug: msg="{{item}}"  # 実行したいコマンド
  when: item != None and lookup("file", item ) | match("正規表現")
  with_first_found:
    - ファイルのパス

とすればOKです。

lookup() はいろいろできて便利です。 lookup('pipe', 'シェル') でシェルの結果をもとに何かすることもできますし、CSV や ini ファイルから特定情報を取り出す機能も持っています。要望があればまた、記事にしますね。

こっちのほうが良い! という方法がありましたら、是非、コメントください!!

16
18
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
18