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

后端 未结 19 1066
执念已碎
执念已碎 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.

提交回复
热议问题