2017-04-10 18:22:06 +00:00
|
|
|
.. _pb-py-compat:
|
|
|
|
|
|
|
|
Python Version and Templating
|
|
|
|
=============================
|
|
|
|
|
|
|
|
Jinja2 templates leverage Python data types and standard functions. This
|
|
|
|
makes for a rich set of operations that can be performed on data. However,
|
|
|
|
this also means that certain specifics of the underlying Python becomes
|
|
|
|
visible to template authors. Since Ansible playbooks use Jinja2 for templates
|
|
|
|
and variables, this means that playbook authors need to be aware of these
|
|
|
|
specifics as well.
|
|
|
|
|
|
|
|
Unless otherwise noted, these differences are only of interest when running
|
|
|
|
Ansible in Python2 versus Python3. Changes within Python2 and Python3 are
|
|
|
|
generally small enough that they are not visible at the jinja2 level.
|
|
|
|
|
|
|
|
.. _pb-py-compat-dict-views:
|
|
|
|
|
|
|
|
Dictionary Views
|
|
|
|
----------------
|
|
|
|
|
|
|
|
In Python2, the :meth:`dict.keys`, :meth:`dict.values`, and :meth:`dict.items`
|
|
|
|
methods returns a list. Jinja2 returns that to Ansible via a string
|
|
|
|
representation that Ansible can turn back into a list. In Python3, those
|
2017-04-19 00:05:28 +00:00
|
|
|
methods return a :ref:`dictionary view <python3:dict-views>` object. The
|
|
|
|
string representation that Jinja2 returns for dictionary views cannot be parsed back
|
|
|
|
into a list by Ansible. It is, however, easy to make this portable by
|
2017-04-10 18:22:06 +00:00
|
|
|
using the :func:`list <jinja2:list>` filter whenever using :meth:`dict.keys`,
|
|
|
|
: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 03:32:34 +00:00
|
|
|
#loop: "{{ hosts.keys() }}"
|
2017-04-10 18:22:06 +00: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 03:32:34 +00:00
|
|
|
loop: "{{ hosts.keys() | list }}"
|
2017-04-10 18:22:06 +00:00
|
|
|
|
|
|
|
.. _pb-py-compat-iteritems:
|
|
|
|
|
|
|
|
dict.iteritems()
|
|
|
|
----------------
|
|
|
|
|
|
|
|
In Python2, dictionaries have :meth:`~dict.iterkeys`,
|
|
|
|
:meth:`~dict.itervalues`, and :meth:`~dict.iteritems` methods. These methods
|
|
|
|
have been removed in Python3. Playbooks and Jinja2 templates should use
|
|
|
|
:meth:`dict.keys`, :meth:`dict.values`, and :meth:`dict.items` in order to be
|
|
|
|
compatible with both Python2 and Python3::
|
|
|
|
|
|
|
|
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 03:32:34 +00:00
|
|
|
#loop: "{{ hosts.iteritems() }}"
|
2017-04-10 18:22:06 +00: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 03:32:34 +00:00
|
|
|
loop: "{{ hosts.items() | list }}"
|
2017-04-10 18:22:06 +00:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
* The :ref:`pb-py-compat-dict-views` entry for information on
|
|
|
|
why the :func:`list filter <jinja2:list>` is necessary
|
|
|
|
here.
|