4
3

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.

MAC環境にAnsible導入後、AWSサービス構築までの道〜①Vagrant〜Ansibleインストールまで〜

Last updated at Posted at 2018-06-20

第1回目としてMacのvagrant上にCentOS7を導入し、Ansibleインストールまでを行う

やりたいこと

Ansibleを使用し、以下AWSサービスを構築するところまでを確認したい。
後々apacheの設定なども含め、total的に環境構築できることを目指す。
あくまでもlocal環境で実施した際のメモとして残す。

・Cloud Front
・Lambda
・API Gateway
・Cloud Search
・Route53

できたら
・WAF

準備する環境

  1. VirtualBox
  2. vagrant
  3. CentOS7

VirtualBoxインストール

  1. https://www.virtualbox.org/ よりDLし、インストールする。

Vagrantインストール

  1. https://www.vagrantup.com/downloads.html より「macOS」を選択肢、dmgをDL。
  2. DLしたdmgを実行し、インストールする。

CentOS7構築

「centos/7」という公式boxが公開されているため、今回はこれを使用する

① Vagrantアップデート

boxを作成しようとしたところ、すでにインストールしているVagrantのPluginが古く、エラーになったため、アップデートを実施。

$ vagrant box list
Vagrant failed to initialize at a very early stage:

The plugins failed to initialize correctly. This may be due to manual
modifications made within the Vagrant home directory. Vagrant can
attempt to automatically correct this issue by running:

  vagrant plugin repair

If Vagrant was recently updated, this error may be due to incompatible
versions of dependencies. To fix this problem please remove and re-install
all plugins. Vagrant can attempt to do this automatically by running:

  vagrant plugin expunge --reinstall

Or you may want to try updating the installed plugins to their latest
versions:

  vagrant plugin update

Error message given during initialization: Unable to resolve dependency: user requested 'vagrant-hostsupdater (> 0)'
$ vagrant plugin update
Updating installed plugins...
Fetching: vagrant-hostsupdater-1.1.1.160.gem (100%)
Updated 'vagrant-hostsupdater' to version '1.1.1.160'!
$ 

② box確認

現在のVagrant上のboxを確認する。

$ vagrant box list
xxxx (virtualbox, 0) ← すでに作成されているboxが表示される
$ 

③ box作成

centos/7のboxを作成する。

$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3 ← 今回「virtualbox」を使用しているため、3を選択。
==> box: Adding box 'centos/7' (v1804.02) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1804.02/providers/virtualbox.box
    box: Download redirected to host: cloud.centos.org
==> box: Successfully added box 'centos/7' (v1804.02) for 'virtualbox'!
$ 

④ box確認

作成したboxを確認する。

$ vagrant box list
centos/7 (virtualbox, 1804.02) ← addしたboxが作成されていること。
xxxx   (virtualbox, 0)
$ 

初期設定

事前準備

新規box作成用のディレクトリを事前に作成し、作成したディレクトリに移動する。

$ mkdir centos7
$ cd centos7

① init centos7

初期設定を行う際に「centos/7」を指定する。

$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ 

② Vagrantfileの確認

Vagrantfileが作成されていること。

$ ls -la
total 8
drwxr-xr-x  3 root  root   102  6 20 14:36 .
drwxr-xr-x  5 root  root   170  6 20 13:57 ..
-rw-r--r--  1 root  root  3015  6 20 14:36 Vagrantfile
$ 

② Vagrant状態確認

$ vagrant status
Current machine states:

default                   not created (virtualbox) ← まだ作成されていない状態

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.
$ 

③ 仮想マシン起動

「vagrant up」コマンドにて起動する。
結構「Retrying...」を繰り返す…

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_default_1529473806530_85010
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. 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: 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: [vagrant-hostsupdater] Checking for host entries
==> default: Rsyncing folder: /vagrant/centos7/ => /vagrant
$

④ Vagrant状態確認

起動(running)していることを確認する。

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
$ 

⑤ VirtualBoxコンソールでの確認

VirtualBox.appを起動し、「centos7」が実行中であることを確認する。

スクリーンショット 2018-06-20 17.32.28.png

⑥ 仮想マシンにログインし、OSのバージョンを確認する

$ vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release ← SSHできること
CentOS Linux release 7.5.1804 (Core) ← CentOS7になっていること
[vagrant@localhost ~]$

Ansibleインストール

yumにてインストールを進めるため、Pythonのバージョン確認、epel-releaseインストールなどAnsibleインストールする前の準備も行っていく。

① 動作環境

  1. Python(2.6+以上)
  2. Ansible

Python 2.6 or 2.7が必要ということみたいなので、バージョンを確認する

[vagrant@localhost ~]$ python --version
Python 2.7.5
[vagrant@localhost ~]$ 

② epel-releaseインストール

AnsibleはEPELリポジトリからインストールするので、EPELリポジトリが未インストールの場合はEPELリポジトリをインストールする。

[vagrant@localhost ~]$ sudo yum -y install epel-release
====途中結果は省略====
Complete!
[vagrant@localhost ~]$ 

③ Ansibleをインストール

[vagrant@localhost ~]$ sudo yum -y install ansible
====途中結果は省略====
Complete!
[vagrant@localhost ~]$ 

④ version確認

[vagrant@localhost ~]$ ansible --version
ansible 2.5.5
[vagrant@localhost ~]$ 

無事インストール完了。
次回はAnsibleの設定とAWSモジュールについて調べていく。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?