VirtualBOX+Vagrant+Ansibleで開発環境を作る その1
こちらの記事を参考にAnsibleを動かしてみたいと思います。
↑の記事は仮想サーバを2本立てて一方をcontroller、もう一方をtargetとしていますが、今回はtargetだけ仮想でたてて、Macをコントローラとして動かしたいと思います。
事前準備
- Vagrant 2.0.3
- VirtualBox 5.2.8r121009
- ansible 2.5.0
VagrantとVirtualBoxはサイトからdmgをDL/ansibleはbrew installしました。 ansibleの導入にはpython 2.6+なのですが、私のmacにはPython 2.7.10が入ってたのでそのまま進めます。
仮想サーバtargetを作成します。
% mkdir vagrant % cd vagrant % vagrant init
vagrantfileが生成されるので、targetサーバの情報を書き込みます。OSはCentOS7にしました。
Vagrant.configure(2) do |config| config.vm.define "target" do |node| node.vm.box = "centos/7" node.vm.hostname = "target" node.vm.network :private_network, ip: "192.168.100.20" node.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2220 end end
起動します。
% vagrant up
これだけ。めっちゃ早い。 sshで疎通を確認しますが、まあ通りません。認証エラーです。 Vagrant 1.7以降、vagrant up 時に旧来の insecure_key を検知した場合、その仮想マシン用の別の鍵ペアを生成して insecure_key を置き換える仕様になったそうです。
Windowsでvagrant upした後にsshログインする方法 · DQNEO起業日記
こちらの記事の通り、sshログイン情報は"vagrant ssh-config"コマンドを叩くと教えてくれるので、設定された情報を元にsshが通ることを確認します。
ansible疎通
対象ホストをまとめて記述したinventoryファイルを作り、ハンズオンのPlaybookを試してみます。 SSHはssh configを参照するように設定したので、inventory/hosts にはssh configで設定したホスト名で記述します。 試してみる、のtest.ymlの中身もuserを書き換えます。
■ssh config
Host vag.tst Hostname 192.168.100.20 User vagrant IdentityFile ~/workspace/vagrant/.vagrant/machines/target/virtualbox/private_key
■inventory/hosts
[targets] vag.tst
■test.yml
- hosts: targets user: vagrant ← tasks: - name: output message. debug: msg="{{ message }}" - name: output fruits debug: msg="We want {{ item.value.amount }} {{ item.key }} !" with_dict: "{{ fruits }}"
Ansibleをもう少し試す
ハンズオン通り下記のPlaybook(main.yml)を実行してみます。( jqだけEPELリポジトリを追加してそちらから取得しないといけないのでいったん削除)
- hosts: targets user: vagrant tasks: - name: install packages from yum yum: name={{ item }} state=latest with_items: - ruby - httpd - name: register cron job cron: name="check ping" day="*/2" hour="12" minute="0" job="ping -c 3 192.168.100.10" - name: create directories file: path={{ item.path }} owner={{ item.owner }} group={{ item.group }} mode=0{{ item.mode }} state=directory with_items: - { "path":"/opt/ansible", "owner":"root", "group":"root", "mode":"755" } - { "path":"/opt/vagrant", "owner":"vagrant", "group":"vagrant", "mode":"755" } - name: copy files copy: src=./files/hoge dest=/opt/ansible/hoge owner=root group=root mode=0755 - name: copy template files template: src=./templates/fuga.j2 dest=/opt/ansible/fuga owner=root group=root mode=0755
↓結果↓
% ansible-playbook -i inventory/hosts main.yml PLAY [targets] ********************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [vag.tst] TASK [install packages from yum] *************************************************************** failed: [vag.tst] (item=[u'ruby', u'httpd']) => {"changed": true, "item": ["ruby", "httpd"], "msg": "You need to be root to perform this command.\n", "rc": 1, "results": ["Loaded plugins: fastestmirror\n"]} to retry, use: --limit @/Users/username/workspace/ansible/main.retry PLAY RECAP ************************************************************************************* vag.tst : ok=1 changed=0 unreachable=0 failed=1
yumを実行するためにルートユーザにならないとダメそうです。Playbook(main.yml)を修正
- hosts: targets user: vagrant become: yes ←タスクすべてをsudoで実行する tasks: (後略)
↓結果↓
% ansible-playbook -i inventory/hosts main.yml PLAY [targets] ********************************************************************************* TASK [Gathering Facts] ************************************************************************* ok: [vag.tst] TASK [install packages from yum] *************************************************************** changed: [vag.tst] => (item=[u'ruby', u'httpd']) TASK [register cron job] *********************************************************************** changed: [vag.tst] TASK [create directories] ********************************************************************** changed: [vag.tst] => (item={u'owner': u'root', u'path': u'/opt/ansible', u'group': u'root', u'mode': u'755'}) changed: [vag.tst] => (item={u'owner': u'vagrant', u'path': u'/opt/vagrant', u'group': u'vagrant', u'mode': u'755'}) TASK [copy files] ****************************************************************************** changed: [vag.tst] TASK [copy template files] ********************************************************************* changed: [vag.tst] PLAY RECAP ************************************************************************************* vag.tst : ok=6 changed=5 unreachable=0 failed=0
成功しました!タスクごとにbecomeを設定することもできるらしいので開発環境作るときはもうちょっとちゃんとやる。
[vagrant@target ~]$ ruby -v ruby 2.0.0p648 (2015-12-16) [x86_64-linux] [vagrant@target ~]$ httpd -v Server version: Apache/2.4.6 (CentOS) Server built: Oct 19 2017 20:39:16 [vagrant@target ~]$ sudo yum history info 2 Loaded plugins: fastestmirror Transaction ID : 2 Begin time : Fri Apr 6 07:12:11 2018 Begin rpmdb : 303:0b438f896414e3740ba8650a5ae32eb0794477ba End time : 07:12:14 2018 (3 seconds) End rpmdb : 318:c8dd1f6f94f41a558ed062c2fc3ce0f4d0411ec9 User : vagrant <vagrant> Return-Code : Success Command Line : -d 2 -y install ruby httpd Transaction performed with: Installed rpm-4.11.3-25.el7.x86_64 @anaconda Installed yum-3.4.3-154.el7.centos.1.noarch @koji-override-1 Installed yum-plugin-fastestmirror-1.1.31-42.el7.noarch @anaconda Packages Altered: Dep-Install apr-1.4.8-3.el7_4.1.x86_64 @updates Dep-Install apr-util-1.5.2-6.el7.x86_64 @base Install httpd-2.4.6-67.el7.centos.6.x86_64 @updates Dep-Install httpd-tools-2.4.6-67.el7.centos.6.x86_64 @updates Dep-Install libyaml-0.1.4-11.el7_0.x86_64 @base Dep-Install mailcap-2.1.41-2.el7.noarch @base Install ruby-2.0.0.648-33.el7_4.x86_64 @updates Dep-Install ruby-irb-2.0.0.648-33.el7_4.noarch @updates Dep-Install ruby-libs-2.0.0.648-33.el7_4.x86_64 @updates Dep-Install rubygem-bigdecimal-1.2.0-33.el7_4.x86_64 @updates Dep-Install rubygem-io-console-0.4.2-33.el7_4.x86_64 @updates Dep-Install rubygem-json-1.7.7-33.el7_4.x86_64 @updates Dep-Install rubygem-psych-2.0.0-33.el7_4.x86_64 @updates Dep-Install rubygem-rdoc-4.0.0-33.el7_4.noarch @updates Dep-Install rubygems-2.0.14.1-33.el7_4.noarch @updates history info