Debuginfo

思考とアウトプット

ansibleでファイルの存在を調べて、taskを実行する(しない)方法

細かいところでGoogleにお世話になりますね、ansibleは。。

いわゆる、'if [ -f /foo/bar ] then' 的なファイルの確認(test)をして、その結果の条件に対して、その次のタスクを実行する、しないをきめるplaybookは下記のようにする。

この例は、packerが/usr/local/binにインストールされていたら、ダウンロードと展開をスキップするという設定。

---
- name: check whether packer exists or not
  shell: "[ -f /usr/local/bin/packer ] && echo 'Found' || echo ''"
  register: packer_installed
- name: download packer zip
  get_url: url="https://dl.bintray.com/mitchellh/packer/0.5.2_linux_386.zip" dest=/tmp/packer.zip
  when: (not packer_installed.stdout)
- name: extract to /usr/local/bin
  shell: unzip -d /usr/local/bin /tmp/packer.zip
  when: (not packer_installed.stdout)

参考

https://github.com/markmandel/dotfiles/blob/master/provision/roles/core/tasks/_oh_my_zsh.yml