使用Ansible的Vagrant配置失败

6
我正在尝试使用Vagrant和Ansible来配置Ubuntu。我正在参考这篇文章,但遇到了以下错误。
________________________
< TASK [Gathering Facts] >
------------------------
       \   ^__^
        \  (oo)\_______
           (__)\       )\/\
               ||----w |
               ||     ||

fatal: [default]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to 127.0.0.1 closed.\r\n", "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n", "msg": "MODULE FAILURE", "rc": 0}
 to retry, use: --limit @/Users/tomoya/vagrant-project/playbook.retry
____________
< PLAY RECAP >
------------
       \   ^__^
        \  (oo)\_______
           (__)\       )\/\
               ||----w |
               ||     ||

default                    : ok=0    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.

我的目录结构如下:

vagrant-project
├── Vagrantfile
└── playbook.yml

Vagrantfile 包含:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

playbook.yml 包含:

---
- hosts: all
  sudo: true
  tasks:
    - name: update apt cache
      apt: update_cache=yes
    - name: install apache
      apt: name=apache2 state=present
    - name: install mysql
      apt: name=mysql-server state=present
    - name: install php
      apt: name=php5 state=present

我正在使用:

  • Vagrant 1.9.4
  • VirtualBox 5.1.22
  • Ansible 2.3.0.0

他们几乎与文章中显示的代码相同。你能告诉我哪里出了问题,以及如何成功地进行配置吗?
谢谢。

你的输出中有一个错误信息:module_stdout": "/bin/sh: 1: /usr/bin/python: not found - Konstantin Suvorov
http://stackoverflow.com/questions/43678361/vagrant-ansible-provisioner-throwing-error-module-failure-when-running-playboo/43678427#43678427 是关于相同的问题。 - Frederic Henri
1个回答

12

正如Konstantin Suvorov所提到的,它可能是被提到的帖子的重复。回答你的问题,当ansible在远程主机上执行时,默认情况下它期望在/usr/bin/python中可用Python。但是在Ubuntu 16.04中/usr/bin/python不可用,只有/usr/bin/python3或/usr/bin/python3.5可用

我们可以通过以下两种方式解决这个问题:

1) 在开始ansible任务之前,在pre_tasks部分使用raw模块安装Python2,以使/usr/bin/python可用。Playbook将变为

---
- hosts: all
  sudo: true
  gather_facts: False

  pre_tasks:
    - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
    - setup:

  tasks:
    - name: update apt cache
      apt: update_cache=yes
    - name: install apache
      apt: name=apache2 state=present
    - name: install mysql
      apt: name=mysql-server state=present
    - name: install php
      apt: name=php5 state=present

或者

2)使用ansible_python_interpeter变量指定Python路径,在这种情况下,Vagrant文件将变成:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vbguest.auto_update = false
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.extra_vars = {
        ansible_python_interpreter: "/usr/bin/python3.5",
    }
  end
end

非常感谢!你的回答解决了我的问题 :) - user6908430

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接