Set ansible_python_interpreter at Task level?

Is there a way to do this? I have a playbook with two tasks. The first task uses Yum module (and python 2 interpreter). The next task uses aws_s3 module (and python3 interpreter), but fails because the playbook is still trying to use python2 interpreter. (the error is: can’t find boto3…)

I tested this by using two separate Playbooks and declaring the appropriate python interpreter at the Playbook level:

  • hosts: ec2lin remote_user: ec2-user become: yes become_user: root vars: ansible_python_interpreter: /usr/bin/python3

Each task worked in a separate playbook. but I would rather use a single playbook for both Tasks.

Is it possible to set the “ansible_python_interpreter” at the Task level - With a keyword or variable or something?

Thank You

Yes, just do the same vars directive on the task like you have done on the play, e.g.

`

  • name: my task
    ping:
    vars:
    ansible_python_interpreter: /usr/bin/python

`

You would have to keep in mind variable precedence [1], task vars do beat play vars but the following will beat task vars

  • Vars added with include_vars:
  • Vars/facts added with set_fact or register
  • Extra vars specified by ‘-e’ when Ansible was invoked

[1] - https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

Thanks

Jordan

Thank you - I did that and it did use the right interpreter, but written this way the play has a new error:

“missing required arguments: bucket, mode”}

This is the new play with the interpreter var:

  • name: downlod file from s3 with aws_s3 module aws_s3: vars: ansible_python_interpreter: /usr/bin/python3 bucket: launch-data object: jre-8u201-linux-x64.tar.gz dest: /home/ec2-user/updater/jre-8u201-linux-x64.tar.gz mode: get

Is my syntax incorrect?

Got it working - I had to put the vars before the module name - thanks for help

`vars` is a 'task keyword' different from module options, the problem
was that you mixed both. This is an indentation issue, not order
(keywords can appear in any order).