使用 Ansible 的 Vagrant 配置失败

Vagrant provisioning with Ansible fails

我正在尝试使用 Vagrant 和 Ansible 提供 Ubuntu。我正在使用 this article 并遇到如下所示的错误。

________________________
< 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

我正在使用:


它们与文章中显示的代码几乎相同。您能告诉我哪里出了问题以及我如何才能成功配置它吗?

谢谢。

正如 Konstantin Suvorov 所提到的,它可能是上述 post 的副本。要回答您的问题,当 ansible 在远程主机上执行时,默认情况下它会排除 python 在 /usr/bin/python 中可用。但是在 ubuntu 16.04 中 /usr/bin/python 不可用,只有 /usr/bin/python3 或 /usr/bin/python3.5 可用 .

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

1) 在启动 ansible 任务之前,使用 pre_tasks 部分中的原始模块安装 python2,因此 /usr/bin/python 可用。剧本将变成

---
- 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