Skip to content
Advertisement

Ansible after ‘vagrant provision’ gives this error “Could not import python modules: apt, apt_pkg. Please install python-apt package.”

I have a very simple VagrantFile and Ansible Playbook. I just want to test install httpd. But every time I run vagrant provision after the VM is up I get this error:

Rons-MacBook-Pro:development you$ vagrant provision
[default] Running provisioner: ansible...

PLAY [Install and start apache] *********************************************** 

GATHERING FACTS *************************************************************** 
<10.0.0.111> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-1384111346.71-231091208956411 && echo $HOME/.ansible/tmp/ansible-1384111346.71-231091208956411']
<10.0.0.111> REMOTE_MODULE setup 
<10.0.0.111> PUT /var/folders/h7/3b23bqhs5g39w_jntlkz3hpm0000gn/T/tmpQ3Hvaw TO /Users/you/.ansible/tmp/ansible-1384111346.71-231091208956411/setup
<10.0.0.111> EXEC ['/bin/sh', '-c', '/usr/bin/python2.6 /Users/you/.ansible/tmp/ansible-1384111346.71-231091208956411/setup; rm -rf /Users/you/.ansible/tmp/ansible-1384111346.71-231091208956411/ >/dev/null 2>&1']
ok: [10.0.0.111]

TASK: [Update apt cache] ****************************************************** 
<10.0.0.111> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-1384111347.33-79837739787852 && echo $HOME/.ansible/tmp/ansible-1384111347.33-79837739787852']
<10.0.0.111> REMOTE_MODULE apt upgrade=yes update_cache=yes
<10.0.0.111> PUT /var/folders/h7/3b23bqhs5g39w_jntlkz3hpm0000gn/T/tmpr5r1YH TO /Users/you/.ansible/tmp/ansible-1384111347.33-79837739787852/apt
<10.0.0.111> EXEC ['/bin/sh', '-c', '/usr/bin/python2.6 /Users/you/.ansible/tmp/ansible-1384111347.33-79837739787852/apt; rm -rf /Users/you/.ansible/tmp/ansible-1384111347.33-79837739787852/ >/dev/null 2>&1']
failed: [10.0.0.111] => {"failed": true}
msg: Could not import python modules: apt, apt_pkg. Please install python-apt package.

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/Users/you/playbook.retry

10.0.0.111                 : ok=1    changed=0    unreachable=0    failed=1   

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

This is my VagrantFile:

Vagrant.configure("2") do |config|

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "development-precise64"
# config.vm.host_name = "development.somethingwithcomputers.com"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://files.vagrantup.com/precise64.box"

config.vm.network "private_network", ip: "10.0.0.111"

# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
# config.vm.share_folder "v-data", "/vagrant_data", "../data"

config.vm.synced_folder "/Users/rontalman/Public/Dropbox/Development/Code/Webdevelopment/htdocs/mgc.com", "/var/www", id: "vagrant-root", :nfs => false

config.vm.usable_port_range = (2200..2250)
config.vm.provider :virtualbox do |virtualbox|
  virtualbox.customize ["modifyvm", :id, "--name", "mgc"]
  virtualbox.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  virtualbox.customize ["modifyvm", :id, "--memory", "512"]
  virtualbox.customize ["setextradata", :id, "--VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
end

# SSH
config.ssh.username = "vagrant"
config.ssh.shell = "bash -l"
config.ssh.keep_alive = true
config.ssh.forward_agent = false
config.ssh.forward_x11 = false

config.vagrant.host = :detect

# Ansible
config.vm.provision "ansible" do |ansible|
  ansible.inventory_path = "provisioning/inventory.yml"
  ansible.playbook = "provisioning/playbook.yml"
  ansible.verbose = "vvvv"
end
end

And this is my simple playbook.yml:

---
- name: Install and start apache
  hosts: all
  user: root

  tasks:
  - name: Update apt cache
    apt: upgrade=yes
         update_cache=yes

  - name: Install httpd
    apt: pkg=httpd

  - name: Start httpd
    service: name=httpd state=running

And my inventory.yml:

[vagrant]

# Set at config.vm.network in the VagrantFile
10.0.0.111      ansible_connection=local ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass=vagrant ansible_python_interpreter=/usr/bin/python2.6

I did install the python-apt package on the virtual machine, but still no dice. If anybody has any tips, I’d love to hear them.

Advertisement

Answer

I see you’re using a specific python interpreter for python2.6, and that you’re using an Ubuntu Precise 64 image. I believe the Precise 64 package for apt-get is for python 2.7 (per apt-cache show python-apt). Assuming you used apt & default sources to install python-apt, I don’t think the apt packages will be available to the 2.6 interpreter.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement