Ansible x Vagrant構築メモ

さくらのナレッジ - AnsibleとVagrantで開発環境を構築する をもとに軽くテスト的にMacで構築したときのメモ。
昔の記事なので少しだけうまくいかない箇所があるため、その補完用にメモ。

構築方法

さくらのナレッジ - AnsibleとVagrantで開発環境を構築する の大項目の中でハマった箇所のみ補完して以降に記載。

Vagrantfileの作成

config.vm.boxの名称は、 Discover Vagrant Boxes で検索して取得する。ちなみに、 [作成者]/[Box名] という命名規則になっている。
「bento」は「chef」が提供していて安心感があるため、今回はそれを利用。

[Vagrantfile]

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-10"

  config.vm.network "private_network", ip: "192.168.33.10"

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/site.yml"
    ansible.inventory_path = "provisioning/hosts"
    ansible.limit = 'all'
  end
end

provisioning/site.ymlの作成

さくらのナレッジ記事のとおりにやると、「sudo: true」の箇所でエラーが表示される。おそらくいまは実行できない設定と思われる。

$ vagrant provision
==> default: [vagrant-hostsupdater] Checking for host entries
==> default: Running provisioner: ansible...
Vagrant has automatically selected the compatibility mode '2.0'
according to the Ansible version installed (2.9.3).

Alternatively, the compatibility mode can be specified in your Vagrantfile:
https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode

    default: Running ansible-playbook...
ERROR! 'sudo' is not a valid attribute for a Play

The error appears to be in '/Users/user/Desktop/tmp/ansible_vagrant/provisioning/site.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- hosts: vagrants
  ^ here

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

sudoユーザーとして実行しないとパッケージのインストールなどができないため、設定自体は必要になる。そこで「become: yes」の設定に書き換えると通るようになる。

[Vagrantfile]

---
- hosts: vagrants
  become: yes
  user: vagrant
  tasks:
    - name: install packages zsh
      apt: name=zsh update_cache=yes

vagrant up

$ vagrant provision を実行すると、いままでの設定でうまくいっていることがわかる。もしエラーだった場合は、 failed=1 のように表示される。

補足:teratail - vagrant provisionのコマンドについて(プロビジョニングの実行タイミング)

$ vagrant provision
==> default: [vagrant-hostsupdater] Checking for host entries
==> default: Running provisioner: ansible...
Vagrant has automatically selected the compatibility mode '2.0'
according to the Ansible version installed (2.9.3).

Alternatively, the compatibility mode can be specified in your Vagrantfile:
https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode

    default: Running ansible-playbook...

PLAY [vagrants] ****************************************************************

TASK [Gathering Facts] *********************************************************
[WARNING]: Platform linux on host 192.168.33.10 is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/referen
ce_appendices/interpreter_discovery.html for more information.

ok: [192.168.33.10]

TASK [install packages zsh] ****************************************************
ok: [192.168.33.10]

PLAY RECAP *********************************************************************
192.168.33.10              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

rbenvとRubyのインストール

さくら記事だと、provisioning/site.ymlに記載されているRubyのバージョンが古くてサポート外になっているのか、エラーが表示される。(JSON部分は整形済み)

$ vagrant provision

・・・

TASK [install ruby 2.1.5] ******************************************************
fatal: [192.168.33.10]: FAILED! => {
    "changed": true,
    "cmd": "export RBENV_ROOT=/home/vagrant/.rbenv; export PATH=$RBENV_ROOT/bin:$PATH; echo N | rbenv install 2.1.5; rbenv global 2.1.5",
    "delta": "0:02:34.188692",
    "end": "2020-02-09 06:14:57.507708",
    "msg": "non-zero return code",
    "rc": 1,
    "start": "2020-02-09 06:12:23.319016",
    "stderr": "Downloading ruby-2.1.5.tar.bz2...\n-> https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.bz2\nInstalling ruby-2.1.5...\n\nWARNING: ruby-2.1.5 is past its end of life and is now unsupported.\nIt no longer receives bug fixes or critical security updates.\n\n\nBUILD FAILED (Debian 10 using ruby-build 20200115-8-g73b926b)\n\nInspect or clean up the working tree at /tmp/ruby-build.20200209061223.14454.8rtP6p\nResults logged to /tmp/ruby-build.20200209061223.14454.log\n\nLast 10 log lines:\n ^\nossl_pkey_dsa.c: In function ‘ossl_dsa_is_public’:\nossl_pkey_dsa.c:273:1: warning: control reaches end of non-void function [-Wreturn-type]\n }\n ^\nmake[2]: *** [Makefile:281: ossl_pkey_dsa.o] Error 1\nmake[2]: Leaving directory '/tmp/ruby-build.20200209061223.14454.8rtP6p/ruby-2.1.5/ext/openssl'\nmake[1]: *** [exts.mk:191: ext/openssl/all] Error 2\nmake[1]: Leaving directory '/tmp/ruby-build.20200209061223.14454.8rtP6p/ruby-2.1.5'\nmake: *** [uncommon.mk:180: build-ext] Error 2\nrbenv: version `2.1.5' not installed",
    "stderr_lines": [
        "Downloading ruby-2.1.5.tar.bz2...",
        "-> https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.bz2",
        "Installing ruby-2.1.5...",
        "",
        "WARNING: ruby-2.1.5 is past its end of life and is now unsupported.",
        "It no longer receives bug fixes or critical security updates.",
        "",
        "",
        "BUILD FAILED (Debian 10 using ruby-build 20200115-8-g73b926b)",
        "",
        "Inspect or clean up the working tree at /tmp/ruby-build.20200209061223.14454.8rtP6p",
        "Results logged to /tmp/ruby-build.20200209061223.14454.log",
        "",
        "Last 10 log lines:",
        " ^",
        "ossl_pkey_dsa.c: In function ‘ossl_dsa_is_public’:",
        "ossl_pkey_dsa.c:273:1: warning: control reaches end of non-void function [-Wreturn-type]",
        " }",
        " ^",
        "make[2]: *** [Makefile:281: ossl_pkey_dsa.o] Error 1",
        "make[2]: Leaving directory '/tmp/ruby-build.20200209061223.14454.8rtP6p/ruby-2.1.5/ext/openssl'",
        "make[1]: *** [exts.mk:191: ext/openssl/all] Error 2",
        "make[1]: Leaving directory '/tmp/ruby-build.20200209061223.14454.8rtP6p/ruby-2.1.5'",
        "make: *** [uncommon.mk:180: build-ext] Error 2",
        "rbenv: version `2.1.5' not installed"
    ],
    "stdout": "",
    "stdout_lines": []
}

