sleighzy / ansible-kafka

Ansible role for installing and configuring Apache Kafka on RedHat and Debian platforms.
MIT License
116 stars 79 forks source link

kafka_advertised_listeners is not resolved #35

Closed DawidNiezgodka closed 9 months ago

DawidNiezgodka commented 10 months ago

I want to set up advertised.listeners, but the value won't resolve. Even though the variable is set in the config, one can see the following: #advertised.listeners=PLAINTEXT://your.host.name:9092

Here's the complete playbook:

- name: Install openjdk-8-jdk on all nodes
  hosts: kafka-nodes
  become: true
  tasks:
    - name: Install openjdk-8-jdk
      apt:
        name: openjdk-8-jdk
        state: present
        update_cache: yes

- name: Setup zookeeper
  hosts: kafka-nodes
  roles:
    - role: sleighzy.zookeeper
  vars:
      zookeeper_servers:
        - kafka1

      zookeeper_mirror: https://archive.apache.org/dist/zookeeper
      zookeeper_version: 3.6.4
      zookeeper_package: apache-zookeeper-3.6.4-bin.tar.gz
      zookeeper_servers_use_inventory_hostname: true

- name: Setup kafka
  hosts: kafka-nodes
  become: true
  roles:
    - role: sleighzy.kafka
  vars:
    kafka_download_base_url: https://archive.apache.org/dist/kafka
    kafka_broker_id: "{{ inventory_hostname.split('kafka')[-1] }}"
    kafka_advertised_listeners: "[\"PLAINTEXT://{{ansible_ssh_host}}:9092\"]"
    kafka_version: 2.7.0
    kafka_scala_version: 2.13

The way the value is passed seems correct. When I run the following task, it gives me the value correctly:

- name: Print kafka_advertised_listeners value
  hosts: kafka-nodes
  tasks:
    - name: Print value
      debug:
        msg: "[\"PLAINTEXT://{{ansible_ssh_host}}:9092\"]"

->


        ok: [kafka1] => {
    "msg": [
        "PLAINTEXT://1.2.3.4:9092"
    ]
}
sleighzy commented 10 months ago

Hi @DawidNiezgodka . It looks like you may be providing this value as a string vs. an array. For example, you have the following:

kafka_advertised_listeners: "[\"PLAINTEXT://{{ansible_ssh_host}}:9092\"]"

This Ansible role takes an array and then joins the entries using commas. https://github.com/sleighzy/ansible-kafka/blob/master/templates/server.properties.j2#L54

Could you try something like the below instead and see if this works for you:

kafka_advertised_listeners:
  - PLAINTEXT://{{ansible_ssh_host}}:9092
DawidNiezgodka commented 10 months ago

Thanks for the quick reply.

Hmm, it does not work. I tried both:

  1. Without quotes: - PLAINTEXT://{{ansible_ssh_host}}:9092
  2. With quotes "PLAINTEXT://{{ansible_ssh_host}}:9092"

Indentation seems correct, i.e., space, space, -, space, PLA...

The workaround I found is to set kafka_start: no, and then:

- name: Add advertised.listeners to server.properties
  hosts: kafka-nodes
  tasks:
    - name: Add advertised.listeners line
      lineinfile:
        path: /etc/kafka/server.properties
        line: "advertised.listeners=PLAINTEXT://{{ansible_ssh_host}}:9092"
        create: no

    - name: Start kafka service
      service:
        name: kafka
        state: started
        enabled: yes

However, I'd be happy to find the culprit and use it directly ;)

thuvh commented 9 months ago

here is working code

Screen Shot 2023-11-30 at 00 23 01

and my tip to work with quote, you can use double quote inside single code, or vice versa (output is same with escape notation) kafka_advertised_listeners: "['PLAINTEXT://{{ansible_ssh_host}}:9092']"

sleighzy commented 9 months ago

Thanks for that response, much appreciated. I’ll do some tests on my end and update the role and readme so this works as expected.

sleighzy commented 9 months ago

I've done a test on my end and have replicated your issue. When I use the below block with ansible_ssh_host then it fails to put that entry in the server.properties file.

kafka_advertised_listeners:
  - 'SASL_SSL://{{ansible_ssh_host}}:9094'
  - 'PLAINTEXT://{{ansible_ssh_host}}:9092'

Looking at these docs (yes they're slightly older) https://docs.ansible.com/archive/ansible/2.4/intro_inventory.html they mentioned that ansible_ssh_host has been deprecated in favour of ansible_host. I then tested using ansible_host instead and that worked, the entry was there and the entry popluated.

kafka_advertised_listeners:
  - 'SASL_SSL://{{ansible_host}}:9094'
  - 'PLAINTEXT://{{ansible_host}}:9092'
# Listener name, hostname and port the broker will advertise to clients.
# If not set, it uses the value for "listeners".
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=SASL_SSL://server-2:9094,PLAINTEXT://server-2:9092

Have a try on your end and see if you get the expected results.

sleighzy commented 9 months ago

Closing this issue now as should be enough info re: Ansible config to resolve it.