-1

I'm trying to setup a Zookeeper cluster and in order to set the url that points to the ZK nodes I would like to know which are the ip addresses of the hosts running the current role.

I know that from the playbook I can pass the hosts machines through a variable but it would be nice to check this within the role.

Painy James
  • 805
  • 2
  • 13
  • 26

2 Answers2

1

If you are interested in the IP the host that is currently executing the role has, just use the builtin ansible fact for that, e.g. :

- debug:
    var={{ "ansible_default_ipv4['address'] }}"
ProfHase85
  • 11,763
  • 7
  • 48
  • 66
1

I don't think Ansible supplies a variable to access the hosts that a role is being applied to. If you want to get the active hosts in the current play, use ansible_play_hosts (or play_hosts if your ansible version < 2.2).

However, I think a better solution would be to group your zookeeper nodes together in your inventory.

[zookeeper_nodes]
node1
node2
node3

From there you can access variables from those hosts using groups and hostvars

{% for host in groups['zookeeper_nodes'] %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}

See Ansible's docs on magic variables for more info.

kfreezy
  • 1,499
  • 12
  • 16