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

后端 未结 19 926
执念已碎
执念已碎 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:33

    Solution 1:

    If you're using Ansible >2.2.0, you can set the ansible_python_interpreter configuration option to /usr/bin/python3:

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

    or in your inventory file:

    [ubuntu_hosts]
    <xxx.xxx.xxx.xxx>
    
    [ubuntu_hosts:vars]
    ansible_python_interpreter=/usr/bin/python3
    

    Solution 2:

    If you're using Ansible <2.2.0 then you can add these pre_tasks to your playbook:

    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 With ansible 2.8.x, you don't need to worry about it, it's working out of the box for python > 3.5 for both controller and target machine(s)

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

    You can use the raw module to install Python on the remote hosts:

    - raw: sudo apt-get install python-simplejson
    
    0 讨论(0)
  • 2020-11-29 15:35

    By default, Ansible requires Python 2, however, Ansible 2.2+ can work with Python 3 as well.

    So either install Python 2 using the raw module, e.g.

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

    or set ansible_python_interpreter variable in the inventory file, like:

    [local]
    localhost ansible_python_interpreter="env python3"
    

    For Docker, you can add the following line:

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

    or run it as:

    ansible-playbook /ansible/provision.yml -e 'ansible_python_interpreter=/usr/bin/python3' -c local
    
    0 讨论(0)
  • 2020-11-29 15:35

    I had the same issue, until I realised you also need to install python on the remote host as well as your own local machine. now it works!

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

    Ansible 2.2 features a tech preview of Python 3 support. To take advantage of this (so you don't have to install Python 2 on Ubuntu 16.04), just set the ansible_python_interpreter config option to /usr/bin/python3. This can be done on a per-host basis in your inventory file:

    [db]
    123.123.123.123 ansible_python_interpreter=/usr/bin/python3
    
    0 讨论(0)
  • 2020-11-29 15:39

    @Miroslav, thanks for pointing me in the right direction. I used user_data in the ec2_instance module too and it works like a treat.

    I.e.

    - 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     
    
    0 讨论(0)
提交回复
热议问题