Ansible playbook task fail: Invalid variable name in register specified

Written by - 0 comments

Published on - Listed in Ansible Linux


An Ansible playbook run failed on a particular task which was using the "stat" module and register the result in a variable:

  # Check if default settings already exist
  - name: MARIADB - Check if default settings 90-ourdefaults.cnf exists
    stat:
      path: /etc/mysql/mariadb.conf.d/90-ourdefaults.cnf
    register: 90ourdefaults

The following playbook run failed:

TASK [MARIADB - Check if default settings 90-ourdefaults.cnf exists] **************************************************
fatal: [server]: FAILED! => {"msg": "Invalid variable name in 'register' specified: '90ourdefaults'"}

The reason is actually quite simple but can be overlooked very easily. From the Ansible variable documentation:

Not all strings are valid Ansible variable names. A variable name can only include letters, numbers, and underscores. Python keywords or playbook keywords are not valid variable names. A variable name cannot begin with a number.

There we have it: A variable name cannot begin with a number!

Slightly adjust the task and use another variable name (remove the leading number) in the register part:

  # Check if default settings already exist
  - name: MARIADB - Check if default settings 90-ourdefaults.cnf exists
    stat:
      path: /etc/mysql/mariadb.conf.d/90-ourdefaults.cnf
    register: ourdefaults

And the Playbook now runs through:

TASK [MARIADB - Check if default settings 90-ourdefaults.cnf exists] **************************************************
ok: [server]



More recent articles: