Set ansible_python_interpreter at Task level?

11,558 views
Skip to first unread message

gigi...@gmail.com

unread,
Mar 25, 2019, 3:36:50 PM3/25/19
to Ansible Project

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 

Jordan Borean

unread,
Mar 25, 2019, 3:51:58 PM3/25/19
to Ansible Project
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


Thanks

Jordan

gigi...@gmail.com

unread,
Mar 25, 2019, 5:29:13 PM3/25/19
to Ansible Project
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?

gigi...@gmail.com

unread,
Mar 25, 2019, 5:33:18 PM3/25/19
to Ansible Project
Got it working - I had to put the vars before the module name - thanks for help 

Brian Coca

unread,
Apr 9, 2019, 10:47:30 AM4/9/19
to Ansible Project
`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).

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages