Ansible fails with /bin/sh: 1: /usr/bin/python: not found

后端 未结 19 929
执念已碎
执念已碎 2020-11-29 15:15

I\'m running into an error I\'ve never seen before. Here is the command and the error:

$ ansible-playbook create_api.yml

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


        
相关标签:
19条回答
  • 2020-11-29 15:40

    As others said, this is due to missing python2. Other answers here provide a workaround with pre_tasks and gather_facts: no, however if you're on EC2 and you spin up the instance with ansible you can use user_data option:

    - 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
    

    Then people usually wait for ssh to be available like this:

      - 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
    

    However I've found, that this isn't always long enough as CloudInit is executed quite late in the boot process so the python2 still might not be installed right after ssh is available. So I've added a pause in case the instance was just created:

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

    This will do the job perfectly and as an advantage you're not checking for python2 on every run and you don't have to do any workarounds to gather facts later.

    I'm sure other cloud providers provide similar CloudInit functionality, so adapt for your use case.

    0 讨论(0)
  • 2020-11-29 15:45

    According to this Gist you can install Python2 on Ubuntu 16.04 as follows:

    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.
    
    0 讨论(0)
  • 2020-11-29 15:47

    To summarize everyone else's answers, here are the combined settings that worked for me:

     - 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
    
    0 讨论(0)
  • 2020-11-29 15:48

    We just run into this.

    We deploy ubuntu 16.04 on a vagrant so if you are not using vagrant my comment is pointless.

    We installed the following vagrant plugins (trigger, shell-commander) and we get python 2.7.6 installed on the machine (which were not without thioose plugins) and after ansible can deploy

    It was our last test, otherwise we were about to include this installation in a shell command in the Vagrant file

    Hope it can help someone

    0 讨论(0)
  • 2020-11-29 15:49

    I found out that it's actually possible to have multiple plays in a single playbook, so my setup now contains a "dependency provisioning" play which runs on all hosts, and other plays for specific hosts. So no more pre_tasks.

    For example:

    - 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 讨论(0)
  • 2020-11-29 15:49

    I was able to fix the same problem by installing Python on target machine i.e. the machine which we want to SSH to. I had used following command:

    sudo apt-get install python-minimal
    
    0 讨论(0)
提交回复
热议问题