2017-04-10 20:22:06 +02:00
.. _pb-py-compat:
2020-01-21 22:57:55 +01:00
***** ***** ***** *****
Python3 in templates
***** ***** ***** *****
2017-04-10 20:22:06 +02:00
2020-01-21 22:57:55 +01:00
Ansible uses Jinja2 to leverage Python data types and standard functions in templates and variables.
You can use these data types and standard functions to perform a rich set of operations on your data. However,
if you use templates, you must be aware of differences between Python versions.
2017-04-10 20:22:06 +02:00
2020-01-21 22:57:55 +01:00
These topics help you design templates that work on both Python2 and Python3. They might also help if you are upgrading from Python2 to Python3. Upgrading within Python2 or Python3 does not usually introduce changes that affect Jinja2 templates.
2017-04-10 20:22:06 +02:00
.. _pb-py-compat-dict-views:
2020-01-21 22:57:55 +01:00
Dictionary views
================
2017-04-10 20:22:06 +02:00
In Python2, the :meth: `dict.keys` , :meth: `dict.values` , and :meth: `dict.items`
2020-01-21 22:57:55 +01:00
methods return a list. Jinja2 returns that to Ansible via a string
representation that Ansible can turn back into a list.
In Python3, those methods return a :ref: `dictionary view <python3:dict-views>` object. The
string representation that Jinja2 returns for dictionary views cannot be parsed back
2017-04-19 02:05:28 +02:00
into a list by Ansible. It is, however, easy to make this portable by
2021-05-12 18:18:31 +02:00
using the :func: `list <jinja2:jinja-filters.list>` filter whenever using :meth: `dict.keys` ,
2017-04-10 20:22:06 +02:00
:meth: `dict.values` , or :meth: `dict.items` ::
vars:
hosts:
testhost1: 127.0.0.2
testhost2: 127.0.0.3
tasks:
- debug:
msg: '{{ item }}'
# Only works with Python 2
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
#loop: "{{ hosts.keys() }}"
2017-04-10 20:22:06 +02:00
# Works with both Python 2 and Python 3
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
loop: "{{ hosts.keys() | list }}"
2017-04-10 20:22:06 +02:00
.. _pb-py-compat-iteritems:
dict.iteritems()
2020-01-21 22:57:55 +01:00
================
Python2 dictionaries have :meth: `~dict.iterkeys` , :meth: `~dict.itervalues` , and :meth: `~dict.iteritems` methods.
2017-04-10 20:22:06 +02:00
2020-01-21 22:57:55 +01:00
Python3 dictionaries do not have these methods. Use :meth: `dict.keys` , :meth: `dict.values` , and :meth: `dict.items` to make your playbooks and templates compatible with both Python2 and Python3::
2017-04-10 20:22:06 +02:00
vars:
hosts:
testhost1: 127.0.0.2
testhost2: 127.0.0.3
tasks:
- debug:
msg: '{{ item }}'
# Only works with Python 2
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
#loop: "{{ hosts.iteritems() }}"
2017-04-10 20:22:06 +02:00
# Works with both Python 2 and Python 3
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
loop: "{{ hosts.items() | list }}"
2017-04-10 20:22:06 +02:00
.. seealso ::
* The :ref: `pb-py-compat-dict-views` entry for information on
2021-05-12 18:18:31 +02:00
why the :func: `list filter <jinja2:jinja-filters.list>` is necessary
2017-04-10 20:22:06 +02:00
here.