Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-14.04"
config.vm.define "server"
# run Ansible playbook from Vagrant host
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
$ vagrant provision
==> server: Running provisioner: ansible...
Vagrant has automatically selected the compatibility mode '2.0'
according to the Ansible version installed (2.4.2.0).
Alternatively, the compatibility mode can be specified in your Vagrantfile:
https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode
server: Running ansible-playbook...
PLAY [server] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server]
TASK [test connection] *********************************************************
ok: [server]
TASK [print debug message] *****************************************************
ok: [server] => {
"msg": {
"changed": false,
"failed": false,
"ping": "pong"
}
}
PLAY RECAP *********************************************************************
server : ok=3 changed=0 unreachable=0 failed=0
雖然透過 Vagrant 運行 playbook 的步驟容易許多,但必須注意的是,這個方法只限於我們使用 Vagrant 搭建虛擬機的時候才能使用。若今天我們需要直接部署配置到實體主機上,我們就無法透過 Vagrant 指揮 managed node 了。這也是為什麼我們還是必須知道如何使用最正規的方法運行 playbook 進行部署,這個章節的教學只是提供一點搭配的小技巧,讓讀者在練習或開發 Ansible playbook 的時候能夠節省更多時間。