LoginSignup
3
5

More than 5 years have passed since last update.

ansibleの導入メモ

Last updated at Posted at 2018-05-08

環境

  • Vagrant
  • ubuntu 16

目的

  • 練習のため、ansibleが動作し簡単なサーバの設定ができることを確認
  • 基本的なルール・文法の整理
  • 一回目は環境の準備・動作確認まで

ゴール

  • サーバに定義した設定が導入されていること
  • ansibleの基本的な実行方法(次回)

手順

vagrant

  • Vagrantfaileを作成
  • ubuntu16を導入する
vagrant init bento/ubuntu-16.04
  • Vagrantfileを以下設定する
  • hostサーバとwebサーバ2台を用意する
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.define "host" do |node|
     node.vm.box = "bento/ubuntu-16.04"
     node.vm.hostname = "host"
     node.vm.network :private_network, ip: "192.168.10.10"
   end

   config.vm.define "web" do |node|
   node.vm.box = "bento/ubuntu-16.04"
     node.vm.hostname = "web"
     node.vm.network :private_network, ip: "192.168.10.11"
   end
end
  • この設定で、2台の仮想マシンを立ち上げることができる。
  • vagrant ssh ホスト名 で接続ができる
vagrant ssh host

ansibleのインストール

  • ホストに接続し、ansibleのインストールを行う
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
  • インストール完了を確認
vagrant@host:~$ ansible --version
ansible 2.5.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/vagrant/.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.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]

sshの設定

  • サーバ側(host)の設定
  • クライアント(web)側公開鍵の共有を行う
  • ~/に.sshディレクトリを作成し、以下で実行
vagrant@host:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FpWqMDarPPqpc++sHvKdshpZWJZgEn637L4EuJJ4NkI vagrant@host
The key's randomart image is:
+---[RSA 2048]----+
|+o        ..     |
|+. .     ..      |
| .+. .  ..       |
| +o B . ..       |
|.E.o B .S        |
|o+. + ..         |
|Oo=. o           |
|oB**o.           |
|+OBB*o.          |
+----[SHA256]-----+

vagrant@host:~/.ssh$ ssh-copy-id 192.168.10.11 <- <<<< クライアントのIPアドレス
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
The authenticity of host '192.168.10.11 (192.168.10.11)' can't be established.
ECDSA key fingerprint is SHA256:l0knxlx4jHjdf9URnGpjQzHAaTPDlRZTyQq7ARpdAP4.
Are you sure you want to continue connecting (yes/no)? yes  <-  <<<<yesを入力
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
vagrant@192.168.10.11's password: <- <<<<パスワードを入力

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.10.11'"
and check to make sure that only the key(s) you wanted were added.
  • ansibleのhostsファイルを設定
cd /etc/ansible
vim hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
192.168.10.11 <- <<<< クライアント(web)のIPを追記

  • クライアント側(web)にpythonを導入 
sudo apt-get install -y python-simplejson

サーバ・クライアント 疎通確認

  • ansible クライアントのIP -m ping コマンドで確認
vagrant@host:/etc/ansible$ ansible 192.168.10.11 -m ping
192.168.10.11 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

この辺りでエラーとなった場合、このようなメッセージが発生する
https://qiita.com/azuki19770922/items/ce101cad63f16702eff9#_reference-3003344051e9f97af6f8

ホスト側のhostsの追加設定

  • .sshディレクトリに
  • .ssh/config ファイルを追加
.ssh
 Host web
  HostName 192.168.10.11
  • この設定によって、ipアドレスを使わなくても
ssh web
  • でクライアント(web)側に接続できます
$ssh web
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-87-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.
Last login: Tue May  8 12:37:58 2018 from 192.168.10.10
  • インベントリファイルの作成
    • ansibleから操作する時、グループ単位で対象を指定することができるように、インベントリファイルを作成する
    • []でホストのグループ名を定義し、その下にipアドレスを指定していく
hosts
[web]                                                                     192.168.10.11
  • 確認

    • ansible 対象名(全てならall)
    • -iオプションでインベントリファイルを指定
    • -mオプションでansibleコマンドを実行
ansible web -i hosts -m ping

スクリーンショット 2018-06-01 20.55.07.png

  • ここまでで、環境は準備完了
  • 次回、Ansibleベストプラクティスに準拠した、基本的なルール実際の動作を確認

参考

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