dragomirr / ansible-role-kafka

1 stars 3 forks source link

'ansible.vars.hostvars.HostVarsVars object' has no attribute 'kafka_node_id' #5

Open hongbo-miao opened 4 days ago

hongbo-miao commented 4 days ago

I am using version v0.10.0. I am trying to set one instance Kafka.

Experiment 1

When I set kafka_node_id: 0 or kafka_node_id: 1:

- name: Install Kafka
  hosts: integrated-test-group-server
  roles:
    - role: dragomirr.kafka
      kafka_node_id: 1
      kafka_heap_size: 2G
      kafka_install_dependencies: true
      kafka_topics:
        - name: production.iot.motor.proto
          replication_factor: 1
          partitions: 1
      kafka_additional_config:
        message.max.bytes: 1048576 # 1 MiB

It gives me error

TASK [dragomirr.kafka : Configure Kafka] ***************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'kafka_node_id'
fatal: [integrated-test-group-server]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'kafka_node_id'"}

Experiment 2

If I remove kafka_node_id field

- name: Install Kafka
  hosts: integrated-test-group-server
  roles:
    - role: dragomirr.kafka
      kafka_heap_size: 2G
      kafka_install_dependencies: true
      kafka_topics:
        - name: production.iot.motor.proto
          replication_factor: 1
          partitions: 1
      kafka_additional_config:
        message.max.bytes: 1048576 # 1 MiB

it gives me error

TASK [dragomirr.kafka : Check if required variables are defined] ***************
fatal: [integrated-test-group-server]: FAILED! => {
    "assertion": "kafka_node_id is defined",
    "changed": false,
    "evaluated_to": false,
    "msg": "Assertion failed"
}

Any ideas? Thanks!

BTW, thank you for this Ansible role! I only found this Ansible role supports KRaft mode. 😃

hongbo-miao commented 3 days ago

Oh I got hint from https://github.com/dragomirr/ansible-role-kafka/issues/1#issuecomment-1607761845

I should set kafka_node_id in my inventory file instead the playbook. (However, I am still not super clear when should I set in the playbook as well 🤔)

Here is my working version. Hope it saves some time for future people 😃

inventory.yaml

all:
  children:
    my_server_group:
      hosts:
        my-server:
          ansible_host: 10.11.10.10
          ansible_user: hongbo-miao
          ansible_become_password: !vault |
            $ANSIBLE_VAULT;1.1;AES256
            xxx
          kafka_node_id: 0

playbook.yml

- name: Install Kafka
  hosts: my-server
  become: yes
  roles:
    - role: dragomirr.kafka
      kafka_heap_size: 2G
      kafka_install_dependencies: true
      kafka_topics:
        - name: production.iot.motor.proto
          replication_factor: 1
          partitions: 1
      kafka_additional_config:
        message.max.bytes: 1048576 # 1 MiB
hongbo-miao commented 3 days ago

Just re-open, it would be great to clarify when need set kafka_node_id in the playbook. 😃 I am quite confused for this comment 🤔 And is "play" here actually "playbook"?

- hosts: servers
  roles:
     - role: dragomirr.kafka
       # setting kafka_node_id in play is only valid if you have 1 kafka node
       # if you have multiple kafka nodes you need to set unique kafka_node_id for each node
       kafka_node_id: 0

Question 1

For me, currently I only have 1 node, but kafka_node_id: 0 in playbook does not work for me. So why does the comment says "setting kafka_node_id in play is only valid if you have 1 kafka node" which is not true.

Question 2

For "if you have multiple kafka nodes you need to set unique kafka_node_id for each node", let's say I have 3 nodes, do I need set different kafka_node_id in

  1. only inventory file
  2. only playbook
  3. both inventory and playbook

which is the case? Thanks!