#85849·ansible

[2.19] variable in delegate_to evaluated before task condition

Author: k-c-pCreated Sep 12, 2025Updated Sep 14, 2026
Labelsbugneeds_verifiedaffects_2.19

Summary

An Ansible 2.19 a variable used in delegate_to seems to be evaluated even if the same var is used in a condition on the task and the task should be skipped. This results in an "undefined error" instead of the task being skipped as it was done in Ansible 2.18

Issue Type

Bug Report

Component Name

delegate_to

Ansible Version

$ ansible --version
ansible [core 2.19.2]
  config file = /home/charly/eclipseprojects/spp_playground/ansible.cfg
  configured module search path = ['/opt/ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible/lib/python3.13/site-packages/ansible
  ansible collection location = /opt/ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.13.7 (main, Aug 20 2025, 22:17:40) [GCC 14.3.0] (/opt/ansible/bin/python3)
  jinja version = 3.1.6
  pyyaml version = 6.0.2 (with libyaml v0.2.5)

Configuration

$ ansible-config dump --only-changed -t all
ANSIBLE_HOME(env: ANSIBLE_HOME) = /opt/ansible
CALLBACKS_ENABLED(env: ANSIBLE_CALLBACKS_ENABLED) = ['profile_tasks']
CONFIG_FILE() = /home/charly/eclipseprojects/spp_playground/ansible.cfg
DEFAULT_GATHERING(env: ANSIBLE_GATHERING) = explicit
DEFAULT_LOG_PATH(env: ANSIBLE_LOG_PATH) = /tmp/ansible.log
DEFAULT_ROLES_PATH(env: ANSIBLE_ROLES_PATH) = ['/home/charly/eclipseprojects/spp_playground', '/home/charly/eclipseprojects']
EDITOR(env: EDITOR) = vi
GALAXY_IGNORE_CERTS(/home/charly/eclipseprojects/spp_playground/ansible.cfg) = False
INTERPRETER_PYTHON(env: ANSIBLE_PYTHON_INTERPRETER) = /usr/bin/python3
RETRY_FILES_ENABLED(env: ANSIBLE_RETRY_FILES_ENABLED) = False

GALAXY_SERVERS:


CALLBACK:
========

default:
_______
result_format(env: ANSIBLE_CALLBACK_RESULT_FORMAT) = yaml

minimal:
_______
result_format(env: ANSIBLE_CALLBACK_RESULT_FORMAT) = yaml

CONNECTION:
==========

ssh:
___
control_path(/home/charly/eclipseprojects/spp_playground/ansible.cfg) = %(directory)s/%%h-%%r

OS / Environment

Controller: Debian Testing Managed host: Debian Bookworm

Steps to Reproduce

Just use this simple task in a play where variable some_var is not defined anywhere

    - command: hostname
      delegate_to: "{{ item }}"
      loop: '{{ some_var }}'
      when: some_var is defined

Expected Results

Task should be skipped like it was done on Ansible 2.18

ansible-playbook [core 2.18.9]
  config file = /home/charly/eclipseprojects/spp_playground/ansible.cfg
  configured module search path = ['/opt/ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible/lib/python3.13/site-packages/ansible
  ansible collection location = /opt/ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.13.7 (main, Aug 20 2025, 22:17:40) [GCC 14.3.0] (/opt/ansible/bin/python3)
  jinja version = 3.1.6
  libyaml = True
Using /home/charly/eclipseprojects/spp_playground/ansible.cfg as config file
...

TASK [command] ***********************************
task path: /home/charly/eclipseprojects/spp_playground/site.yml:21
Freitag 12 September 2025  13:29:16 +0200 (0:00:00.027)       0:00:00.027 ***** 
skipping: [charly-dev01.server.lan] => 
    changed: false
    false_condition: some_var is defined
    skip_reason: Conditional result was False

Actual Results

Run fails with 'item'' is undefined' message:

TASK [command] *****************
task path: /home/charly/eclipseprojects/spp_playground/site.yml:21
Freitag 12 September 2025  13:25:21 +0200 (0:00:00.026)       0:00:00.026 ***** 
[ERROR]: Task failed: 'item' is undefined

Task failed.
Origin: /home/charly/eclipseprojects/spp_playground/site.yml:21:7

19   tasks:
20
21     - command: hostname
         ^ column 7

<<< caused by >>>

'item' is undefined
Origin: /home/charly/eclipseprojects/spp_playground/site.yml:22:20

20
21     - command: hostname
22       delegate_to: "{{ item }}"
                      ^ column 20

fatal: [charly-dev01.server.lan -> {{ item }}]: FAILED! => 
    changed: false
    msg: 'Task failed: ''item'' is undefined'

Code of Conduct

  • I agree to follow the Ansible Code of Conduct