通过Vagrant自动安装和运行Ansible Local

时间:2016-02-09 18:18:31

标签: vagrant ansible

我正在使用Vagrant并尝试启用Ansible来使用它。因为我在使用Python 3.5.0的virtualenv工作,所以我必须使用Ansible-Local - “主”Ansible将无法在我的主机上运行,​​因为我没有运行Python 2.x。

无论如何,除了vagrant up找不到Ansible之外,一切都很有效。以下是来电的输出:

==> default: Running provisioner: ansible_local...
    default: Installing Ansible...
The Ansible software could not be found! Please verify
that Ansible is correctly installed on your guest system.

If you haven't installed Ansible yet, please install Ansible
on your Vagrant basebox, or enable the automated setup with the
`install` option of this provisioner. Please check
https://docs.vagrantup.com/v2/provisioning/ansible_local.html
for more information.

但是,如果我使用vagrant ssh访问VM,则会明确安装Ansible:

vagrant@vagrant-ubuntu-trusty-64:~$ ansible --version
ansible 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

我不知道该怎么办。我可以运行ansible-playbook server.yml,同时SSH进入服务器,它可以正常工作。但是,我使用Ansible的全部意义在于我可以通过Vagrant自动运行它。这是我的Vagrantfile的相关部分:

  config.vm.provision "ansible_local" do |ansible|
      ansible.install = true
      ansible.version = "latest"
      ansible.sudo = true
      ansible.playbook = "server.yml"
  end

非常感谢任何建议。谢谢!

1 个答案:

答案 0 :(得分:9)

将盒子置于调试模式:

VAGRANT_LOG=debug vagrant up

节目:

DEBUG ssh: Re-using SSH connection.
 INFO ssh: Execute: ansible-galaxy --help && ansible-playbook --help (sudo=false)
DEBUG ssh: stderr: ERROR! Missing required action

DEBUG ssh: stdout: Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...

Options:
  -h, --help     show this help message and exit
  -v, --verbose  verbose mode (-vvv for more, -vvvv to enable connection
                 debugging)
  --version      show program's version number and exit

DEBUG ssh: Exit status: 5

ansible-galaxy --help是问题所在,因为它期待一个行动。因为失败了,vagrant认为安装有问题。

看起来这已在master branch上修复,并且会在next version中。

你可以尝试的是:

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

$install_ansible = <<SCRIPT
apt-get -y install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get -y update
apt-get -y install ansible

SCRIPT

Vagrant.configure(2) do |config|
  config.vm.box = 'ubuntu/trusty64'
  config.vm.provision 'shell', inline: $install_ansible
  # Patch for https://github.com/mitchellh/vagrant/issues/6793
  config.vm.provision "shell" do |s|
    s.inline = '[[ ! -f $1 ]] || grep -F -q "$2" $1 || sed -i "/__main__/a \\    $2" $1'
    s.args = ['/usr/bin/ansible-galaxy', "if sys.argv == ['/usr/bin/ansible-galaxy', '--help']: sys.argv.insert(1, 'info')"]
  end
  config.vm.provision :ansible_local do |ansible|
    ansible.sudo = true
    ansible.playbook = 'server.yml'
  end
end

注意:归功于MasonM 以下代码段:

# Patch for https://github.com/mitchellh/vagrant/issues/6793
config.vm.provision "shell" do |s|
    s.inline = '[[ ! -f $1 ]] || grep -F -q "$2" $1 || sed -i "/__main__/a \\    $2" $1'
    s.args = ['/usr/bin/ansible-galaxy', "if sys.argv == ['/usr/bin/ansible-galaxy', '--help']: sys.argv.insert(1, 'info')"]
end 

或等待修复发布时的另一个选项是构建vagrant from source