LoginSignup
1
0

More than 1 year has passed since last update.

vagrant(win環境) + ansibleの備忘録

Posted at

概要

ローカル環境をいくつも作ったり壊したりしているので、作る作業を無くしたく。
かねてから気になっていたansibleを使ってみる。

環境

マシンなど

  • windows 8.1(早く買い替えたい…)
  • vagrant

使用するbox

  • bento/amazonlinux-2

やること

  • 仮想サーバを2台起動(masterとweb)
  • master→webへsshで繋げる。
    ・ansibleからhttpdをインストールしてブラウザから表示を確認する。

仮想サーバを2台起動する。

参考記事

↑の記事を参考にVagrantfileを編集

1. Vagrantfileの編集

Vagrantfaile
  config.vm.define :master do |server|
    server.vm.hostname = "master"
    server.vm.network "private_network", ip: "192.168.33.10"
  end

  config.vm.define :web do |server|
    server.vm.hostname = "web"
    server.vm.network "private_network", ip: "192.168.33.11"
  end

vm.boxは今回の趣旨に合わせてこちらを選択

Vagrantfile
config.vm.box = "bento/amazonlinux-2"

2. vagrantを起動

Terminal
vagrant up

master→webへssh接続

ansibleを使えるようになりたいのでとりあえず以下の記事を参考に必要そうなものをインストールさせる。

参考記事

1. 必要そうなものインストール

Terminal
sudo yum -y install python-devel openssl-devel gcc git
sudo amazon-linux-extras install ansible2

2. .ssh/configファイルにサーバ情報を追加

Terminal
vim ~/.ssh/config
vim
Host web
 HostName 192.168.33.11

3.鍵の作成

Terminal
ssh-keygen -t rsa

4.鍵の権限変更

Terminal
chmod 600 ~/.ssh/config

5.鍵のコピー

Terminal
ssh-copy-id web

6.ssh接続確認(password聞かれるのでvagrantのpasswordを使う)

Terminal
ssh web

接続できれば↓が出る。

Terminal
       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/

This system is built by the Bento project by Chef Software
More information can be found at https://github.com/chef/bento

ansibleでhttpdをインストールしてブラウザから表示

参考記事

1.ansible inventoryファイルの編集

Terminal
sudo vim /etc/ansible/hosts

一番下に追加

inventory
[client_node]
client_node01 ansible_ssh_host=192.168.33.11

[client_node:vars]
ansible_ssh_user=vagrant
ansible_ssh_private_key_file=/home/vagrant/.ssh/id_rsa

2.ansible hostsファイルの編集

[defaults]の下に追加

hosts
inventory = /etc/ansible/hosts

3.playbookの作成

playbook
- hosts: client_node
  become: yes
  tasks:
    - name: install httpd
      yum: name=httpd state=latest
    - name: apache start / enable
      service: name=httpd state=started enabled=yes
  handlers:
    - name: httpd restart
      service: name=httpd state=restarted

4.ansibleの実行

Terminal
ansible-playbook playbook.yml

なんかWARNING言われているけど…ワカチコの精神で気にしないようにして
結果だけを見ると、完了したっぽいのでブラウザで確認する。

Terminal
[vagrant@master ansible]$ ansible-playbook playbook.yml

PLAY [client_node] *****************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
[WARNING]: Platform linux on host client_node01 is using the discovered Python interpreter at /usr/bin/python, but
future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [client_node01]

TASK [install httpd] ***************************************************************************************************
changed: [client_node01]

TASK [apache start / enable] *******************************************************************************************
changed: [client_node01]

PLAY RECAP *************************************************************************************************************
client_node01              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

結果

image.png

1
0
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
1
0