PLAY RECAP *********************************************************************
192.168.33.10              : ok=25   changed=18   unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

Ruby公式サイト より、Rubyの最新バージョンを確認して、そのバージョンに書き換えたら通った。

[provisioning/site.yml]

- hosts: vagrants
  become: yes
  user: vagrant
  vars:
    vagrant_home: "/home/vagrant" 
  tasks:
    - name: install packages build-essential
      apt: name=build-essential update_cache=yes

    - name: install packages git  
      apt: name=git update_cache=yes

    - name: install packages git-core
      apt: name=git-core update_cache=yes

    - name: install packages libssl-dev
      apt: name=libssl-dev update_cache=yes

    - name: install packages libqt4-dev
      apt: name=libqt4-dev update_cache=yes

    - name: install packages libc6-dev
      apt: name=libc6-dev update_cache=yes

    - name: install packages automake
      apt: name=automake update_cache=yes

    - name: install packages libtool
      apt: name=libtool update_cache=yes

    - name: install packages libyaml-dev
      apt: name=libyaml-dev update_cache=yes

    - name: install packages zlib1g
      apt: name=zlib1g update_cache=yes

    - name: install packages zlib1g-dev
      apt: name=zlib1g-dev update_cache=yes

    - name: install packages openssl
      apt: name=openssl update_cache=yes

    - name: install packages libssl-dev
      apt: name=libssl-dev update_cache=yes

    - name: install packages libreadline-dev
      apt: name=libreadline-dev update_cache=yes

    - name: install packages libxml2-dev
      apt: name=libxml2-dev update_cache=yes

    - name: install packages libxslt1-dev
      apt: name=libxslt1-dev update_cache=yes

    - name: install packages libncurses5-dev
      apt: name=libncurses5-dev update_cache=yes

    - name: install packages pkg-config
      apt: name=pkg-config update_cache=yes

    - name: install packages chrpath
      apt: name=chrpath update_cache=yes

    - name: install packages libfontconfig1-dev
      apt: name=libfontconfig1-dev update_cache=yes

    - name: install packages libxft-dev
      apt: name=libxft-dev update_cache=yes

    - name: install rbenv
      git: repo=https://github.com/sstephenson/rbenv.git
           dest={{ vagrant_home }}/.rbenv
           version=master

    - name: add rbenv path to bash_profile
      copy: src=files/rbenv.sh dest=/etc/profile.d/rbenv.sh
      # copy: content="export PATH=\"/home/vagrant/.rbenv/bin:$PATH\"\neval \"$(rbenv init -)\"" dest=/etc/profile.d/rbenv.sh

    - name: install ruby_build
      git: repo=https://github.com/sstephenson/ruby-build.git
           dest={{ vagrant_home }}/.rbenv/plugins/ruby-build
           version=master

    - name: install ruby 2.7.0
      shell: "export RBENV_ROOT={{ vagrant_home }}/.rbenv; export PATH=$RBENV_ROOT/bin:$PATH; echo N | rbenv install 2.7.0; rbenv global 2.7.0"
      args:
        creates: "{{ vagrant_home }}/.rbenv/versions/2.7.0/"

    - name: install bundler
      shell: "{{ vagrant_home }}/.rbenv/shims/gem install bundler"
      args:
        creates: "{{ vagrant_home }}/.rbenv/shims/bundle"

    - name: change ~/.rbenv owner to vagrant
      file: path={{ vagrant_home }}/.rbenv state=directory owner=vagrant group=vagrant recurse=yes

設定したら反映。

$ vagrant provision

・・・

PLAY RECAP *********************************************************************
192.168.33.10              : ok=28   changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$ vagrant ssh
Linux debian-10 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64

This system is built by the Bento project by Chef Software
More information can be found at https://github.com/chef/bento

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Feb  9 11:16:28 2020 from 10.0.2.2

vagrant@debian-10:~$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

参考サイト