Vagrant + Ansible + Python3

14

我有一个Vagrantfile,它被简化为:

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.boot_timeout = 900

  config.vm.define 'srv' do |srv|
    srv.vm.provision 'ansible' do |ansible|
      ansible.compatibility_mode = '2.0'
      ansible.playbook = 'playbook.yml'
    end
  end
end
当我运行vagrant provision命令时,在Gathering Facts阶段报错:/usr/bin/python: not found,因为Ubuntu 16.04默认只安装了python3而非Python 2.x python

我看到有些旧文章提到这个问题。Ansible的最新版本支持使用Python 3,但必须通过在主机文件或ansible命令行中配置ansible_python_interpreter=/usr/bin/python3来设置。是否可以在我的Vagrantfileplaybook.yml文件中指定此选项?我目前没有使用主机文件,也没有通过命令行运行ansible-playbook,而是通过Vagrant集成运行Ansible。

FYI,我正在使用Ansible 2.4.1.0和Vagrant 2.0.1,它们是截至本写作时的最新版本。
1个回答

32
据我所知,您可以在 Vagrant 文件中使用 extra_vars,确保将其放在 Ansible 范围内。
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.boot_timeout = 900

  config.vm.define 'srv' do |srv|
    srv.vm.provision 'ansible' do |ansible|
      ansible.compatibility_mode = '2.0'
      ansible.playbook = 'playbook.yml'
      ansible.extra_vars = { ansible_python_interpreter:"/usr/bin/python2" }
    end
  end
end
在上述代码块中,extra_vars是通过ansible_python_interpreter进行设置的,或者您可以像这样使用host_vars:
ansible.host_vars = {
        "default" => {
            "ansible_python_interpreter" => "/usr/bin/python2.7"
        }
    }

4
extra_vars部分中,ansible_python_interpreter: '/usr/bin/python3'完美地运行了,谢谢! - clay
由于某种奇怪的原因,Ansible开始在我使用Vagrant管理的本地集群上寻找我的本地Python(通过Homebrew在macOS上安装)(/bin/sh: 1: /usr/local/opt/python@2/bin/python2.7: not found),因此需要使用ansible.extra_vars显式定义它以解决连接问题。我猜我在其他地方错误地设置了解释器 :/ - geerlingguy
啊,我刚发现原因了——是我自己愚蠢的角色覆盖了 ansible_python_interpreter(参见:https://github.com/geerlingguy/ansible-role-k8s_manifests/issues/3)。 - geerlingguy
哈,一年里我第三次回到这个答案。再次感谢@julian salas! - geerlingguy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接