14

I have a Vagrantfile that is simplified to:

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

When I run vagrant provision, at the Gathering Facts stage, I get /usr/bin/python: not found because Ubuntu 16.04 by default only has python3 not Python 2.x python installed.

I see several older posts about this. It seems recent versions of Ansible support using Python 3, but it must be configured via ansible_python_interpreter=/usr/bin/python3 in the hosts file or on the ansible command line. Is there any way I specify this option in my Vagrantfile or in my playbook.yml file? I'm currently not using a hosts file, I'm not running ansible-playbook via command line, I'm running Ansible through the Vagrant integration.

FYI, I'm using Ansible 2.4.1.0 and Vagrant 2.0.1, which are the latest versions as of this writing.

clay
  • 18,138
  • 28
  • 107
  • 192

1 Answers1

32

As far as I know, you can use in Vagrant file extra_vars make sure put it inside of ansible scope.

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

In above block extra_vars was set up withansible_python_interpreter or you can use host_vars like this:

ansible.host_vars = {
        "default" => {
            "ansible_python_interpreter" => "/usr/bin/python2.7"
        }
    }
julian salas
  • 3,714
  • 1
  • 19
  • 20
  • 4
    `ansible_python_interpreter: '/usr/bin/python3'` in the `extra_vars` section worked perfectly, thanks! – clay Nov 21 '17 at 23:28
  • For some strange reason, Ansible started looking for my local Python (installed on macOS via Homebrew) on a local cluster I was managing with Vagrant (`/bin/sh: 1: /usr/local/opt/python@2/bin/python2.7: not found`), so explicitly defining it with `ansible.extra_vars` was required to fix the connection issue. I'm guessing I have set the interpreter somewhere else errantly :/ – geerlingguy Jan 03 '19 at 03:15
  • Ah, just found out why—it's my own silly role that's overwriting `ansible_python_interpreter` (see: https://github.com/geerlingguy/ansible-role-k8s_manifests/issues/3). – geerlingguy Jan 03 '19 at 03:17
  • Ha, third time in a year I've come back to this answer. Thanks again @julian salas! – geerlingguy Mar 20 '20 at 16:22