この記事は約15分44秒で読むことができます。

vagrant ssh -cでコマンド実行できるんだねって話

Vagrantfile

ここらへんもホスト名違いだけだし、うまくすっきり出来そうだけど、今はこれでよしとして楽しみにとっておこう。やり方はあるとおもうんだよな。

コード表示

[oracle@centos vx]$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.synced_folder '.', '/mnt', type: 'rsync'
  config.vm.synced_folder '.', '/vagrant', disabled: true
  config.vm.define "node1" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node1"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node2" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node2"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node3" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node3"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node4" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node4"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node5" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node5"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node6" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node6"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
end

vagrantツールでlibvirt管理のkvm上の仮想マシン立ち上げ!

コード表示

[oracle@centos vx]$ time vagrant up
real	0m54.930s
user	0m8.535s
sys	0m0.917s
[oracle@centos vx]$ vagrant status
Current machine states:

node1                     running (libvirt)
node2                     running (libvirt)
node3                     running (libvirt)
node4                     running (libvirt)
node5                     running (libvirt)
node6                     running (libvirt)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

vagrant ssh-configによるip確認

dhcpサービスによる動的ip配布なので。

コード表示

[oracle@centos vx]$ vagrant ssh-config | grep -E "^Host|\s{1,}Host"
Host node1
  HostName 192.168.121.233
Host node2
  HostName 192.168.121.193
Host node3
  HostName 192.168.121.18
Host node4
  HostName 192.168.121.17
Host node5
  HostName 192.168.121.227
Host node6
  HostName 192.168.121.98

vagrant ssh -cによるip確認

bashコマンドってパイプ渡しがいいぽいね。echoしてどのノードのipなのか確認しつつ、疎通に1秒以上かかるから、5秒待って次ノードって感じ。dummy_onelineファイルでawkによる行単位処理ができるように工夫。

コード表示

[oracle@centos vx]$ cat dummy_oneline
dummy_oneline
[oracle@centos vx]$ while read line;do echo ${line};sleep 5; echo ${line}|bash;done < <(seq 6 | xargs -I@ bash -c "awk '{print \"vagrant ssh node@ -c \"\"\x5c\x27\"\"ip a show eth0\"\"\x5c\x27\"}' dummy_oneline")
vagrant ssh node1 -c 'ip a show eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:5d:4a:3f brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.233/24 brd 192.168.121.255 scope global noprefixroute dynamic eth0
       valid_lft 3435sec preferred_lft 3435sec
    inet6 fe80::5054:ff:fe5d:4a3f/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node2 -c 'ip a show eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:dc:5b:1b brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.193/24 brd 192.168.121.255 scope global noprefixroute dynamic eth0
       valid_lft 3232sec preferred_lft 3232sec
    inet6 fe80::5054:ff:fedc:5b1b/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node3 -c 'ip a show eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:77:33:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.18/24 brd 192.168.121.255 scope global noprefixroute dynamic eth0
       valid_lft 3281sec preferred_lft 3281sec
    inet6 fe80::5054:ff:fe77:3399/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node4 -c 'ip a show eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:61:72:67 brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.17/24 brd 192.168.121.255 scope global noprefixroute dynamic eth0
       valid_lft 3570sec preferred_lft 3570sec
    inet6 fe80::5054:ff:fe61:7267/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node5 -c 'ip a show eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:88:c2:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.227/24 brd 192.168.121.255 scope global noprefixroute dynamic eth0
       valid_lft 3540sec preferred_lft 3540sec
    inet6 fe80::5054:ff:fe88:c254/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node6 -c 'ip a show eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:a8:bc:df brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.98/24 brd 192.168.121.255 scope global noprefixroute dynamic eth0
       valid_lft 2150sec preferred_lft 2150sec
    inet6 fe80::5054:ff:fea8:bcdf/64 scope link 
       valid_lft forever preferred_lft forever

Leave a Reply

Your email address will not be published. Required fields are marked *