Ansible 2.5 Porting Guide

    It is intended to assist in updating your playbooks, plugins and other parts of your Ansible infrastructure so they will work with this version of Ansible.

    We suggest you read this page along with to understand what updates you may need to make.

    This document is part of a collection on porting. The complete list of porting guides can be found at porting guides.

    In Ansible version 2.4, the concept of dynamic includes () versus static imports (import_tasks) was introduced to clearly define the differences in how include works between dynamic and static includes.

    All attributes applied to a dynamic include* would only apply to the include itself, while attributes applied to astatic import* would be inherited by the tasks within.

    This separation was only partially implemented in Ansible version 2.4. As of Ansible version 2.5, this work is complete and the separation now behaves as designed; attributes applied to an include_* task will not be inherited by the tasks within.

    To achieve an outcome similar to how Ansible worked prior to version 2.5, playbooks should use an explicit application of the attribute on the needed tasks, or use blocks to apply the attribute to many tasks. Another option is to use a static import_* when possible instead of a dynamic task.

    OLD In Ansible 2.4:

    Included file:

    1. - block:
    2. - debug:
    3. msg: "In included file"
    4.  
    5. - apt:
    6. name: nginx
    7. state: latest

    NEW In Ansible 2.5:

    Including task:

    1. - include_tasks: "{{ ansible_distribution }}.yml"
    2. tags:
    3. - distro_include

    Included file:

    1. - block:
    2. - debug:
    3. msg: "In included file"
    4.  
    5. - apt:
    6. name: nginx
    7. state: latest
    8. tags:
    9. - distro_include

    The relevant change in those examples is, that in Ansible 2.5, the included file defines the tag distro_include again. The tag is not inherited automatically.

    Fixed handling of keywords and inline variables

    We made several fixes to how we handle keywords and ‘inline variables’, to avoid conflating the two. Unfortunately these changes mean you must specify whether name is a keyword or a variable when calling roles. If you have playbooks that look like this:

    1. roles:
    2. - { role: myrole, name: Justin, othervar: othervalue, become: True}

    You will run into errors because Ansible reads name in this context as a keyword. Beginning in 2.5, if you want to use a variable name that is also a keyword, you must explicitly declare it as a variable for the role:

    1. roles:
    2. - { role: myrole, vars: {name: Justin, othervar: othervalue}, become: True}

    For a full list of keywords see ::ref::Playbook Keywords.

    Migrating from with_X to loop

    With the release of Ansible 2.5, the recommended way to perform loops is the use the new loop keyword instead of with_X style loops.

    In many cases, loop syntax is better expressed using filters instead of more complex use of query or lookup.

    The following examples will show how to convert many common with_ style loops to loop and filters.

    with_list

    with_list is directly replaced by loop.

    1. - name: with_list
    2. debug:
    3. msg: "{{ item }}"
    4. with_list:
    5. - one
    6. - two
    7.  
    8. - name: with_list -> loop
    9. debug:
    10. msg: "{{ item }}"
    11. loop:
    12. - one
    13. - two

    with_items

    with_items is replaced by loop and the flatten filter.

    1. - name: with_items
    2. debug:
    3. msg: "{{ item }}"
    4. with_items: "{{ items }}"
    5. - name: with_items -> loop
    6. debug:
    7. msg: "{{ item }}"
    8. loop: "{{ items|flatten(levels=1) }}"

    with_indexed_items

    with_indexed_items is replaced by loop, the flatten filter and loop_control.index_var.

    with_flattened

    with_flattened is replaced by loop and the flatten filter.

    1. - name: with_flattened
    2. debug:
    3. msg: "{{ item }}"
    4. with_flattened: "{{ items }}"
    5.  
    6. - name: with_flattened -> loop
    7. debug:
    8. msg: "{{ item }}"
    9. loop: "{{ items|flatten }}"

    with_together

    1. - name: with_together
    2. debug:
    3. msg: "{{ item.0 }} - {{ item.1 }}"
    4. with_together:
    5. - "{{ list_one }}"
    6. - "{{ list_two }}"
    7.  
    8. - name: with_together -> loop
    9. debug:
    10. msg: "{{ item.0 }} - {{ item.1 }}"
    11. loop: "{{ list_one|zip(list_two)|list }}"

    with_dict

    with_dict can be substituted by loop and either the dictsort or dict2items filters.

    1. - name: with_dict
    2. debug:
    3. msg: "{{ item.key }} - {{ item.value }}"
    4. with_dict: "{{ dictionary }}"
    5.  
    6. - name: with_dict -> loop (option 1)
    7. debug:
    8. msg: "{{ item.key }} - {{ item.value }}"
    9. loop: "{{ dictionary|dict2items }}"
    10.  
    11. - name: with_dict -> loop (option 2)
    12. debug:
    13. msg: "{{ item.0 }} - {{ item.1 }}"
    14. loop: "{{ dictionary|dictsort }}"

    with_sequence is replaced by loop and the range function, and potentially the filter.

    1. - name: with_sequence
    2. debug:
    3. msg: "{{ item }}"
    4. with_sequence: start=0 end=4 stride=2 format=testuser%02x
    5.  
    6. - name: with_sequence -> loop
    7. debug:
    8. msg: "{{ 'testuser%02x' | format(item) }}"
    9. # range is exclusive of the end point
    10. loop: "{{ range(0, 4 + 1, 2)|list }}"

    with_subelements

    with_subelements is replaced by loop and the subelements filter.

    1. - name: with_subelements
    2. debug:
    3. msg: "{{ item.0.name }} - {{ item.1 }}"
    4. with_subelements:
    5. - "{{ users }}"
    6. - mysql.hosts
    7.  
    8. - name: with_subelements -> loop
    9. debug:
    10. msg: "{{ item.0.name }} - {{ item.1 }}"
    11. loop: "{{ users|subelements('mysql.hosts') }}"

    with_nested/with_cartesian

    with_nested and with_cartesian are replaced by loop and the product filter.

    1. - name: with_nested
    2. debug:
    3. msg: "{{ item.0 }} - {{ item.1 }}"
    4. with_nested:
    5. - "{{ list_one }}"
    6. - "{{ list_two }}"
    7.  
    8. - name: with_nested -> loop
    9. debug:
    10. msg: "{{ item.0 }} - {{ item.1 }}"
    11. loop: "{{ list_one|product(list_two)|list }}"

    with_random_choice

    with_random_choice is replaced by just use of the random filter, without need of loop.

    1. - name: with_random_choice
    2. debug:
    3. msg: "{{ item }}"
    4.  
    5. - name: with_random_choice -> loop (No loop is needed here)
    6. debug:
    7. msg: "{{ my_list|random }}"
    8. tags: random

    Deprecated

    Jinja tests used as filters

    Using Ansible-provided jinja tests as filters will be removed in Ansible 2.9.

    Prior to Ansible 2.5, jinja tests included within Ansible were most often used as filters. The large difference in use is that filters are referenced as variable | filter_name while jinja tests are referenced as variable is test_name.

    Jinja tests are used for comparisons, while filters are used for data manipulation and have different applications in jinja. This change is to help differentiate the concepts for a better understanding of jinja, and where each can be appropriately used.

    As of Ansible 2.5, using an Ansible provided jinja test with filter syntax, will display a deprecation error.

    OLD In Ansible 2.4 (and earlier) the use of an Ansible included jinja test would likely look like this:

    NEW In Ansible 2.5 it should be changed to look like this:

    1. when:
    2. - result is failed
    3. - results is not successful

    In addition to the deprecation warnings, many new tests have been introduced that are aliases of the old tests. These new tests make more sense grammatically with the jinja test syntax, such as the new successful test which aliases success.

    1. when: result is successful

    See for more information.

    Additionally, a script was created to assist in the conversion for tests using filter syntax to proper jinja test syntax. This script has been used to convert all of the Ansible integration tests to the correct format. There are a few limitations documented, and all changes made by this script should be evaluated for correctness before executing the modified playbooks. The script can be found at https://github.com/ansible/ansible/blob/devel/hacking/fix_test_syntax.py.

    Ansible fact namespacing

    Ansible facts, which have historically been written to names like ansible_in the main facts namespace, have been placed in their own new namespace,ansible_facts. For example, the fact ansible_distribution is now bestqueried through the variable structure ansible_facts.distribution.

    A new configuration variable, injectfacts_as_vars, has been added toansible.cfg. Its default setting, ‘True’, keeps the 2.4 behavior of factsvariables being set in the old ansible locations (while also writing themto the new namespace). This variable is expected to be set to ‘False’ in afuture release. When inject_facts_as_vars is set to False, you mustrefer to ansible_facts through the new ansible_facts. namespace.

    Major changes in popular modules are detailed here.

    github_release

    In Ansible versions 2.4 and older, after creating a GitHub release using the create_release state, the github_release module reported state as skipped.In Ansible version 2.5 and later, after creating a GitHub release using the create_release state, the github_release module now reports state as changed.

    Modules removed

    The following modules no longer exist:

    Note

    These modules may no longer have documentation in the current release. Please see theAnsible 2.4 module documentation if you needto know how they worked for porting your playbooks.

    Deprecation notices

    The following modules will be removed in Ansible 2.9. Please update your playbooks accordingly.

    • The stat and modules have changed the default of the option get_md5 from true to false.

    This option will be removed starting with Ansible version 2.9. The options get_checksum: Trueand checksum_algorithm: md5 can still be used if an MD5 checksum isdesired.

    • osx_say module was renamed into say.
    • Several modules which could deal with symlinks had the default value of their follow optionchanged as part of a feature to :
      • The file module changed from to follow=True becauseits purpose is to modify the attributes of a file and most systems do not allow attributes to beapplied to symlinks, only to real files.
      • The had its follow parameter removed because itinherently modifies the content of an existing file so it makes no sense to operate on the linkitself.
      • The blockinfile module had its follow parameter removed becauseit inherently modifies the content of an existing file so it makes no sense to operate on thelink itself.
      • In Ansible-2.5.3, the became more strict about itssrc file being proper utf-8. Previously, non-utf8 contents in a template module src filewould result in a mangled output file (the non-utf8 characters would be replaced with a unicodereplacement character). Now, on Python2, the module will error out with the message, “Templatesource files must be utf-8 encoded”. On Python3, the module will first attempt to pass thenon-utf8 characters through verbatim and fail if that does not succeed.

    Plugins

    Inventory

    Inventory plugins have been fine tuned, and we have started to add some common features:

    • The ability to use a cache plugin to avoid costly API/DB queries is disabled by default.If using inventory scripts, some may already support a cache, but it is outside of Ansible’s knowledge and control.Moving to the internal cache will allow you to use Ansible’s existing cache refresh/invalidation mechanisms.
    • A new ‘auto’ plugin, enabled by default, that can automatically detect the correct plugin to use IF that plugin is using our ‘common YAML configuration format’.The previous host_list, script, yaml and ini plugins still work as they did, the auto plugin is now the last one we attempt to use.If you had customized the enabled plugins you should revise the setting to include the new auto plugin.

    Shell

    Shell plugins have been migrated to the new plugin configuration framework. It is now possible to customize more settings, and settings which were previously ‘global’ can now also be overriden using host specific variables.

    For example, system_temps is a new setting that allows you to control what Ansible will consider a ‘system temporary dir’. This is used when escalating privileges for a non-administrative user. Previously this was hardcoded to ‘/tmp’, which some systems cannot use for privilege escalation. This setting now defaults to [ '/var/tmp', '/tmp'].

    Another new setting is admin_users which allows you to specify a list of users to be considered ‘administrators’. Previously this was hardcoded to root. It now it defaults to [root, toor, admin]. This information is used when choosing between your remote_temp and system_temps directory.

    For a full list, check the shell plugin you are using, the default shell plugin is sh.

    Those that had to work around the global configuration limitations can now migrate to a per host/group settings,but also note that the new defaults might conflict with existing usage if the assumptions don’t correlate to your environment.

    Filter

    The lookup plugin API now throws an error if a non-iterable value is returned from a plugin. Previously, numbers orother non-iterable types returned by a plugin were accepted without error or warning. This change was made because plugins should always return a list. Please note that plugins that return strings and other non-list iterable values will not throw an error, but may cause unpredictable behavior. If you have a custom lookup plugin that does not return a list, you should modify it to wrap the return values in a list.

    Lookup

    A new option was added to lookup plugins globally named error which allows you to control how errors produced by the lookup are handled, before this option they were always fatal. Valid values for this option are warn, ignore and strict. See the lookup page for more details.

    No notable changes.

    Network

    Expanding documentation

    We’re expanding the network documentation. There’s new content and a . We will continue to build the network-related documentation moving forward.

    Top-level connection arguments will be removed in 2.9

    Top-level connection arguments like username, host, and password are deprecated and will be removed in version 2.9.

    OLD In Ansible < 2.4

    1. - name: example of using top-level options for connection properties
    2. ios_command:
    3. commands: show version
    4. host: "{{ inventory_hostname }}"
    5. username: cisco
    6. password: cisco
    7. authorize: yes
    8. auth_pass: cisco

    The deprecation warnings reflect this schedule. The task above, run in Ansible 2.5, will result in:

    1. [DEPRECATION WARNING]: Param 'username' is deprecated. See the module docs for more information. This feature will be removed in version
    2. 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
    3. [DEPRECATION WARNING]: Param 'password' is deprecated. See the module docs for more information. This feature will be removed in version
    4. 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
    5. [DEPRECATION WARNING]: Param 'host' is deprecated. See the module docs for more information. This feature will be removed in version 2.9.
    6. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

    We recommend using the new connection types network_cli and netconf (see below), using standard Ansible connection properties, and setting those properties in inventory by group. As you update your playbooks and inventory files, you can easily make the change to become for privilege escalation (on platforms that support it). For more information, see the using become with network modules guide and the .

    Adding persistent connection types network_cli and netconf

    Ansible 2.5 introduces two top-level persistent connection types, network_cli and netconf. With connection: local, each task passed the connection parameters, which had to be stored in your playbooks. With network_cli and netconf the playbook passes the connection parameters once, so you can pass them at the command line if you prefer. We recommend you use network_cli and netconf whenever possible.Note that eAPI and NX-API still require local connections with provider dictionaries. See the platform documentation for more information. Unless you need a local connection, update your playbooks to use network_cli or netconf and to specify your connection variables with standard Ansible connection variables:

    OLD In Ansible 2.4

    1. ---
    2. vars:
    3. cli:
    4. host: "{{ inventory_hostname }}"
    5. username: operator
    6. password: secret
    7. transport: cli
    8.  
    9. tasks:
    10. - nxos_config:
    11. src: config.j2
    12. provider: "{{ cli }}"
    13. username: admin
    14. password: admin

    NEW In Ansible 2.5

    1. [nxos:vars]
    2. ansible_connection=network_cli
    3. ansible_network_os=nxos
    4. ansible_user=operator
    5. ansible_password=secret
    1. tasks:
    2. - nxos_config:
    3. src: config.j2

    Using a provider dictionary with either network_cli or netconf will result in a warning.

    Developers: Shared Module Utilities Moved

    Beginning with Ansible 2.5, shared module utilities for network modules moved to ansible.module_utils.network.

    • Platform-independent utilities are found in

    If your module uses shared module utilities, you must update all references. For example, change:

    OLD In Ansible 2.4

    NEW In Ansible 2.5

    1. from ansible.module_utils.network.vyos.vyos import get_config, load_config

    See the module utilities developer guide see for more information.