通过ansible为每个流浪者vm设置/ etc / hosts

时间:2017-11-09 07:54:29

标签: deployment vagrant ansible vagrantfile ansible-2.x

我的Vagrantfile

hosts = {
    "host01" => "192.168.11.101",
    "host02" => "192.168.11.102",
}

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

    config.ssh.username = "root"
    config.ssh.password = "vagrant"
    config.ssh.insert_key = "true"

    hosts.each_with_index do |(name,ip),index|
        config.vm.define name do |machine|
            machine.vm.box = "centos7"
            machine.vm.box_check_update = false
            machine.vm.hostname = name


            machine.vm.synced_folder "/data", "/data"

            machine.vm.network :private_network, ip: ip

            machine.vm.provider "virtualbox" do |v|
                v.name = name
                v.customize ["modifyvm", :id, "--memory", 2048]
            end
        end
    end
end

ansible tamplate for generate / etc / host

127.0.0.1 localhost
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_' + iface].ipv4.address }}  {{ host }}
{% endfor %}

ansible task

- name: Create the hosts file for all machines
  template: src=hosts.j2 dest=/etc/hosts

但我得到了结果

[root@host01 ~]# cat /etc/hosts  
127.0.0.1 localhost              
10.0.2.15  host01                
10.0.2.15  host02                

的ifconfig

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                     
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0                   
        inet6 fe80::42:49ff:fed1:eebb  prefixlen 64  scopeid 0x20<link>           
        ether 02:42:49:d1:ee:bb  txqueuelen 0  (Ethernet)                         
        RX packets 77  bytes 6065 (5.9 KiB)                                       
        RX errors 0  dropped 0  overruns 0  frame 0                               
        TX packets 99  bytes 8572 (8.3 KiB)                                       
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0                

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                      
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255               
        inet6 fe80::a00:27ff:fede:e0e  prefixlen 64  scopeid 0x20<link>           
        ether 08:00:27:de:0e:0e  txqueuelen 1000  (Ethernet)                      
        RX packets 785483  bytes 57738892 (55.0 MiB)                              
        RX errors 0  dropped 0  overruns 0  frame 0                               
        TX packets 777457  bytes 1957320412 (1.8 GiB)                             
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0                

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                      
        inet 192.168.11.101  netmask 255.255.255.0  broadcast 192.168.11.255      
        ether 08:00:27:15:2c:64  txqueuelen 1000  (Ethernet)                      
        RX packets 41445  bytes 39878552 (38.0 MiB)                               
        RX errors 0  dropped 0  overruns 0  frame 0                               
        TX packets 18055  bytes 2113498 (2.0 MiB)                                 

我在ifconfig找到,在host01和host02上只有enp0s8 inet 192.168.11.102不同

host01和host02得到相同的ip ??

host01有一个docker注册表。

  • host01,curl http://host01:5006/v2/_catalog正常工作
  • host02,curl http://host01:5006/v2/_catalog无法正常工作

1 个答案:

答案 0 :(得分:1)

  

host01和host02得到相同的ip ??

是。这就是Vagrant的工作方式,并且能够将编排目的连接到由来自不同发布者的各种盒子创建的机器上。

没有什么奇怪的,它们连接到VirtualBox中的不同虚拟交换机。

  

我只是希望host01和host02可以通过主机名互相访问。

在Jinja2模板中使用其他界面作为iface的值(无论如何你没有在问题中显示它)。