Skip to content

Commit

Permalink
Merge pull request #49 from ernestas-poskus/features/add_custom_ip_ha…
Browse files Browse the repository at this point in the history
…sh_key_per_host

Add custom IP hash key per host
  • Loading branch information
ernestas-poskus authored Sep 27, 2016
2 parents 51df52c + 62d3426 commit b8f20df
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ Assuming ```zookeepers``` is a [hosts group](http://docs.ansible.com/ansible/int
server[1:3]
```

Custom IP per host group

```
zookeeper_hosts: "
{%- set ips = [] %}
{%- for host in groups['zookeepers'] %}
{{- ips.append(dict(id=loop.index, host=host, ip=hostvars[host]['ansible_default_ipv4'].address)) }}
{%- endfor %}
{{- ips -}}"
```
See this sample [playbook](https://github.com/AnsibleShipyard/ansible-galaxy-roles/blob/master/playbook.yml)
which shows how to use this playbook as well as others. It is part of [ansible-galaxy-roles](https://github.com/AnsibleShipyard/ansible-galaxy-roles) and
serves as a curation (and thus an example) of all our ansible playbooks.
Expand Down
4 changes: 4 additions & 0 deletions templates/zoo.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ autopurge.snapRetainCount={{ zookeeper_autopurge_snapRetainCount }}

{% for server in zookeeper_hosts %}
{% if server.host is defined %}
{% if server.ip is defined %}
server.{{server.id}}={{server.ip}}:2888:3888
{% else %}
server.{{server.id}}={{server.host}}:2888:3888
{% endif %}
{% else %}
server.{{loop.index}}={{server}}:2888:3888
{% endif %}
Expand Down
34 changes: 34 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,37 @@
- shell: "grep 1 /var/lib/zookeeper/myid"
register: status
failed_when: status.rc != 0

#
# Host hash playbook with ip key
#

- hosts: localhost
connection: local
remote_user: root
roles:
- role: role_under_test
zookeeper_version: 3.4.6
zookeeper_dir: /opt/zookeeper-{{zookeeper_version}}
zookeeper_hosts:
- host: "{{inventory_hostname}}" # the machine running
ip: '192.168.0.1'
id: 2

- hosts: localhost
connection: local
remote_user: root
vars:
zookeeper_version: 3.4.6
zookeeper_dir: /opt/zookeeper-{{zookeeper_version}}
tasks:
# Expecting myid to be 2 as defined in zookeeper_hosts variable
- shell: "grep 2 /var/lib/zookeeper/myid"
register: status
failed_when: status.rc != 0

# Expecting zoo.cfg to include serve definition with custom IP
- shell: "grep 'server.2=192.168.0.1:2888:3888' {{ zookeeper_dir }}/conf/zoo.cfg"
register: status
failed_when: status.rc != 0

0 comments on commit b8f20df

Please sign in to comment.