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

3,095 views
Skip to first unread message

Gregory Spranger

unread,
Jan 9, 2014, 1:07:27 PM1/9/14
to ansible...@googlegroups.com
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

Matt Martz

unread,
Jan 9, 2014, 1:31:11 PM1/9/14
to ansible...@googlegroups.com, Gregory Spranger
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.
-- 
Matt Martz
ma...@sivel.net
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Gregory Spranger

unread,
Jan 9, 2014, 1:34:06 PM1/9/14
to Matt Martz, ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages