Vagrant的Ansible供应商的动态库存?

时间:2018-06-05 21:51:15

标签: ansible vagrant

我目前正在尝试将动态广告资源写入我的Vagrant脚本的运行部分,但无法枚举vm_nameip_address等内容。我知道这只是因为我试图f.write错误的查找,遗憾的是我不知道我应该调用的变量?你可以在Vagrantfile的评论栏中看到我正在尝试的内容。

Vagrant.configure(2) do | config |
  N = 2

  ANSIBLE_RAW_SSH_ARGS  = []
  ANSIBLE_INVENTORY_DIR = "provisioner/inventory"
  VAGRANT_VM_PROVIDER   = "virtualbox"

  (1..N-1).each do | machine_id |
    ANSIBLE_RAW_SSH_ARGS << "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
  end

  (1..N).each do | machine_id |
    config.vm.define "machine#{machine_id}" do | machine |

      machine.vm.box      = "ubuntu/trusty64"
      machine.vm.hostname = "machine#{machine_id}"

      machine.vm.network "private_network",
        ip: "192.168.77.#{10+machine_id-1}"

      # only execute once the Ansible provisioner, when all the machines are up and ready.
      if machine_id == N
        machine.vm.provision :ansible do | ansible |
          # Disable default limit to connect to all the machines
          ansible.limit           = "all"
          ansible.playbook        = "provisioner/test.yml"
          ansible.inventory_path  = "#{ANSIBLE_INVENTORY_DIR}/vagrant"
          ansible.verbose         = "-v"

          ansible.raw_ssh_args    = ANSIBLE_RAW_SSH_ARGS
        end
        # dynamically create the Ansible inventory file
        Dir.mkdir(ANSIBLE_INVENTORY_DIR) unless Dir.exist?(ANSIBLE_INVENTORY_DIR)
        # File.open("#{ANSIBLE_INVENTORY_DIR}/vagrant" ,'w') do | f |
        #   f.write "[#{machine_id['vm_name']}]\n"
        #   f.write "#{machine_id['ip_address']}\n"
        # end
      end
    end
  end
end

test.yml 如果有人关心

---
- hosts: all
  gather_facts: false
  tasks:
  - command: hostname -f

1 个答案:

答案 0 :(得分:0)

即使它不是超级漂亮,这仍然有效

T

这给了我一个动态主机文件

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

Vagrant.configure(2) do | config |
  N = 3

  ANSIBLE_RAW_SSH_ARGS  = []
  ANSIBLE_INVENTORY_DIR = "provisioner/inventory"

  VAGRANT_VM_PROVIDER   = "virtualbox"

  (N-1..N).each do | machine_id |
    ANSIBLE_RAW_SSH_ARGS << "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
  end

  # ensure directory exists
  Dir.mkdir(ANSIBLE_INVENTORY_DIR) unless Dir.exist?(ANSIBLE_INVENTORY_DIR)

  # dynamically create the Ansible inventory file
  File.open("#{ANSIBLE_INVENTORY_DIR}/hosts" ,'w') do | f |
    f.write "[all]\n"
    (N-1..N).each do | machine_id |
      f.write "192.168.77.#{10+machine_id-1}\n"
    end
    f.write "\n"
  end


  (1..N).each do | machine_id |
    config.vm.define "machine#{machine_id}" do | machine |
      machine.vm.box      = "ubuntu/trusty64"
      machine.vm.hostname = "machine#{machine_id}"

      machine.vm.network "private_network",
        ip: "192.168.77.#{10+machine_id-1}"

      # only execute once the Ansible provisioner, when all the machines are up and ready.
      if machine_id != 1

        # dynamically create individual Ansible entries
        File.open("#{ANSIBLE_INVENTORY_DIR}/hosts" ,'a') do | f |
          f.write "[machine#{machine_id}]\n"
          f.write "192.168.77.#{10+machine_id-1}\n"
          f.write "\n"
        end

        machine.vm.provision :ansible do | ansible |

          # Disable default limit to connect to all the machines
          ansible.limit           = "all"
          ansible.playbook        = "provisioner/test.yml"
          ansible.inventory_path  = "#{ANSIBLE_INVENTORY_DIR}/hosts"
          ansible.verbose         = "-v"
          ansible.raw_ssh_args    = ANSIBLE_RAW_SSH_ARGS
        end
      end
    end
  end
end

不幸的是,我看起来运行不好,但我认为这是由于竞争条件导致我在服务之前添加[all] 192.168.77.11 192.168.77.12 [machine2] 192.168.77.11 [machine3] 192.168.77.12

machine3

... machine2: Running ansible-playbook... PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o IdentityFile=/Users/ehime/Repositories/Corporations/ByDeluxe/terraform-ansible/.vagrant/machines/machine2/virtualbox/private_key -o IdentityFile=.vagrant/machines/machine2/virtualbox/private_key -o IdentityFile=.vagrant/machines/machine3/virtualbox/private_key -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --extra-vars=ansible_user\=\'vagrant\' --limit="all" --inventory-file=provisioner/inventory/hosts -v provisioner/test.yml [WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting PLAY [all] ********************************************************************* TASK [command] ***************************************************************** changed: [192.168.77.11] => {"changed": true, "cmd": ["hostname", "-f"], "delta": "0:00:00.003067", "end": "2018-06-06 00:06:19.628683", "rc": 0, "start": "2018-06-06 00:06:19.625616", "stderr": "", "stderr_lines": [], "stdout": "machine2", "stdout_lines": ["machine2"]} fatal: [192.168.77.12]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.77.12 port 22: Operation timed out\r\n", "unreachable": true} to retry, use: --limit @/Users/ehime/Repositories/Corporations/ByDeluxe/terraform-ansible/provisioner/test.retry PLAY RECAP ********************************************************************* 192.168.77.11 : ok=1 changed=1 unreachable=0 failed=0 192.168.77.12 : ok=0 changed=0 unreachable=1 failed=0 Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.

vagrant status