'Ansible - playbook : Make sure your variable name does not contain invalid characters like '-'

I am using ansible version 2.1.0.0 and part of my code is :

- shell: /etc/init.d/named restart
  when: ('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout)
  ignore_errors: True
  register: namedrestart
- debug: var=namedrestart.stdout_lines
  when: ('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout)
  ignore_errors: True

- shell: /etc/init.d/named status
  when: ('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout)
  ignore_errors: True
  register: namedstatus
- debug: var=namedstatus.stdout_lines
  when: ('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout)
  ignore_errors: True

which is throwing error as :

TASK [debug] *******************************************************************
fatal: [10.139.73.152]: FAILED! => {"failed": true, "msg": "The conditional check '('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout)' failed. The error was: error while evaluating conditional (('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout)): Unable to look up a name or access an attribute in template string ({% if ('FAILED' not in zoneconfig.stdout) and ('neither' not in zoneconfig.stdout) and ('FAILED' not in echofail.stdout) and ('SUCCESSFUL' in echopass.stdout_lines) and ('SUCCESSFUL' in zoneconfig.stdout) %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterable\n\nThe error appears to have been in '/etc/ansible/named_configtest/configtest_named.yml': line 51, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      register: namedrestart\n    - debug: var=namedrestart.stdout_lines\n      ^ here\n"}
...ignoring

Please let me know if there are any workaround to solve this problem



Solution 1:[1]

This is a confusing error message - in my case with a simple if 'abc' in mylist condition, the problem was just that the mylist var was undefined.

Try adding lines like these to show the vars involved:

- debug: var=zoneconfig
- debug: var=zoneconfig.stdout

Solution 2:[2]

in my case got the same kind of below error, while registering the file state output into variable.

Below is the ansible code and error message for your reference

{"msg": "Invalid variable name in 'register' specified: 'imgexec-cache'"}
- name: 'find out /var/imgexec-cache file/director exists or not'
  stat:
    path: "/var/imgexec-cache"
  register: imgexec-cache

due to special character "-" which i mentioned imgexec-cache variable cause this error.

To fix the Invalid variable name in 'register error, removed special character "-" from that register variable and kept like this imgexeccache

- name: 'find out /var/imgexec-cache file/director exists or not'
  stat:
    path: "/var/imgexec-cache"
  register: imgexeccache

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RichVel
Solution 2 Namasivayam Chinnapillai