即使清单文件设置正确,Ansible 也会重新提供相同的主机

Ansible is re-provision the same host even though inventory file setup correctly

我已经尝试调试它一段时间了,我以为我可以正常工作,但后来做了一些其他更改,现在又回来了。

基本上,我让 Vagrant 循环遍历机器定义列表,虽然我的 Ansible 清单看起来非常好,但我发现实际上只配置了一个主机。

生成的 Ansible 清单 -- SSH 端口都不同,组是正确的

# Generated by Vagrant

kafka.cp.vagrant ansible_host=127.0.0.1 ansible_port=2200 ansible_user='vagrant' ansible_ssh_private_key_file='/workspace/confluent/cp-ansible/vagrant/.vagrant/machines/kafka.cp.vagrant/virtualbox/private_key' kafka='{"broker": {"id": 1}}'
zk.cp.vagrant ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_ssh_private_key_file='/workspace/confluent/cp-ansible/vagrant/.vagrant/machines/zk.cp.vagrant/virtualbox/private_key'
connect.cp.vagrant ansible_host=127.0.0.1 ansible_port=2201 ansible_user='vagrant' ansible_ssh_private_key_file='/workspace/confluent/cp-ansible/vagrant/.vagrant/machines/connect.cp.vagrant/virtualbox/private_key'

[preflight]
zk.cp.vagrant
kafka.cp.vagrant
connect.cp.vagrant

[zookeeper]
zk.cp.vagrant

[broker]
kafka.cp.vagrant

[schema-registry]
kafka.cp.vagrant

[connect-distributed]
connect.cp.vagrant

生成的主机文件 -- IP 和主机名正确

## vagrant-hostmanager-start id: aca1499c-a63f-4747-b39e-0e71ae289576
192.168.100.101 zk.cp.vagrant

192.168.100.102 kafka.cp.vagrant

192.168.100.103 connect.cp.vagrant

## vagrant-hostmanager-end

我想要的 Ansible Playbook 运行 -- 正确对应我清单中的组

- hosts: preflight
  tasks:
  - import_role:
      name: confluent.preflight
- hosts: zookeeper
  tasks:
  - import_role:
      name: confluent.zookeeper
- hosts: broker
  tasks:
  - import_role:
      name: confluent.kafka-broker
- hosts: schema-registry
  tasks:
  - import_role:
      name: confluent.schema-registry
- hosts: connect-distributed
  tasks:
  - import_role:
      name: confluent.connect-distributed

对于此处遗漏的任何代码,请参阅 Confluent :: cp-ansible

以下是我的 Vagrantfile 示例。 (我 made a fork,但在我开始工作之前还没有承诺...)

我知道这个 if index == machines.length - 1 应该工作 according to the Vagrant documentation,它确实启动了所有机器,然后在最后一台机器上只有 运行s Ansible,但它只是所有任务由于某种原因在 first 上执行。

machines = {"zk"=>{"ports"=>{2181=>nil}, "groups"=>["preflight", "zookeeper"]}, "kafka"=>{"memory"=>3072, "cpus"=>2, "ports"=>{9092=>nil, 8081=>nil}, "groups"=>["preflight", "broker", "schema-registry"], "vars"=>{"kafka"=>"{\"broker\": {\"id\": 1}}"}}, "connect"=>{"ports"=>{8083=>nil}, "groups"=>["preflight", "connect-distributed"]}}

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

  if Vagrant.has_plugin?("vagrant-hostmanager")
    config.hostmanager.enabled = true
    config.hostmanager.manage_host = true
    config.hostmanager.ignore_private_ip = false
    config.hostmanager.include_offline = true
  end

  # More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end

  if Vagrant.has_plugin?("vagrant-vbguest")
    config.vbguest.auto_update = false
  end

  config.vm.box = VAGRANT_BOX
  config.vm.box_check_update = false
  config.vm.synced_folder '.', '/vagrant', disabled: true

  machines.each_with_index do |(machine, machine_conf), index|
    hostname = getFqdn(machine.to_s)

    config.vm.define hostname do |v|
      v.vm.network "private_network", ip: "192.168.100.#{101+index}"
      v.vm.hostname = hostname

      machine_conf['ports'].each do |guest_port, host_port|
        if host_port.nil?
          host_port = guest_port
        end
        v.vm.network "forwarded_port", guest: guest_port, host: host_port
      end

      v.vm.provider "virtualbox" do |vb|
        vb.memory = machine_conf['memory'] || 1536 # Give overhead for 1G default java heaps
        vb.cpus = machine_conf['cpus'] || 1
      end

      if index == machines.length - 1
        v.vm.provision "ansible" do |ansible|
          ansible.compatibility_mode = '2.0'
          ansible.limit = 'all'
          ansible.playbook = "../plaintext/all.yml"
          ansible.become = true
          ansible.verbose = "vv"

          # ... defined host and group variables here

        end # Ansible provisioner
      end # If last machine
    end # machine configuration
  end # for each machine
end 

我像这样设置了一个 Ansible 任务

- debug:
    msg: "FQDN: {{ansible_fqdn}}; Hostname: {{inventory_hostname}}; IPv4: {{ansible_default_ipv4.address}}"

就该任务而言,请注意以下 ansible_fqdnalways zk.cp.vagrant,这符合只有该 VM 正在获取的事实由 Ansible 提供。

ok: [zk.cp.vagrant] => {
    "msg": "FQDN: zk.cp.vagrant; Hostname: zk.cp.vagrant; IPv4: 10.0.2.15"
}
ok: [kafka.cp.vagrant] => {
    "msg": "FQDN: zk.cp.vagrant; Hostname: kafka.cp.vagrant; IPv4: 10.0.2.15"
}
ok: [connect.cp.vagrant] => {
    "msg": "FQDN: zk.cp.vagrant; Hostname: connect.cp.vagrant; IPv4: 10.0.2.15"
}

用最少的例子更新:hostname -f 只是一个主机,我认为这就是 gather_factsansible_fqdn

的 运行ning
ansible all --private-key=~/.vagrant.d/insecure_private_key --inventory-file=/workspace/confluent/cp-ansible/vagrant/.vagrant/provisioners/ansible/inventory -a 'hostname -f' -f1

zk.cp.vagrant | SUCCESS | rc=0 >>
kafka.cp.vagrant

connect.cp.vagrant | SUCCESS | rc=0 >>
kafka.cp.vagrant

kafka.cp.vagrant | SUCCESS | rc=0 >>
kafka.cp.vagrant

原来我可以通过 not 在我的 ansible.cfg

中包含此部分来解决这个问题
[ssh_connection]
control_path = %(directory)s/%%h-%%r