首页 文章

Ansible失败了/ bin / sh:1:/ usr / bin / python:找不到

提问于
浏览
150

我遇到了一个我以前从未见过的错误 . 这是命令和错误:

$ 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

这是hosts文件:

[api]
104.55.47.224

我可以删除角色部分,它不会进入第一个任务,它将使它只会到达 /bin/sh: 1: /usr/bin/python: not found 行 . 这可能会发生什么?


注意:如果有人正在ping IP地址并且没有得到响应,您应该知道自从粘贴代码后我已经更改了IP地址 .

EDIT python是在本地安装的,问题是它没有安装在运行Ubuntu 15.04的远程机器上

16 回答

  • 5

    我偶然发现在 Ubuntu 15.10 服务器上运行ansible时出现此错误,因为它随附 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
    
      tasks:
        - name: 'ensure user {{ project_name }} exists'
          user: name={{ project_name }} state=present
    
    • Don't forget the -y (says yes to all questions) option with apt-get (或原始模块将无声地卡住)

    • gather_facts: no line is also critical (因为我们不能在没有python的情况下收集事实)

  • 97

    Ansible 2.2具有Python 3支持的技术预览 . To take advantage of this(所以你不必在Ubuntu 16.04上安装Python 2),只需将 ansible_python_interpreter config选项设置为 /usr/bin/python3 即可 . 这可以在您的库存文件中基于每个主机完成:

    [db]
    123.123.123.123 ansible_python_interpreter=/usr/bin/python3
    
  • 8

    Solution 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
    

    Solution 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
    
  • 0

    您可以使用raw module在远程主机上安装Python:

    - raw: sudo apt-get install python-simplejson
    
  • 13

    总结其他人的答案,以下是适合我的组合设置:

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

    你需要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

  • 150

    我曾经在新鲜的数字海洋液滴上使用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
    

    对于ubuntu 16.04在新的OVH SSD上,我必须在python2包可用之前进行apt-get升级 .

  • 1

    我发现实际上可以在一个剧本中进行多次播放,所以我的设置现在包含一个在所有主机上运行的"dependency provisioning"播放,以及针对特定主机的其他播放 . 所以没有 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
      # ....
    
  • 67

    正如其他人所说,这是由于缺少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在启动过程中很晚才执行,所以在ssh可用之后仍然可能没有安装python2 . 所以我添加了一个暂停,以防刚刚创建实例:

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

    这将完美地完成工作,并且作为一个优势,您不会在每次运行时检查python2,并且您不必再做任何变通方法来收集事实 .

    我确信其他 Cloud 提供商提供类似的CloudInit功能,因此请根据您的使用情况进行调整 .

  • 17

    根据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.
    
  • -2

    很多答案..感谢您发布,因为我也从这个页面开始!

    我做了一些挖掘,它与Ubuntu 14.04LTS一样稳固,Ubuntu 15.04LTS似乎已经放弃了最新的 python ,而Ubuntu 16.04LTS似乎已经下降 aptitude .

    在进行任何 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 ,请随意剥离它 .

    资料来源:

  • 1

    我能够通过在目标机器上安装Python来解决同样的问题,即我们想要SSH的机器 . 我使用了以下命令:

    sudo apt-get install python-minimal
    
  • 1

    默认情况下,Ansible requires Python 2,然而,Ansible 2.2+ can work with Python 3也是如此 .

    所以要么使用raw module安装Python 2,例如

    ansible localhost --sudo -m raw -a "yum install -y python2 python-simplejson"
    

    或在库存文件中设置ansible_python_interpreter variable,如:

    [local]
    localhost ansible_python_interpreter="env python3"
    

    对于Docker,您可以添加以下行:

    RUN printf '[local]\r\nlocalhost ansible_python_interpreter="env python3"\r\n' > /etc/ansible/hosts
    

    或者运行它:

    ansible-playbook /ansible/provision.yml -e 'ansible_python_interpreter=/usr/bin/python3' -c local
    
  • 1

    @Miroslav,谢谢你指出我正确的方向 . 我在 ec2_instance 模块中也使用了 user_data ,它就像一个款待 .

    - name: Creating single EC2 instance 
      ec2_instance:
        region: "{{ aws_region }}"
        key_name: "{{ aws_ec2_key_pair }}"
        name: "some-cool-name"
        instance_type: t1.micro
        image_id: ami-d38a4ab1
        security_group: sg-123456
        vpc_subnet_id: sn-678901234
        network:
            assign_public_ip: no
        volumes:
          - device_name: /dev/sda1
            ebs:
              volume_type: gp2
              volume_size: 15
        user_data: |
          #!/bin/bash
          #
          apt update
          apt install -y python-simplejson              
        termination_protection: yes
        wait: yes
    
  • 1

    我有同样的问题,直到我意识到你需要在远程主机以及您自己的本地计算机上安装python . 现在它的作品!

  • 11

    我们碰到这个 .

    我们在流浪汉上部署ubuntu 16.04,所以如果你不使用流浪汉,我的评论毫无意义 .

    我们安装了以下vagrant插件(触发器,shell-commander),我们在机器上安装了python 2.7.6(不是没有thioose插件)并且在ansible可以部署之后

    这是我们的最后一次测试,否则我们将把这个安装包含在Vagrant文件的shell命令中

    希望它可以帮助某人

相关问题