Danny
Danny

Reputation: 14134

Ansible item when clause throws error: Unable to look up a name or access an attribute in template string

In the following task, I am getting the error message Unable to look up a name or access an attribute in template string. Make sure your variable name does not contain invalid characters like '-'. I have traced it down to the when clause.

Using debug statements I have verified:

If I remove the when statement, the task runs.

 - name: download MySQL packages
   tags:
     - preosupdates
   when: "{{ mysql_server_version | version_compare(mysql_version, '<') or mysql_client_version | version_compare(mysql_version, '<') }}"
   command: yum update -y --downloadonly MySQL-server-advanced-{{ mysql_version }} MySQL-shared-compat-advanced-{{ mysql_version }} MySQL-client-advanced-{{ mysql_version }}
   register: downloadonly
   failed_when: downloadonly.rc not in (1, 0)
   changed_when: "downloadonly is defined and 'No Packages marked for Update' not in downloadonly.stdout"

Versions

Upvotes: 6

Views: 13744

Answers (1)

beporter
beporter

Reputation: 3970

According to the docs, when conditions do not need to use the template markers {{ and }} since those are already implied.

Try this instead:

when: mysql_server_version | version_compare(mysql_version, '<') or 
      mysql_client_version | version_compare(mysql_version, '<')

Upvotes: 1

Related Questions