ansibleっぽいitamaeっぽいプロビジョニングツールminarai作った

github.com

作りました. ansibleとitamaeの名前を出しましたがお手本にしたのは @r7kamuraさんが作ったr7kamura/serverkit · GitHub というgemです.

動機

これが欲しかったわけではないんですがgem作ったことなかったし作りたいものもなかったのでなんか真似しようとおもって色々探してたらserverkitが出てきてちょうどよかったので自分でも書いてみました.

使い方

一番シンプルな使い方はrecipe.ymlを以下の内容で作って

- name: clone codic-clojure
  type: git
  repository: https://github.com/ganmacs/codic-clojure.git
  destination: codic-clojure

- name: clone dot_emacs
  type: git
  repository: https://github.com/ganmacs/emacs.d.git
  destination: .emacs.d

以下のコマンドを実行するだけです.また、既に目的のファイルが存在する場合などはスキップされるようになってます.

$ minarai recipe.yml
INFO : Minarai starting...
INFO : [SKIP] clone codic-clojure
INFO : [DONE] clone dot_emacs
INFO : Minarai finish

以上でgit cloneしてきてくれます.

変数を使った場合

recipeに変数を使うこともできます.

まずvars.ymlに以下を記述します.

remote_dotfile_repo: git@github.com:ganmacs/dotfiles.git
tmp_dir: /Users/ganmacs/tmp
dotfile_path: /Users/ganmacs/tmp/dotfile
vim_config_in_dotfile: /Users/ganmacs/tmp/dotfile/vim/vimrc
dot_vim_path: /Users/ganmacs/tmp/.vim

そしてrecipe.ymlに以下を記述します

- name: install vim
  type: homebrew
  item: vim

- name: create tmp directory
  type: directory
  destination: <%= tmp_dir %>

- name: clone dotfile
  type: git
  repository: <%= remote_dotfile_repo %>
  destination: <%= dotfile_path %>

- name: link dotfile to hgoe
  type: link
  source: <%= vim_config_in_dotfile %>
  destination: <%= dot_vim_path %>

そして実行すれば終わりです

$ minarai recipe_erb.yml.erb --variables=vars.yml
INFO : Minarai starting...
INFO : [SKIP] install vim
INFO : [DONE] create tmp directory
INFO : [DONE] clone dotfile
INFO : [DONE] link dotfile to hgoe
INFO : Minarai finish

機能

とにかくシンプルにしたくて他の構成管理ツールみたいに高機能なものを作るのはやめました. 基本的にローカルのMacの構成管理のためにしか使う気はないのでsshもできないです. コマンドも実行するrecipeを渡すだけです.(変数は使えますが)

実行できるタスク(アクション)は以下のとおりです

  • git - git cloneしてきます
  • file - fileのコピーをします
  • link - symlinkをはります
  • directory - directoryを作ります
  • homebrew - homebrewでパッケージをインストールします
  • homebrew_cask - homebrew_caskでappをインストールします
  • url_get - 指定したurlからcurlしてきます

感想

ansibleとitamaeはすごいなーって感想とserverkitのコードが読みやすくて最高って感じでした.

今後

rbenvとかdefaultとかcommandのactionも増やして構成管理に使っていきたい

関連

Serverkitつくった - ✘╹◡╹✘

itamae-kitchen/itamae · GitHub

ansible/ansible · GitHub