"register" list of ansible hostnames that are part of an inventory group

hi all,

i am trying to be DRY, and wondering if i can somehow register all of the hosts that are part of inventory group … for example:

inventory
[db_slave]
host1
host2

playbook

  • name: create slave user for each slave host
    mysql_user:
    name=slave
    password=foo
    host={{ item }}
    state=present
    with_items:
  • #need help here
  • #somehow get list of hosts from group db_slave

yes, i did see this example online:

{% for host in groups[‘db_slave’] %}
{{ hostvars[host][‘hostname’] }}
{% endfor %}

and i even tried to put that into my roles “vars” directory … but just kept getting syntax errors which makes sense …

slave hosts

mysql_slave_hosts:
{% for host in groups[‘db_slave’] %}

  • {{ hostvars[host][‘hostname’] }}
    {% endfor %}

any thoughts ??

thanks as always,
greg

I think what you probably want is something like:

inventory
[db_slave]
host1
host2

playbook

  • name: create slave user for each slave host
    mysql_user:
    name=slave
    password=foo
    host={{ hostvars[item][‘ansible_hostname’] }}
    state=present
    with_items: groups[‘db_slave’]

No need to create another variable anywhere. And I used ansible_hostname here, since hostname is not a standard var and I wasn’t sure if that is a custom var you have set for those hosts.

awesome !!

didn't you know you could reference it directly .. thanks for the info
and speedy reply -- will give it a try here in a bit ..

greg