Это работает, хотя это не очень красиво
# -*- 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
bash-4.4$ vagrant status
Current machine states:
machine1 running (virtualbox)
machine2 running (virtualbox)
machine3 not created (virtualbox)