5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleでvagrant plugin install

Last updated at Posted at 2015-03-23

AnsibleでMacの開発環境を整える際にぶち当たった問題。
vagrant plugin installは既にインストールされていてもそのまま強制的に上書きインストールを始めてしまう。

シェルスクリプトでBrewfile的なものを書いていた時は簡単にインストールをスキップする処理を書けたが、ansibleで同じことを実現するのにやや詰まったのでメモ。

いままで

今までは以下のようにしていたから少し気持ち悪かった。

vagrant-plugin-install.yml
---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks: 
    - name: Install Vagrant Plugins
      shell: "vagrant plugin list | grep {{item}} || vagrant plugin install {{item}}"
      with_items:
          - vagrant-omnibus
          - vagrant-vbguest
          - vagrant-cachier
          - vagrant-vbox-snapshot
          - vagrant-aws
          - vagrant-digitalocean

これだと、以下のようにitem全てについてchangedが出てしまう。しかもループごとに毎回vagrant plugin listを実行するので遅い。

見つけた解

一つのスマートな解として以下の様なものに落ち着いた。

vagrant-plugin-install.yml
---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks: 
    - name: List Installed Vagrant Plugins
      shell: "vagrant plugin list | awk '{ print $1 }'"
      changed_when: false
      register: vagrant_plugin_list

    - name: Install Vagrant Plugins
      shell: "vagrant plugin install {{item}}"
      with_items:
        - vagrant-omnibus
        - vagrant-vbguest
        - vagrant-cachier
        - vagrant-vbox-snapshot
        - vagrant-aws
        - vagrant-digitalocean
      when: item not in vagrant_plugin_list.stdout_lines

出力は以下。


ASK: [List Installed Vagrant Plugins] ****************************************
ok: [localhost]

TASK: [Install Vagrant Plugins] ***********************************************
skipping: [localhost] => (item=vagrant-omnibus)
skipping: [localhost] => (item=vagrant-vbguest)
skipping: [localhost] => (item=vagrant-cachier)
skipping: [localhost] => (item=vagrant-vbox-snapshot)
skipping: [localhost] => (item=vagrant-aws)
skipping: [localhost] => (item=vagrant-digitalocean)

1つ目のtaskでvagrant plugin listの出力をregisterしておく。awkで一つ目のフィールド(プラグイン名のみ)を取り出す。

2つ目のtaksでループ。ただし1つ目のタスクの出力で出ていないもののみ実行。

ポイントはstdout_linesでした。grep芸で実行するよりもスマートで、シェルシェルしないAnsibleらしいものになったかなと。

ただskippingよりokのほうが気持ちがいい・・・。okにする方法は今後思いついたら書く。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?