Skip to content

fix(tasks): avoid substring match when deriving node_id from rpk output #129

@ashishpatelkaseya

Description

@ashishpatelkaseya

Problem

start-redpanda.yml derives node_id using grep on the broker IP:

rpk cluster status {{ redpanda_rpk_opts }} | sed 's/*//g' | grep {{ hostvars[inventory_hostname].private_ip }} | awk '{ print $1;}'

grep matches substrings, so an IP like 172.31.241.1 also matches 172.31.241.153, returning multiple IDs and breaking subsequent logic.

Reproduction

  1. Have two brokers with similar IP prefixes:
    • 172.31.241.1
    • 172.31.241.153
  2. Run the task on the host 172.31.241.1.
  3. The command outputs two IDs (e.g., 6 and 11) instead of one.

Expected

Match the host column exactly and return a single broker ID for the current host.

Proposed fix

Use awk to match column 2 exactly and strip the * from the ID:

- name: Establish node id
  ansible.builtin.shell:
    cmd: >-
      rpk cluster status {{ redpanda_rpk_opts }} |
      awk '$2=={{ hostvars[inventory_hostname].private_ip | quote }} { gsub(/\*/,"",$1); print $1 }'
  register: node_id_out
  changed_when: false

Alternative (more robust, requires jq)

Parse JSON output instead of text columns:

rpk cluster status {{ redpanda_rpk_opts }} -o json \
| jq -r '.brokers[] | select(.host=={{ hostvars[inventory_hostname].private_ip | tojson }}) | .id'

Impact

Prevents incorrect node_id detection in environments where broker IPs share prefixes, improving reliability of maintenance/decommission tasks that depend on node_id.

Reference

roles/redpanda_broker/tasks/start-redpanda.yml (around the node_id derivation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions