title: how can I specify the version of Python to use in an Ansible playbook?
body: <p>I'm working on a project that is still running Python 2.  I'm trying to use Ansible to set up new test servers.  The base Linux installation that I start with only has Python 3, so I need my very first "bootstrap" playbook to use Python 3, but then I want subsequent playbooks to use Python 2.</p>
  
  <p>I can specify the version of python in my inventory file like this:</p>
  
  <pre><code>[test_server:vars]
  ansible_python_interpreter=/usr/bin/python3
  
  [test_server]
  test_server.example.com
  </code></pre>
  
  <p>But then I have to go edit the inventory file to make sure that I'm using Python 3 for the bootstrap playbook, and then edit it again for the rest of my playbooks.  That seems odd.  I've tried a few different versions of changing <code>ansible_python_interpreter</code> within my playbooks, like</p>
  
  <pre><code>- hosts: test_server
      ansible_python_interpreter: /usr/bin/python
  </code></pre>
  
  <p>and </p>
  
  <pre><code>- hosts: test_server
    tasks:
      - name: install pip
        ansible_python_interpreter: /usr/bin/python
        apt:
          name: python-pip
  </code></pre>
  
  <p>but ansible complains that </p>
  
  <blockquote>
    <p>ERROR! 'ansible_python_interpreter' is not a valid attribute for a Task</p>
  </blockquote>
  
  <p>Even though <a href="https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html" rel="noreferrer">https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html</a> says that </p>
  
  <blockquote>
    <p>You can still set ansible_python_interpreter to a specific path at any variable level (for example, in host_vars, in vars files, in playbooks, etc.). </p>
  </blockquote>
  
  <p>What's the invocation to do this correctly?</p>