0
2

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.

Vagrant で Ansible 使ってみた

Last updated at Posted at 2020-01-11

はじめに

業務で Ansible を使うことになったため、調べたことなどを備忘録的にまとめます

環境構築

Vagrant + Virtual Box で、VM を 2 つ用意

VM controller node
用途 Ansible をインストール 管理対象
OS CentOS 7 Ubuntu 18.04
IP 192.168.100.10 192.168.100.20

controller

$ mkdir ansible
$ cd ansible
$ mkdir data
$ vagrant init 
$ vi Vagrantfile

以下のように記述

Vagrant.configure("2") do |config|
  config.vm.define "controller" do |controller|
    controller.vm.box = "bento/ubuntu-18.04"
    controller.vm.hostname = "controller"
    controller.vm.network "private_network", ip: "192.168.100.10", virtualbox__intnet: "mynetwork"
    controller.vm.synced_folder "./data", "/home/vagrant/data"
  end
  config.vm.define "node" do |node|
    node.vm.box = "centos/7"
    node.vm.hostname = "node"
    node.vm.network "private_network", ip: "192.168.100.20", virtualbox__intnet: "mynetwork"
  end
end

VM の SSH 接続情報は以下のコマンドで確認

$ vagrant ssh-config
Host controller
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/f/Vagrant/ansible_x/.vagrant/machines/controller/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

特に大事なのは、IdentityFile~ の行
ここに、秘密鍵が格納されているため、SSH 接続する際はその情報を利用
この秘密鍵をコピーして、VM の中におけば、controller から、node に SSH 接続可能

controller に接続する際は、以下のコマンドを実行

$ ssh -i /Users/f/Vagrant/ansible/controller/.vagrant/machines/default/virtualbox/private_key vagrant@192.168.100.10

[注意]
本来の Vagrant 的な SSH 接続は、vagrant ssh コマンド

接続が確認できたら、controller に Ansible をインストール

Ansible のインストール

[注意]
controller の VM で行う

公式のインストレーションガイド の通りにインストール

$ sudo apt -y update
$ sudo apt -y install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt -y install ansible

インストール確認

$ ansible --version
ansible 2.9.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.15+ (default, Oct  7 2019, 17:39:04) [GCC 7.4.0]

Ansible 実行

Ansible を使って、node に Apache をインストール & 起動してみる

以下のファイルを作成

$ tree
.
└── sample
    ├── inventory
    └── playbook.yml
inventory
[node]
192.168.1.200
ansible_ssh_private_key_file=~/.ssh/key/node_private_key

[注意]
node の秘密鍵を、~/.ssh/key/node_private_key600 で格納しておく

playbook.yml
- hosts: node
  become: yes
  tasks:
    - name: install package
      yum:
        name: httpd
        state: installed

    - name: start service
      systemd:
        name: httpd
        state: started
        enabled: yes

必要ファイルが準備できたら以下のコマンドを実行

$ ansible-playbook -i inventory playbook.yml

これで、node で Apache Web サーバが起動している
http://192.168.100.20
にブラウザからアクセスすると、welcome ページが表示される

おまけ

ssh 接続設定
これがあれば、inventory はホスト名だけ書けばいい

~/.ssh/config
Host node
User vagrant
HostName 192.168.100.20
IdentityFile ~/.ssh/private_key/node_private_key
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?