takashi kono's blog

コーヒーとキーボードと共に何かを記録していくブログ

Vagrant up 時に Ansible で構成を実装させて、トライアンドエラー出来るようにしたい

Vagrant up 時に Ansible で構成を自動的に実装させたい

トライアンドエラーしたい

環境

環境メモ

takashi@takashi-desktop:~/vagrant/jiratest1$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
takashi@takashi-desktop:~/vagrant/jiratest1$ 
takashi@takashi-desktop:~/vagrant/jiratest1$ uname -a
Linux takashi-desktop 4.4.0-139-generic #165~14.04.1-Ubuntu SMP Wed Oct 31 10:55:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
takashi@takashi-desktop:~/vagrant/jiratest1$ 
takashi@takashi-desktop:~/vagrant/jiratest1$ python3 -V
Python 3.6.4
takashi@takashi-desktop:~/vagrant/jiratest1$ pip3 -V
pip 18.0 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
takashi@takashi-desktop:~/vagrant/jiratest1$ 

Ansible のインストール

Ansible をインストールする

takashi@takashi-desktop:~/vagrant/jiratest1$ sudo pip3 install ansible
[sudo] password for takashi: 
The directory '/home/takashi/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/takashi/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting ansible
  Downloading https://files.pythonhosted.org/packages/9e/df/b7ce359f9cc16864e0d5c9c93efe69f576fc437e74549bcc142f02cd4216/ansible-2.7.4.tar.gz (11.8MB)
    100% |████████████████████████████████| 11.8MB 3.6MB/s 
Requirement already satisfied: jinja2 in /usr/local/lib/python3.6/site-packages (from ansible) (2.10)
Collecting PyYAML (from ansible)
  Downloading https://files.pythonhosted.org/packages/9e/a3/1d13970c3f36777c583f136c136f804d70f500168edc1edea6daa7200769/PyYAML-3.13.tar.gz (270kB)
    100% |████████████████████████████████| 276kB 15.9MB/s 
Collecting paramiko (from ansible)
  Downloading https://files.pythonhosted.org/packages/cf/ae/94e70d49044ccc234bfdba20114fa947d7ba6eb68a2e452d89b920e62227/paramiko-2.4.2-py2.py3-none-any.whl (193kB)
    100% |████████████████████████████████| 194kB 9.2MB/s 
Requirement already satisfied: cryptography in /home/takashi/.local/lib/python3.6/site-packages (from ansible) (2.3.1)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/site-packages (from ansible) (28.8.0)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/site-packages (from jinja2->ansible) (1.0)
Collecting pyasn1>=0.1.7 (from paramiko->ansible)
  Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB)
    100% |████████████████████████████████| 81kB 4.8MB/s 
Collecting pynacl>=1.0.1 (from paramiko->ansible)
  Downloading https://files.pythonhosted.org/packages/27/15/2cd0a203f318c2240b42cd9dd13c931ddd61067809fee3479f44f086103e/PyNaCl-1.3.0-cp34-abi3-manylinux1_x86_64.whl (759kB)
    100% |████████████████████████████████| 768kB 4.4MB/s 
Collecting bcrypt>=3.1.3 (from paramiko->ansible)
  Downloading https://files.pythonhosted.org/packages/b8/09/905ec939994e2c49dcffff72f823802557f166b3815ea54c1db3671eed42/bcrypt-3.1.4-cp36-cp36m-manylinux1_x86_64.whl (54kB)
    100% |████████████████████████████████| 61kB 11.8MB/s 
