mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.4 KiB
ReStructuredText
69 lines
2.4 KiB
ReStructuredText
8 years ago
|
.. _pb-py-compat:
|
||
|
|
||
5 years ago
|
********************
|
||
|
Python3 in templates
|
||
|
********************
|
||
8 years ago
|
|
||
3 years ago
|
Ansible uses Jinja2 to take advantage of Python data types and standard functions in templates and variables.
|
||
5 years ago
|
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.
|
||
8 years ago
|
|
||
5 years ago
|
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.
|
||
8 years ago
|
|
||
|
.. _pb-py-compat-dict-views:
|
||
|
|
||
5 years ago
|
Dictionary views
|
||
|
================
|
||
8 years ago
|
|
||
|
In Python2, the :meth:`dict.keys`, :meth:`dict.values`, and :meth:`dict.items`
|
||
5 years ago
|
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
|
||
8 years ago
|
into a list by Ansible. It is, however, easy to make this portable by
|
||
4 years ago
|
using the :func:`list <jinja2:jinja-filters.list>` filter whenever using :meth:`dict.keys`,
|
||
3 years ago
|
:meth:`dict.values`, or :meth:`dict.items`.
|
||
|
|
||
|
.. code-block:: yaml+jinja
|
||
8 years ago
|
|
||
|
vars:
|
||
|
hosts:
|
||
|
testhost1: 127.0.0.2
|
||
|
testhost2: 127.0.0.3
|
||
|
tasks:
|
||
|
- debug:
|
||
|
msg: '{{ item }}'
|
||
|
# Only works with Python 2
|
||
7 years ago
|
#loop: "{{ hosts.keys() }}"
|
||
8 years ago
|
# Works with both Python 2 and Python 3
|
||
7 years ago
|
loop: "{{ hosts.keys() | list }}"
|
||
8 years ago
|
|
||
|
.. _pb-py-compat-iteritems:
|
||
|
|
||
|
dict.iteritems()
|
||
5 years ago
|
================
|
||
|
|
||
|
Python2 dictionaries have :meth:`~dict.iterkeys`, :meth:`~dict.itervalues`, and :meth:`~dict.iteritems` methods.
|
||
8 years ago
|
|
||
3 years ago
|
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.
|
||
|
|
||
|
.. code-block:: yaml+jinja
|
||
8 years ago
|
|
||
|
vars:
|
||
|
hosts:
|
||
|
testhost1: 127.0.0.2
|
||
|
testhost2: 127.0.0.3
|
||
|
tasks:
|
||
|
- debug:
|
||
|
msg: '{{ item }}'
|
||
|
# Only works with Python 2
|
||
7 years ago
|
#loop: "{{ hosts.iteritems() }}"
|
||
8 years ago
|
# Works with both Python 2 and Python 3
|
||
7 years ago
|
loop: "{{ hosts.items() | list }}"
|
||
8 years ago
|
|
||
|
.. seealso::
|
||
|
* The :ref:`pb-py-compat-dict-views` entry for information on
|
||
4 years ago
|
why the :func:`list filter <jinja2:jinja-filters.list>` is necessary
|
||
8 years ago
|
here.
|