Vagrant + Ansible + Python3

cla*_*lay 11 python vagrant ansible vagrantfile

我有一个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
Run Code Online (Sandbox Code Playgroud)

当我运行时vagrant provision,在Gathering Facts阶段,我得到/usr/bin/python: not found因为Ubuntu 16.04默认情况下只安装了python3Python 2.x.python

我看到几篇关于此的帖子.似乎最近版本的Ansible支持使用Python 3,但它必须通过ansible_python_interpreter=/usr/bin/python3hosts文件或ansible命令行进行配置.有没有办法在我Vagrantfile或我的playbook.yml文件中指定此选项?我目前没有使用hosts文件,我没有ansible-playbook通过命令行运行,我通过Vagrant集成运行Ansible.

仅供参考,我使用的是Ansible 2.4.1.0和Vagrant 2.0.1,这是本文撰写时的最新版本.

jul*_*las 23

据我所知,你可以在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
Run Code Online (Sandbox Code Playgroud)

在上面的块extra_vars设置ansible_python_interpreter或你可以这样使用 host_vars:

ansible.host_vars = {
        "default" => {
            "ansible_python_interpreter" => "/usr/bin/python2.7"
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • `extra_vars`部分的'ansible_python_interpreter:'/ usr/bin/python3'`工作得很好,谢谢! (4认同)