Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • mysql_server_version='5.2.23'
  • mysql_client_version='5.2.23'
  • mysql_version='5.2.23'

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

  • ansible-1.9.0.1
like image 438
Danny Avatar asked Apr 17 '15 18:04

Danny


1 Answers

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, '<')
like image 200
beporter Avatar answered Dec 28 '22 13:12

beporter