with_items and groups of hosts

Hello!

We’re trying to set up a MySQL user for each one of our FreeRADIUS nodes on our master MySQL host.

file /etc/ansible/hosts

[mysql-masters]
db.company.local

[radius-servers]
radius1.company.local
radius2.comapny.local

In the role ‘mysql-masters’ we have this task:

  • name: Add FreeRADIUS MySQL user
    mysql_user: name={{ sql_user }} host={{ item }} password={{ sql_pass }} priv={{ sql_db }}.*:ALL
    with_items: “{{ hosts[‘radius_servers’] }}”

Unfortunately this approach isn’t working:
TASK: [Add FreeRADIUS MySQL user] *********************************************
fatal: [db.company.local] => with_items expects a list

What’s the best way to go about this? I’m following the best practices role based layout.

Cheers!

Any time you say “{{ }}” you are saying “hey, I want a string!”

So you don’t need to do this, and can just say

with_items: hosts[‘radius_servers’]

or (cleaner)

with_items: hosts.radius_servers

Ah I see, thank you!

If anyone else has the same issue, I was actually looking for “groups[‘radius_servers’]” not hosts.

Thanks again!

Yes, I had assumed you had that explicitly defined somewhere.

For new readers, groups[‘foo’] is an Ansible provided list of all hostnames (or IPs, etc) in a given group.