2
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 1 year has passed since last update.

ansible+vagrantで「ソースコードで体感するネットワークの仕組み」のIaCでの環境構築

Last updated at Posted at 2022-06-14

「ソースコードで体感するネットワークの仕組み」の環境構築をぽちぽちするよりIaCで管理したかったのでそのメモです。

書籍はこちらです。ソースコードなどもこちらからダウンロードできます。

環境

  • Intel Macbook Pro 2019 16 inch
  • MacOS Big Sur

Ansible, Vagrantなどが事前に入っているようにしてください。

ファイル構成

├── Vagrantfile
└── provisioning
    ├── ansible.yml
    └── hosts

それぞれのファイル内容

Vagrantfileは以下です。

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

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/ansible.yml"
    ansible.inventory_path = "provisioning/hosts"
    ansible.limit = 'all'
  end
end

ansible.ymlではgcc,gitをインストールするようにしておきます。

provisioning/ansible.yml
- name: my ansible start
  hosts: vagrant

  tasks:
  # Note: これ実行すると初回は時間がかかります
  - name: upgrade all packages
    yum:
      name: '*'
      state: latest
  - name: Install a list of packages
    yum:
      name:
        - gcc
        - git
      state: installed

以下の設定で少し手こずりました。まずvagrant ssh-configコマンドを走らせます。

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile [YOUR PATH]
  IdentitiesOnly yes
  LogLevel FATAL

ここで Port 2222と[YOUR PATH]のところを以下のhostsに書きます。他はコピペでOKです。

[vagrant]
127.0.0.1
[vagrant:vars]
ansible_ssh_private_key_file=[YOUR PATH]
ansible_become=yes
ansible_user=vagrant
ansible_become_method=su
ansible_become_user=root
ansible_become_password=vagrant
ansible_port=2222

立ち上げ

これでルートディレクトリで立ち上げます。

$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> 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...
...
PLAY [my ansible start] ********************************************************

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

TASK [upgrade all packages] ****************************************************
changed: [127.0.0.1]

TASK [Install a list of packages] **********************************************
changed: [127.0.0.1]

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

ssh するには以下でいけます。

$ vagrant ssh

走らせる時

コードをクローンなり、scpで移行できたと仮定します。

自分は第二章終了までコードの写経が終わっているので、第二章終了までのコードを前提にpingを投げるところのデモを行います。

以下のようにeth0が出てきます。

$ ip addr
2: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 86358sec preferred_lft 86358sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link
       valid_lft forever preferred_lft forever

MyEth.iniのデバイスを書き換えます。

MyEth.ini
IP-TTL=64
device=eth0
vmac=02:00:00:00:00:01
vip=192.168.111.100
vmask=255.255.255.0
gateway=192.168.111.2

必要によってはsudoをつけないといけないかもしれませんが、以下のように走らせることができます。

$ sudo ./MyEth
IP-TTL=64
MTU=1500
device=eth0
++++++++++++++++++
UPBroadcastPROMISC
mtu=1500
myip TODOmymac TODO++++++++++++++++++
vmac=02:00:00:00:00:01
vip=192.168.111.100
vmask=255.255.255.0
gateway=192.168.111.2
===ARP===
---ether_header---
ether_dhost=ff:ff:ff:ff:ff:ff
ether_shost=02:00:00:00:00:01
ether_type=806Address resolution
ether_arp
arp_hrd=1Ethernet 10/100Mbps.,arp_pro=2048IP
arp_hln=6arp_pln=4arp_op=1ARP request
arp_sha=00:00:00:00:00:00
arp_spa=0.0.0.0
arp_tha=00:00:00:00:00:00
arp_tpa=192.168.111.100
]
===ARP===
---ether_header---
ether_dhost=ff:ff:ff:ff:ff:ff
ether_shost=02:00:00:00:00:01
ether_type=806Address resolution
ether_arp
arp_hrd=1Ethernet 10/100Mbps.,arp_pro=2048IP
arp_hln=6arp_pln=4arp_op=1ARP request
arp_sha=00:00:00:00:00:00
arp_spa=0.0.0.0
arp_tha=00:00:00:00:00:00
arp_tpa=192.168.111.100
]
===ARP===
---ether_header---
ether_dhost=ff:ff:ff:ff:ff:ff
ether_shost=02:00:00:00:00:01
ether_type=806Address resolution
ether_arp
arp_hrd=1Ethernet 10/100Mbps.,arp_pro=2048IP
arp_hln=6arp_pln=4arp_op=1ARP request
arp_sha=00:00:00:00:00:00
arp_spa=0.0.0.0
arp_tha=00:00:00:00:00:00
arp_tpa=192.168.111.100
]
===ARP===
---ether_header---
ether_dhost=ff:ff:ff:ff:ff:ff
ether_shost=02:00:00:00:00:01
ether_type=806Address resolution
ether_arp
arp_hrd=1Ethernet 10/100Mbps.,arp_pro=2048IP
arp_hln=6arp_pln=4arp_op=1ARP request
arp_sha=00:00:00:00:00:00
arp_spa=0.0.0.0
arp_tha=00:00:00:00:00:00
arp_tpa=192.168.111.100
]
2
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
2
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?