在Ubuntu 16.04下,Ansible找不到Python

最近開始嘗試透過 Vagrant 與 Ansible 建置開發環境,主要以 ubuntu 官方 的 ubutnu/xenial64 box 作為開發環境,

# Vagrantfile
Vagrant.configure(“2”) do |config|
  config.vm.box = “ubuntu/xenial64”
  config.vm.define :app do |app|
    app.vm.hostname = “app.dev”
    app.vm.network :private_network, ip: “192.168.66.1”
    app.vm.provision “ansible” do |ansible|
      ansible.playbook = “provisions/app-playbook.yml”
    end
  end
end

但在 playbook.yml 尚未執行任何指令前,

# provisions/app-playbook.yml
---
- hosts: all

執行 vagrant provision,卻發生這樣的錯誤,

PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
fatal: [app]: 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”}
to retry, use: — limit @/Users/liyibin/servers/laravel-ap-server/provisions/app-playbook.retry
PLAY RECAP *********************************************************************
app : 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.

從錯誤訊息發現,似乎是因為 /usr/bin/python: not found,在Google後,找到 gwillem 分享在 Gist 的 ansible-bootstrap-ubuntu-16.04.yml

---
- hosts: all
  become: true
  gather_facts: False
  pre_tasks:

- name: Install python for Ansible
      raw: test -e /usr/bin/python || apt install -y python-minimal
      changed_when: False
  
- setup: # aka gather_facts

主要是因為 ubuntu 16.04 預設沒有安裝 python 2,因此必須要先安裝 Python 2才可以執行 Ansible,因此先將 gather_facts 設為 False,並在 pre_tasks 加入安裝 Python後,再執行 gather_facts,即可以正常執行 Ansible。

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.