2015-09-06 226 views
120

我遇到了一個我從未見過的錯誤。下面是一個命令和錯誤:Ansible以/ bin/sh失敗:1:/ usr/bin/python:找不到

$ ansible-playbook create_api.yml 

PLAY [straw] ****************************************************************** 

GATHERING FACTS *************************************************************** 
failed: [104.55.47.224] => {"failed": true, "parsed": false} 
/bin/sh: 1: /usr/bin/python: not found 


TASK: [typical | install required system packages] ***************************** 
FATAL: no hosts matched or all hosts have already failed -- aborting 


PLAY RECAP ******************************************************************** 
      to retry, use: --limit @/Users/john/create_api.retry 

104.55.47.224    : ok=0 changed=0 unreachable=0 failed=1 

這裏是create_api.yml文件:

--- 

- hosts: api 
    remote_user: root 
    roles: 
    - api 

,這裏是主機文件:

[api] 
104.55.47.224 

我可以刪除角色部分它不會使它成爲第一個任務,它將使它只會變成行/bin/sh: 1: /usr/bin/python: not found。這裏會發生什麼?


注:如果有人在強制IP地址,並未能得到迴應,你應該知道,因爲粘貼代碼我已經改變了IP地址。

編輯 蟒蛇是本地安裝的,問題是,這是不是在遠程機器,這是運行Ubuntu 15.04

回答

28

可以使用raw module在遠程主機上安裝Python的安裝:

- raw: sudo apt-get install python-simplejson 
+11

爲了確保這一點會對自己的角色任務之前調用,並且之前在元文件的任何相關性,這樣把它添加到您的劇本: '''pre_tasks: - 原料:sudo易於得到安裝python- simplejson ''' –

+4

請注意,在劇本中,您必須*也*禁用gather_facts,否則在運行raw命令之前將失敗。 (gather_facts:no) – rcreswick

+0

@rcreswick這是我的問題,你的解決方案爲我工作。非常感謝。 我在我的主要.yml文件(setup-ansible.yml)中放入了「gather_facts:no」行,並使用以下命令執行了劇本:「ansible-playbook -i hosts setup-ansible.yml --flush-cache -vvvvvv - KK」。 因爲默認的ubuntu安裝需要密碼才能執行「sudo」,所以我使用了「-kK」選項。 –

126

我偶然發現在運行ansible的Ubuntu 15.10服務器這個錯誤,因爲它附帶的Python 3.4.3ansible requires Python 2

這是我現在provision.yml的樣子:

- hosts: my_app 
    sudo: yes 
    remote_user: root 
    gather_facts: no 
    pre_tasks: 
    - name: 'install python2' 
     raw: sudo apt-get -y install python-simplejson 

    tasks: 
    - name: 'ensure user {{ project_name }} exists' 
     user: name={{ project_name }} state=present 
  • 不要忘了-y(說是所有問題)與選項apt-get的(或原始模塊將被卡住默默)

  • gather_facts: no線也很關鍵(因爲我們不能沒有收集蟒蛇的事實)

+8

那麼後來的角色不能使用事實......有沒有辦法再次收集事實? aha,http://stackoverflow.com/questions/31054453/ansible-actions-before-gathering-facts – surfer190

+1

也可能需要'sudo apt-get update' – AlanSE

+11

請注意,'gather_facts:no'這一行也是* critical * 。 – rcreswick

10

我用來獲取在Ubuntu 15.10這個工作在一個新的數字海洋滴:

# my-playbook.yml 
- name: python2 
    hosts: test 
    gather_facts: no 
    pre_tasks: 
    - raw: sudo apt-get -y install python-simplejson 

$ ansible-playbook path/to/my-playbook.yml 

就新鮮OVH SSD ubuntu的16.04,我必須易於得到的python2包之前升級可用。

-2

我們只是碰到這個。

我們將ubuntu 16.04部署在流浪者身上,所以如果你不使用流浪者,我的評論是毫無意義的。

我們安裝了下面的vagrant插件(觸發器,shell-command),我們得到python 2.7。安裝了6機(這是不是沒有thioose插件)上,並ansible後可以部署

這是我們的最後一次測試,否則我們要包括這個安裝在流浪一個shell命令文件

希望它可以幫助某人

+2

如果您使用Ansible,下面的Ansible解決方案是正確的修復。希望Vagrant會偶然爲你安裝它,因爲一些插件的副作用似乎是在尋求麻煩。 –

+0

不好意思,但是如果沒有python,你怎麼能運行一個可靠的pretask? 我嘗試瞭解決方案,並且在任務設置中發生故障,因此在執行前任務之前的事件。 流浪者檔案中的流浪外殼命令是做這件事的最佳方式(當然是流浪案例),但我只注意到我安裝在我的開發者上的流浪插件完成了這項工作。 我不是依賴插件,而是在流浪文件上,我只是指出它也適用於插件,但流浪文件是一個更好的選擇(也適用於自動化),因爲您手動無需執行任何操作每個銷燬/提供 – wadoo

+1

我完全按照原來的方式複製了該代碼塊,並且它在您發佈答案之前完全按照所述的方式工作。我相信你可能沒有把需要python的'gather_facts:no'行加入。另一種可能是你也需要在主機上使用python,但我猜想在這個過程中甚至會提前發生錯誤。 –

4

正如其他人所說,這是由於缺少python2。其他的答案在這裏提供pre_tasksgather_facts: no一種解決方法,但是如果你在EC2和你旋轉了ansible的情況下,你可以使用user_data選項:

