今回はみんな大好きGolangをAnsibleでインストールしてみました。なかなかググっても出てこないので参考に!!
■tree
| 1 2 3 4 5 6 7 8 | $ tree go go ├── files │   └── goenv.sh └── tasks     └── main.yml 2 directories, 2 files | 
■main.yml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | - name: unzip go 1.11.1   unarchive:     src: https://dl.google.com/go/go1.11.1.linux-amd64.tar.gz     dest: /usr/local     remote_src: yes   ignore_errors: yes - name: copy goenv.sh   copy:     src: "{{ item.src }}"     dest: "/etc/profile.d/{{ item.dest }}"     mode: 0755     backup: no     owner: root     group: root   loop:     - { src: roles/go/files/goenv.sh, dest: . } - name: source /etc/profile.d/goenv.sh   shell: bash -lc "source /etc/profile.d/goenv.sh"   ignore_errors: yes - name: go get    shell: >       bash -lc "go get -u github.com/pressly/goose/cmd/goose";     bash -lc "go get -u github.com/lestrrat-go/test-mysqld";     bash -lc "go get -u github.com/volatiletech/sqlboiler";     bash -lc "go get -u github.com/volatiletech/sqlboiler/drivers/sqlboiler-mysql";     bash -lc "go get -u github.com/golang/dep/cmd/dep";     bash -lc "go get -u github.com/tockins/realize";   ignore_errors: yes | 
・goenv.sh
| 1 2 3 | export GOROOT=/usr/local/go export GOPATH=/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin | 
今回はgo.1.11.1をインストールしていますが、goenv.shは/etc/profile.d配下に置くことで、全ユーザがログイン時にgoのPATHを読み込むようにしています。23行目はshellモジュールを使ってgo getしていますが、数が多いのでメンテしやすいようにまとめています。ansible 2.5からwith_itemsがdeprecatedされるのでloopに変更してあげましょう。
■まとめ
究極の冪等性。もっとリファクタリングできるけどもとりあえずいいかな。
 
													 
													 
													
0件のコメント