如何在本地使用Ansible和Vagrant?

时间:2016-09-20 19:12:32

标签: vagrant ansible ansible-playbook vagrantfile vagrant-provision

我遵循了http://docs.ansible.com/ansible/guide_vagrant.html

的官方教程

我的Vagrantfile看起来像是:

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = 'bento/centos-7.2'

  # SSH default root user
  config.ssh.username = 'root'
  config.ssh.password = 'vagrant'

  # Networking
  # config.vm.network :private_network, ip: '192.168.33.16'

  # Provisioning
  config.vm.provision :ansible do |ansible|
    ansible.playbook = 'playbooks/main.yml'
  end
end

我的test.sh脚本:

ansible-playbook \
  --private-key=.vagrant/machines/default/virtualbox/private_key \
  -u vagrant \
  -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory \
  playbooks/main.yml \
  $@

但是当我运行脚本时,我收到以下错误:

fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
  to retry, use: --limit @playbooks/main.retry

PLAY RECAP *********************************************************************
default                    : ok=0    changed=0    unreachable=1    failed=0

如何使用ansible-playbook测试Vagrant

1 个答案:

答案 0 :(得分:0)

如果你想对主机运行ansible,那么Vagrant已经拥有ansible_local配置器。

要运行此操作,您必须先在来宾计算机上安装ansible(如果尚未安装)。这可以通过easy_installpip的Vagrantfile shell配置程序完成。