Requirement already satisfied: asn1crypto>=0.21.0 in /home/takashi/.local/lib/python3.6/site-packages (from cryptography->ansible) (0.24.0)
Requirement already satisfied: cffi!=1.11.3,>=1.7 in /home/takashi/.local/lib/python3.6/site-packages (from cryptography->ansible) (1.11.5)
Requirement already satisfied: idna>=2.1 in /usr/local/lib/python3.6/site-packages (from cryptography->ansible) (2.6)
Requirement already satisfied: six>=1.4.1 in /usr/local/lib/python3.6/site-packages (from cryptography->ansible) (1.11.0)
Requirement already satisfied: pycparser in /home/takashi/.local/lib/python3.6/site-packages (from cffi!=1.11.3,>=1.7->cryptography->ansible) (2.18)
Installing collected packages: PyYAML, pyasn1, pynacl, bcrypt, paramiko, ansible
  Running setup.py install for PyYAML ... done
  Running setup.py install for ansible ... done
Successfully installed PyYAML-3.13 ansible-2.7.4 bcrypt-3.1.4 paramiko-2.4.2 pyasn1-0.4.4 pynacl-1.3.0
takashi@takashi-desktop:~/vagrant/jiratest1$ 
takashi@takashi-desktop:~/vagrant/jiratest1$ 
takashi@takashi-desktop:~/vagrant/jiratest1$ ansible --version
ansible 2.7.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/takashi/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4]
takashi@takashi-desktop:~/vagrant/jiratest1$ 

良さそう

Vagrantfile の編集

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/6"
  config.vm.box_check_update = false

  # Network
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "public_network"

  # RAM setting
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end

  # Run Ansible from the Vagrant Host
  config.vm.provision "ansible" do |ansible|
      ansible.playbook = "playbook.yaml"
  end
end

playbook.yaml の作成

- hosts: all
  tasks:
    - name: ensure ntpd is at the latest version
      yum: pkg=ntp state=latest
      notify:
      - restart ntpd
  handlers:
    - name: restart ntpd
      service: name=ntpd state=restarted

vagrant up

失敗したけど、特に ssh の設定を行うこと無く、 playbook.yaml の内容が起動時に反映されることがわかった。

takashi@takashi-desktop:~/vagrant/ansible_test$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/6'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ansible_test_default_1544475292941_35884
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Available bridged network interfaces:
1) eth0
2) virbr0
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
    default: Which interface should the network bridge to? 1 
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
    default: Adapter 3: bridged
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
[default] No installation found.
Loaded plugins: fastestmirror, security
Setting up Install Process
No package kernel-devel-2.6.32-696.13.2.el6.x86_64 available.
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-144.el6.x86_64 already installed and latest version
Package bzip2-1.0.5-7.el6_0.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package binutils.x86_64 0:2.20.51.0.2-5.47.el6_9.1 will be updated
---> Package binutils.x86_64 0:2.20.51.0.2-5.48.el6 will be an update
---> Package gcc.x86_64 0:4.4.7-23.el6 will be installed
--> Processing Dependency: libgomp = 4.4.7-23.el6 for package: gcc-4.4.7-23.el6.x86_64
--> Processing Dependency: cpp = 4.4.7-23.el6 for package: gcc-4.4.7-23.el6.x86_64
--> Processing Dependency: libgcc >= 4.4.7-23.el6 for package: gcc-4.4.7-23.el6.x86_64
--> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.4.7-23.el6.x86_64
--> Processing Dependency: cloog-ppl >= 0.15 for package: gcc-4.4.7-23.el6.x86_64
--> Processing Dependency: libgomp.so.1()(64bit) for package: gcc-4.4.7-23.el6.x86_64
--> Running transaction check
---> Package cloog-ppl.x86_64 0:0.15.7-1.2.el6 will be installed
--> Processing Dependency: libppl_c.so.2()(64bit) for package: cloog-ppl-0.15.7-1.2.el6.x86_64
--> Processing Dependency: libppl.so.7()(64bit) for package: cloog-ppl-0.15.7-1.2.el6.x86_64
---> Package cpp.x86_64 0:4.4.7-23.el6 will be installed
--> Processing Dependency: libmpfr.so.1()(64bit) for package: cpp-4.4.7-23.el6.x86_64
---> Package glibc-devel.x86_64 0:2.12-1.212.el6 will be installed
--> Processing Dependency: glibc-headers = 2.12-1.212.el6 for package: glibc-devel-2.12-1.212.el6.x86_64
--> Processing Dependency: glibc = 2.12-1.212.el6 for package: glibc-devel-2.12-1.212.el6.x86_64
--> Processing Dependency: glibc-headers for package: glibc-devel-2.12-1.212.el6.x86_64
---> Package libgcc.x86_64 0:4.4.7-18.el6 will be updated
---> Package libgcc.x86_64 0:4.4.7-23.el6 will be an update
---> Package libgomp.x86_64 0:4.4.7-23.el6 will be installed
--> Running transaction check
---> Package glibc.x86_64 0:2.12-1.209.el6_9.2 will be updated
--> Processing Dependency: glibc = 2.12-1.209.el6_9.2 for package: glibc-common-2.12-1.209.el6_9.2.x86_64
---> Package glibc.x86_64 0:2.12-1.212.el6 will be an update
---> Package glibc-headers.x86_64 0:2.12-1.212.el6 will be installed
--> Processing Dependency: kernel-headers >= 2.2.1 for package: glibc-headers-2.12-1.212.el6.x86_64
--> Processing Dependency: kernel-headers for package: glibc-headers-2.12-1.212.el6.x86_64
---> Package mpfr.x86_64 0:2.4.1-6.el6 will be installed
---> Package ppl.x86_64 0:0.10.2-11.el6 will be installed
--> Running transaction check
---> Package glibc-common.x86_64 0:2.12-1.209.el6_9.2 will be updated
---> Package glibc-common.x86_64 0:2.12-1.212.el6 will be an update
---> Package kernel-headers.x86_64 0:2.6.32-754.9.1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch        Version                     Repository    Size
================================================================================
Installing:
 gcc                 x86_64      4.4.7-23.el6                base          10 M