- ec2: 
    key_name: mykey 
    instance_type: t2.micro 
    image: ami-123456 
    wait: yes 
    group: webserver 
    count: 3 
    vpc_subnet_id: subnet-29e63245 
    assign_public_ip: yes 
    user_data: | 
     #!/bin/bash 
     apt-get update 
     apt-get install -y python-simplejson 
    register: ec2 

那麼,人們通常等待ssh來提供這樣的:

- name: "Wait for the instances to boot and start ssh" 
    wait_for: 
     host: "{{item.public_ip}}" 
     port: 22 
     delay: 5 
     timeout: 300 
    with_items: "{{ ec2.tagged_instances }}" 
    when: ec2|changed 

但是我發現,隨着CloudInit執行得比較晚的引導過程,使得在python2仍可能未安裝SSH可用之後,這是不夠總是長。所以我加入的情況下,該實例是剛剛創建了一個暫停:

- name: "Wait for cloud init on first boot" 
    pause: minutes=2 
    when: ec2|changed 

這將完全做的工作,作爲一個優勢你不檢查在每次運行python2,你不必做任何解決方法以後收集事實。

我確定其他雲提供商提供了類似的CloudInit功能,因此請根據您的使用情況進行調整。

8

您需要python 2.7來運行Ansible。 在Ubuntu 16.04中,您可以通過這個命令來安裝它:

sudo apt-get install python-minimal

在那之後,我可以運行

ansible-playbook -i inventories/staging playbook.yml

Run ansible successfully

請查看更多在Using ansible on Ubuntu 16.04

13

總結其他人的一個swers,這裏的組合設置爲我工作:

- hosts: all 
    become: true 
    gather_facts: false 

    # Ansible requires python2, which is not installed by default on Ubuntu Xenial 
    pre_tasks: 
    - raw: sudo apt-get -y install python-simplejson 
    # action: setup will gather facts after python2 has been installed 
    - action: setup 
1

根據this Gist你可以在Ubuntu 16.04安裝Python2如下:

enter code here 
gather_facts: False 
pre_tasks: 
    - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) 
    - setup: # aka gather_facts 

tasks: 
    # etc. etc. 
53

Ansible 2.2功能的Python 3的支持技術預覽。 To take advantage of this(因此您不必在Ubuntu 16.04上安裝Python 2),只需將ansible_python_interpreter配置選項設置爲/usr/bin/python3即可。這可以在每臺主機上的庫存中的文件來完成:

[db] 
123.123.123.123 ansible_python_interpreter=/usr/bin/python3 
39

解決方案1:

如果您使用Ansible >2.2.0,您可以在ansible_python_interpreter配置選項設置爲/usr/bin/python3

ansible my_ubuntu_host -m ping -e 'ansible_python_interpreter=/usr/bin/python3' 

或在您的清單文件:

[ubuntu_hosts] 
<xxx.xxx.xxx.xxx> 

[ubuntu_hosts:vars] 
ansible_python_interpreter=/usr/bin/python3 

解決方案2:

如果你使用Ansible <2.2.0,那麼你可以將這些pre_tasks添加到您的劇本:

gather_facts: False 
pre_tasks: 
    - name: Install python for Ansible 
    raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) 
    register: output 
    changed_when: output.stdout != "" 
    tags: always 
    - setup: # aka gather_facts 

UPDATE: Ansible >= 2.3.0開箱與python3,無需要做上面提到的步驟。

+0

如果您使用標籤來運行您的手冊,請確保您添加標籤:始終是安裝任務 - 否則ansible不會收集事實你正在使用標籤。 –

+12

我有'ansible 2.3.0.0',它不能用於開箱即用。與OP發佈的錯誤相同。 –

+0

如果沒有明確說明,您必須將其添加到主機清單文件中,而不是添加到包含的變量中,即它與主機地址/名稱位於相同的清單文件中。 –

1

我能夠通過在目標機器上安裝Python來解決同樣的問題,即我們希望通過SSH連接的機器。我已經使用了以下命令 - sudo apt-get install python-minimal

1

很多答案..感謝張貼,因爲我也從這頁開始!

我做了一些挖掘工作,它使用Ubuntu 14.04LTS,Ubuntu 15.04LTS似乎已經丟棄了最新的python,而Ubuntu 16.04LTS似乎已經丟失了​​。

我把下面的動作在我的引導做任何apt調用之前:

- name: "FIX: Ubuntu 16.04 LTS doesn't come with certain modules, required by ansible" 
    raw: apt-get install python-minimal aptitude -y 
    become: true 
    become_user: root 
    become_method: sudo 

如果管理become其他地方,隨意剝奪它。

來源:

7

我發現,它實際上可能有多個劇本在一個單一的劇本,所以我的設置現在包含一個「依賴供應」戲它在所有主機上運行,​​以及其他特定主機的運行。所以沒有更多pre_tasks

例如:

- name: dependency provisioning 
    hosts: all 
    become: yes 
    become_method: sudo 
    gather_facts: false 
    tasks: 
    - name: install python2 
     raw: sudo apt-get -y install python-simplejson 

- name: production 
    hosts: production_host 
    roles: 
    - nginx 
    tasks: 
    - name: update apt cache 
     apt: update_cache=yes cache_valid_time=3600 
    # .... 

- name: staging 
    hosts: staging_host 
    roles: 
    - nginx 
    tasks: 
    - name: update apt cache 
     apt: update_cache=yes cache_valid_time=3600 
    # .... 
0

我有同樣的問題,直到我意識到,你還需要在遠程主機上安裝python,以及你自己的本地機器。現在它工作!