Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple blocks, meta: end_play and undefined variables #68939

Closed
keitalbame opened this issue Apr 14, 2020 · 2 comments
Closed

Multiple blocks, meta: end_play and undefined variables #68939

keitalbame opened this issue Apr 14, 2020 · 2 comments
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@keitalbame
Copy link

keitalbame commented Apr 14, 2020

SUMMARY

Mutiple blocks and meta: end_play triggering undefined variable errors

ISSUE TYPE
  • Bug Report
COMPONENT NAME

block

ANSIBLE VERSION
ansible 2.9.1
  config file = /mnt/c/Users/nuncosta/git/nokia/ansible/ansible.cfg
  configured module search path = [u'/home/nuno/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/nuno/.local/lib/python2.7/site-packages/ansible
  executable location = /home/nuno/.local/bin/ansible
  python version = 2.7.15+ (default, Jul  9 2019, 16:51:35) [GCC 7.4.0]
CONFIGURATION
DEFAULT_CALLBACK_WHITELIST(/REDACTED/ansible/ansible.cfg) = [u'profile_tasks', u'log_plays']

DEFAULT_ROLES_PATH(/REDACTED/ansible/ansible.cfg) = [u'/REDACTED/ansible/roles']
DEFAULT_STDOUT_CALLBACK(/REDACTED/ansible/ansible.cfg) = yaml
INVENTORY_ENABLED(/REDACTED/ansible//ansible/ansible.cfg) = [u'yaml']
OS / ENVIRONMENT

Ubuntu 18.04 LTS WSL

STEPS TO REPRODUCE

Gist here.

---

- hosts: all
  vars:
    create_file: false
    create_directory: false
  tasks:
    - name: Create file
      file:
        path: /tmp/test
        state: touch
      when: create_file | bool
    
    - name: Create folder
      file:
        path: /tmp/test-folder
        state: directory
      when: create_directory | bool

    - block:
        - name: Verify if file is present
          stat:
            path: /tmp/test
          register: file_check
        
        - debug:
            msg: "file does not exist"
          when: not file_check.stat.exists

        - meta: end_play
          when: not file_check.stat.exists
      tags: create_file
    
    - block:
        - name: Verify if folder is present
          stat:
            path: /tmp/test-folder
          register: folder_check
        
        - debug:
            msg: "folder does not exist"
          when: not folder_check.stat.exists

        - meta: end_play
          when: not folder_check.stat.exists
      tags: create_folder
EXPECTED RESULTS

Be able to run block using tag create_folder, without errors pointing to undefined variables on meta: end_play task from the first block.

ACTUAL RESULTS

Undefined variable error is seen, only pointing to meta: end_play task. The same variable is used in debug (line 20) task and no error is triggered.

Commenting lines 30 and 31 and rerun the playbook, finishes sucessfully.

Workaround is to add - file_check is defined to debug (Line 28) and meta (line 33) when conditions

$ ansible-playbook -v -i inventories/dev playbooks/01-test.yaml -t create_folder
Using /REDACTED/ansible/ansible.cfg as config file

PLAY [REDACTED] ****************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
Tuesday 14 April 2020  12:22:07 +0100 (0:00:00.181)       0:00:00.181 *********
ok: [REDACTED]
ERROR! The conditional check 'not file_check.stat.exists' failed. The error was: error while evaluating conditional (not file_check.stat.exists): 'file_check' is undefined

The error appears to be in '/REDACTED/ansible/playbooks/01-test.yaml': line 30, column 11, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


        - meta: end_play
          ^ here
@ansibot
Copy link
Contributor

ansibot commented Apr 14, 2020

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Apr 14, 2020
@mkrizek
Copy link
Contributor

mkrizek commented Apr 14, 2020

This is due to meta tasks not supporting tags. As a result of that the end_play task from the first block is always executed regardles of --tags and since the task registering file_check variable is not executed, the end_play task fails on that variable being undefined.

This issue is reported in #64558 and the tags support for meta tasks is being worked on in #67508.

If you have further questions please stop by IRC or the mailing list:

@mkrizek mkrizek closed this as completed Apr 14, 2020
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Apr 14, 2020
@ansible ansible locked and limited conversation to collaborators May 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

3 participants