Updating:
 binutils            x86_64      2.20.51.0.2-5.48.el6        base         2.8 M
Installing for dependencies:
 cloog-ppl           x86_64      0.15.7-1.2.el6              base          93 k
 cpp                 x86_64      4.4.7-23.el6                base         3.7 M
 glibc-devel         x86_64      2.12-1.212.el6              base         991 k
 glibc-headers       x86_64      2.12-1.212.el6              base         620 k
 kernel-headers      x86_64      2.6.32-754.9.1.el6          updates      4.5 M
 libgomp             x86_64      4.4.7-23.el6                base         135 k
 mpfr                x86_64      2.4.1-6.el6                 base         157 k
 ppl                 x86_64      0.10.2-11.el6               base         1.3 M
Updating for dependencies:
 glibc               x86_64      2.12-1.212.el6              base         3.8 M
 glibc-common        x86_64      2.12-1.212.el6              base          14 M
 libgcc              x86_64      4.4.7-23.el6                base         104 k

Transaction Summary
================================================================================
Install       9 Package(s)
Upgrade       4 Package(s)

Total download size: 43 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           7.6 MB/s |  43 MB     00:05     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-9.el6.12.3.x86_64 (@anaconda-CentOS-201703281317.x86_64/6.9)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating   : libgcc-4.4.7-23.el6.x86_64                                  1/17 
  Updating   : glibc-common-2.12-1.212.el6.x86_64                          2/17 
  Updating   : glibc-2.12-1.212.el6.x86_64                                 3/17 
  Installing : libgomp-4.4.7-23.el6.x86_64                                 4/17 
  Installing : mpfr-2.4.1-6.el6.x86_64                                     5/17 
  Installing : cpp-4.4.7-23.el6.x86_64                                     6/17 
  Updating   : binutils-2.20.51.0.2-5.48.el6.x86_64                        7/17 
  Installing : ppl-0.10.2-11.el6.x86_64                                    8/17 
  Installing : cloog-ppl-0.15.7-1.2.el6.x86_64                             9/17 
  Installing : kernel-headers-2.6.32-754.9.1.el6.x86_64                   10/17 
  Installing : glibc-headers-2.12-1.212.el6.x86_64                        11/17 
  Installing : glibc-devel-2.12-1.212.el6.x86_64                          12/17 
  Installing : gcc-4.4.7-23.el6.x86_64                                    13/17 
  Cleanup    : binutils-2.20.51.0.2-5.47.el6_9.1.x86_64                   14/17 
  Cleanup    : glibc-common-2.12-1.209.el6_9.2.x86_64                     15/17 
  Cleanup    : glibc-2.12-1.209.el6_9.2.x86_64                            16/17 
  Cleanup    : libgcc-4.4.7-18.el6.x86_64                                 17/17 
  Verifying  : libgomp-4.4.7-23.el6.x86_64                                 1/17 
  Verifying  : gcc-4.4.7-23.el6.x86_64                                     2/17 
  Verifying  : glibc-headers-2.12-1.212.el6.x86_64                         3/17 
  Verifying  : glibc-devel-2.12-1.212.el6.x86_64                           4/17 
  Verifying  : mpfr-2.4.1-6.el6.x86_64                                     5/17 
  Verifying  : cloog-ppl-0.15.7-1.2.el6.x86_64                             6/17 
  Verifying  : binutils-2.20.51.0.2-5.48.el6.x86_64                        7/17 
  Verifying  : glibc-2.12-1.212.el6.x86_64                                 8/17 
  Verifying  : kernel-headers-2.6.32-754.9.1.el6.x86_64                    9/17 
  Verifying  : glibc-common-2.12-1.212.el6.x86_64                         10/17 
  Verifying  : cpp-4.4.7-23.el6.x86_64                                    11/17 
  Verifying  : ppl-0.10.2-11.el6.x86_64                                   12/17 
  Verifying  : libgcc-4.4.7-23.el6.x86_64                                 13/17 
  Verifying  : binutils-2.20.51.0.2-5.47.el6_9.1.x86_64                   14/17 
  Verifying  : libgcc-4.4.7-18.el6.x86_64                                 15/17 
  Verifying  : glibc-2.12-1.209.el6_9.2.x86_64                            16/17 
  Verifying  : glibc-common-2.12-1.209.el6_9.2.x86_64                     17/17 

