Hello,
I’ve been having some problems passing parameters to roles. Essentially the data in my parameter is getting flattened into a string using {{}} interpolation, but not with ${} interpolation. Is there a recommended way to pass parameters like this? I want top be able to define lists of dictionaries. pass them to roles, and have the roles loop over them.
A toy example - given a playbook like this which passes a list parameter ‘databases’ to a role called ‘mysql’:
---
- name: test roles
hosts: all
user: root
roles:
- role: mysql
databases:
```- ‘{{my.mysql
.database}}’`
The mysql role consists only of this tasks/main.yml file:
---
- debug: msg="database is '{{databases[0].name}}'"
- debug: msg="database is '{{item}}'"
with_items: '{{databases}}'
I am defining my.mysql.database variable in host_vars/all:
my:
mysql:
database:
name: fred
I get an error implying that the parameter’s data has been flattened into a string, as follows:
TASK: [debug msg="database is '{{databases[0].name}}'"] ***********************
fatal: [lamian.noodlefactory.co.uk] => One or more undefined variables: 'unicode object' has no attribute 'name'
Removing the quotes is no good, it gets a YAML syntax error.
ERROR: Syntax Error while loading YAML script, test-roles.yml
Note: The error may actually appear before this position: line 8, column 12
databases:
- {{my.drupal.database}}
^
But what does work is using the old dollar syntax, ${my.mysql.database}, with no quotes:
TASK: [debug msg="database is '{{databases[0].name}}'"] ***********************
ok: [lamian.noodlefactory.co.uk] => {"item": "", "msg": "database is 'fred'"}
TASK: [debug msg="database is ''"] ********************************************
ok: [lamian.noodlefactory.co.uk] => (item={'name': 'fred'}) => {"item": {"name": "fred"}, "msg": "database is '{'name': 'fred'}'"}
However, I gather that syntax is discouraged these days. Is there a way of avoiding it in this case?
Cheers,
Nick