vagrant ansible访问其他机器IP地址

vagrant ansible access other machines IP address

我有一个用于创建 3 个虚拟机的 vagrant 文件和一个用于管理这 3 台机器的 ansible(库存文件由 vagrant 文件生成)。我需要访问 VM 1 号 IP 地址,以便将其放入其他两台机器的配置文件中。但是使用 hostvars[vm1] 变量不会给我 vm1 的 IP 地址。

这是我的 vagrant 文件:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", type: "dhcp"
  config.vm.provider "virtualbox" do |v|
    v.memory = 512
  end
  config.vm.synced_folder ".", "/vagrant"
  config.vm.box_check_update = false

  config.vm.define "vm1" do |vm1|
    vm1.vm.box = "hashicorp/bionic64"
  end

  config.vm.define "vm2" do |vm2|
    vm2.vm.box = "hashicorp/bionic64"
  end

  config.vm.define "vm3" do |vm3|
    vm3.vm.box = "hashicorp/bionic64"
  end

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    #ansible.ask_become_pass = true
    ansible.groups = {
      "node_master" => ["vm1"],
      "node_replicas" => ["vm2", "vm3"],
      "node:children" => ["node_master", "node_replicas"]
    }
  end

我该如何解决这个问题?

我不知道如何解决你的特定版本。

不过,我分别使用Vagrant和Ansible。 Vagrant 仅使用 vagrant up 构建主机,并使用 Ansible 管理这些主机上的配置。

我使用这个 Vagrantfile:

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

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

  config.vm.provider "virtualbox" do |v|
    v.memory = 2048
    v.cpus = 1
  end

config.ssh.insert_key = false

config.vm.define "multi-1" do | localMulti1 |
  localMulti1.vm.box = "ubuntu/xenial64"
  localMulti1.vm.hostname = "multi-1"
  localMulti1.vm.network :forwarded_port, guest: 22, host: 30001, id: "ssh"
  localMulti1.vm.network "private_network", ip: "10.0.0.111"
         end

  config.vm.define "multi-2" do | localMulti2 |
    localMulti2.vm.box = "ubuntu/xenial64"
    localMulti2.vm.hostname = "multi-2"
    localMulti2.vm.network :forwarded_port, guest: 22, host: 30002, id: "ssh"
    localMulti2.vm.network "private_network", ip: "10.0.0.112"
  end

  config.vm.define "multi-3" do | localMulti3 |
    localMulti3.vm.box = "ubuntu/xenial64"
    localMulti3.vm.hostname = "multi-3"
    localMulti3.vm.network :forwarded_port, guest: 22, host: 30003, id: "ssh"
    localMulti3.vm.network "private_network", ip: "10.0.0.113"
    localMulti3.vm.provision "ansible" do |ansible|
      ansible.playbook = "playbook.yml"
      ansible.inventory_path = "inventory"
      ansible.limit = "local_multi"
              end
           end
end

我将其放入我的库存文件中:

[local_multi]
multi-1 ansible_ssh_user=vagrant ansible_host=127.0.0.1 ansible_ssh_port=30001 ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
multi-2 ansible_ssh_user=vagrant ansible_host=127.0.0.1 ansible_ssh_port=30002 ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
multi-3 ansible_ssh_user=vagrant ansible_host=127.0.0.1 ansible_ssh_port=30003 ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

你的playbook.yml

---
- hosts: local_multi
  become: True
  tasks:
    - name: check who is master
      debug: 
         msg: "{{ node_master }}"
      when: node_master is defined

现在您可以将所有 Ansible 变量放在分组或主机变量中;

./inventory
./playbook.yml
./Vagrantfile
./group_vars/all.yml
./host_vars/multi-1.yml
./host_vars/multi-2.yml
./host_vars/multi-3.yml

根据配置,您的 ansible provisionner 将 运行 三次:每台机器独立一次,被调用时限制设置为当前机器名称。

在这种情况下,所有其他机器的事实将不会收集到您的剧本中,并且 hostvars[vm1] 将为空(除非您当前 运行ning 在 vm1 上)。

您可以尝试仅在一台机器上声明配置器(最好的选择是 vm3,最后一个)并将默认的当前机器限制更改为 all

config.vm.define "vm3" do |vm3|
    vm3.vm.box = "hashicorp/bionic64"

    vm3.vm.provision "ansible" do |ansible|
      ansible.playbook = "playbook.yml"
      ansible.limit = "all"
      #ansible.ask_become_pass = true
      ansible.groups = {
        "node_master" => ["vm1"],
        "node_replicas" => ["vm2", "vm3"],
        "node:children" => ["node_master", "node_replicas"]
      }
    end
end

这样,您的 playbook 将 运行 同时在您的所有 vms 上并行,您应该能够从您在 playbook 中定位的所有主机访问事实。