Installed:
  gcc.x86_64 0:4.4.7-23.el6                                                     

Dependency Installed:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6                                             
  cpp.x86_64 0:4.4.7-23.el6                                                     
  glibc-devel.x86_64 0:2.12-1.212.el6                                           
  glibc-headers.x86_64 0:2.12-1.212.el6                                         
  kernel-headers.x86_64 0:2.6.32-754.9.1.el6                                    
  libgomp.x86_64 0:4.4.7-23.el6                                                 
  mpfr.x86_64 0:2.4.1-6.el6                                                     
  ppl.x86_64 0:0.10.2-11.el6                                                    

Updated:
  binutils.x86_64 0:2.20.51.0.2-5.48.el6                                        

Dependency Updated:
  glibc.x86_64 0:2.12-1.212.el6       glibc-common.x86_64 0:2.12-1.212.el6      
  libgcc.x86_64 0:4.4.7-23.el6       

Complete!
Copy iso file /usr/share/virtualbox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.1.38 - guest version is unknown
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.38 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.
An error occurred during installation of VirtualBox Guest Additions 5.1.38. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
vboxadd.sh: Starting the VirtualBox Guest Additions.
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: modprobe vboxguest failed.
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: /home/takashi/vagrant/ansible_test/ => /vagrant
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [default]

TASK [ensure ntpd is at the latest version] ************************************
fatal: [default]: FAILED! => {"changed": true, "msg": "You need to be root to perform this command.\n", "rc": 1, "results": ["Loaded plugins: fastestmirror, security\n"]}
    to retry, use: --limit @/home/takashi/vagrant/ansible_test/playbook.retry

PLAY RECAP *********************************************************************
default                    : ok=1    changed=0    unreachable=0    failed=1   

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
takashi@takashi-desktop:~/vagrant/ansible_test$ 

残念ながらエラー。だけど、次に繋がるぞ。

今日は以上です。