linux - Ansible 在 'vagrant provision' 之后给出此错误 "Could not import python modules: apt, apt_pkg. Please install python-apt package."

标签 linux virtual-machine ubuntu-12.04 vagrant ansible

我有一个非常简单的 VagrantFile 和 Ansible Playbook。我只是想测试安装httpd。但每次我在虚拟机启动后运行vagrant provision时,都会收到此错误:

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.

这是我的 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

这是我的简单 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

还有我的 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

我确实在虚拟机上安装了 python-apt 包,但仍然没有骰子。 如果有人有任何建议,我很乐意听听。

最佳答案

我发现您正在使用 python2.6 的特定 python 解释器,并且您正在使用 Ubuntu Precise 64 镜像。我相信 apt-get 的 Precise 64 包适用于 python 2.7(根据 apt-cache show python-apt)。假设您使用 apt 和默认源来安装 python-apt,我认为 2.6 解释器将无法使用 apt 软件包。

关于linux - Ansible 在 'vagrant provision' 之后给出此错误 "Could not import python modules: apt, apt_pkg. Please install python-apt package.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895203/

相关文章:

linux - 内存溢出!在 Linux 中

linux - 在 HPUX 上连接失败后空闲的 tcp 文件描述符

linux - 使用 Chrome(60 及以上版本)、Firefox(50 或以上)、Internet Explorer 在 Linux 虚拟机上执行 Selenium 脚本

docker - 从现有虚拟机创建 docker 镜像

android - ADB 未在 ubuntu 12.04 中检测到设备 samsung i777

python - 如何每隔 x 小时在 bash 脚本中重新启动 python 脚本?

linux - 使用 get_wchan()

python - 如何让多个虚拟机在 GCP 中的同一个脚本上运行?

ubuntu - 卸载相扑 0.22

wxpython - 在 Ubuntu 12.04 上安装 wxPython