Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional variable to use inventory_hostname instead of ansible_hostname #38

Merged
merged 3 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
molecule-venv/

super-linter.log
.vscode
.vscode

*.iml
81 changes: 61 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,67 @@ See <https://github.com/ansible/ansible/issues/71528> for more information.

## Role Variables

| Variable | Default |
| ----------------------- | ----------------------------------------------------------------- |
| zookeeper_mirror | <http://www-eu.apache.org/dist/zookeeper> |
| zookeeper_version | 3.7.1 |
| zookeeper_package | apache-zookeeper-{{ zookeeper_version }}-bin.tar.gz |
| zookeeper_group | zookeeper |
| zookeeper_user | zookeeper |
| zookeeper_root_dir | /usr/share |
| zookeeper_install_dir | '{{ zookeeper_root_dir}}/apache-zookeeper-{{zookeeper_version}}' |
| zookeeper_dir | '{{ zookeeper_root_dir }}/zookeeper' |
| zookeeper_log_dir | /var/log/zookeeper |
| zookeeper_data_dir | /var/lib/zookeeper |
| zookeeper_data_log_dir | /var/lib/zookeeper |
| zookeeper_client_port | 2181 |
| zookeeper_id | 1 |
| zookeeper_leader_port | 2888 |
| zookeeper_election_port | 3888 |
| zookeeper_servers | zookeeper-nodes |
| zookeeper_environment | "JVMFLAGS": "-javaagent:/opt/jolokia/jolokia-jvm-1.6.0-agent.jar" |
| zookeeper_firewalld | false |
| Variable | Default | Comment |
|------------------------------------------|-------------------------------------------------------------------|----------------------------------------------------------------|
| zookeeper_mirror | <http://www-eu.apache.org/dist/zookeeper> ||
| zookeeper_version | 3.7.1 ||
| zookeeper_package | apache-zookeeper-{{ zookeeper_version }}-bin.tar.gz ||
| zookeeper_group | zookeeper ||
| zookeeper_user | zookeeper ||
| zookeeper_root_dir | /usr/share ||
| zookeeper_install_dir | '{{ zookeeper_root_dir}}/apache-zookeeper-{{zookeeper_version}}' ||
| zookeeper_dir | '{{ zookeeper_root_dir }}/zookeeper' ||
| zookeeper_log_dir | /var/log/zookeeper ||
| zookeeper_data_dir | /var/lib/zookeeper ||
| zookeeper_data_log_dir | /var/lib/zookeeper ||
| zookeeper_client_port | 2181 ||
| zookeeper_id | 1 | Unique per server and should be declared in the inventory file |
| zookeeper_leader_port | 2888 ||
| zookeeper_election_port | 3888 ||
| zookeeper_servers | zookeeper-nodes | See below |
| zookeeper_servers_use_inventory_hostname | false | See below |
| zookeeper_environment | "JVMFLAGS": "-javaagent:/opt/jolokia/jolokia-jvm-1.6.0-agent.jar" ||
| zookeeper_firewalld | false ||

## Inventory and zookeeper_servers variable

zookeeper_servers variable above accepts a list of inventory host names.
These will be used in the `zoo.cfg` to configure a multi-server cluster
so the hosts can find each other. By default, the hostname used in
the `zoo.cfg` will be the hostname reported by the `hostname` command on
the server(provided by the ansible_nodename variable). See the example below.

Assuming the below inventory file, and that the `hostname` command returns
only the hostname and does not include the domain name.

```ini
[zookeeper-nodes]
zoo1.foo.com zookeeper_id=1 #hostname command returns "zoo1"
zoo2.foo.com zookeeper_id=2 #hostname command returns "zoo2"
zoo3.foo.com zookeeper_id=3 #hostname command returns "zoo3"
```

And assuming the following role variables:

```yaml
...
- role: sleighzy.zookeeper
zookeeper_servers:
- zoo1.foo.com
- zoo2.foo.com
- zoo3.foo.com
```

The templated `zoo.cfg` file will contain the below entries:

```ini
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
```

If you DO NOT want this behaviour and would like the `zoo.cfg` to template the
inventory_hostname then set `zookeeper_servers_use_inventory_hostname` to `true`

### Default Ports

Expand Down
1 change: 1 addition & 0 deletions defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ zookeeper_id: 1
zookeeper_leader_port: 2888
zookeeper_election_port: 3888
zookeeper_servers: "{{ groups['zookeeper-nodes'] }}"
zookeeper_servers_use_inventory_hostname: false
zookeeper_environment: {}

# Set to "false" to disable the AdminServer. By default the AdminServer is enabled.
Expand Down
4 changes: 3 additions & 1 deletion templates/zoo.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ autopurge.purgeInterval={{ zookeeper_purge_interval }}
# Each server also needs a /var/lib/zookeeper/myid file created containing
# a single unique number, e.g. 1, 2, etc.
{% for host in zookeeper_servers %}
server.{{ hostvars[host].zookeeper_id | default(zookeeper_id) }}={{ hostvars[host].ansible_nodename }}:{{ zookeeper_leader_port }}:{{ zookeeper_election_port }}
server.{{ hostvars[host].zookeeper_id | default(zookeeper_id) }}={{
host if zookeeper_servers_use_inventory_hostname else hostvars[host].ansible_nodename
}}:{{ zookeeper_leader_port }}:{{ zookeeper_election_port }}
{% endfor %}

{% if zookeeper_version is version('3.5', '>=') %}
Expand Down