Vagrant + Ansible + Python3

Viewed 5964

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.

1 Answers

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"
        }
    }
Related