'vagrant provision'之后的Ansible给出了这个错误:“无法导入python模块:apt,apt_pkg。 请安装python-apt软件包。“

我有一个非常简单的VagrantFile和Ansible Playbook。 我只是想testing安装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. 

这是我的stream浪文件:

 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,我不认为apt软件包将可用于2.6解释器。

我使用的解决方法是将apt-get packgages安装在单独的角色中 ,而不显式设置ansible_python_interpreter 。 然后在其中设置了ansible_python_interpreter的下一个角色中休息。 希望这可以帮助。