line-in-file . line update from a list of values

5 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Mar 8, 2021, 3:08:47 AM3/8/21
to Ansible Project

Hi

I’m using lineinfile to include new value inside a file

 

Original line

Kafka-list= kafka1:9092;kafka2:9092;kafka3:9092

I need to replace the value with parameters that are managed inside a configuration – I need to include all server hostname that are under kafka_server_details section

 

The line format should be <kafka-server-hostname>:<kafka-port>;<kafka-server-hostname>:<kafka-port>; ……

The list can include 1 or more servers

 

Configuration File :

kafka_server_details:
     - {ip: 1.1.1.1 , hostname: kafka1.cluster1.com }
     - {ip: 1.1.1.2 , hostname: kafka2.cluster1.com }
     - {ip: 1.1.1.3 , hostname: kafka3.cluster1.com }
kafka_port: 9092

 

 

Playbook

- name: Update flow.properties file
  lineinfile:
    path: /opt/flow.properties
    regexp: "{{ item.from }}"
    line: "{{ item.replace_with }}"
  with_items:
     - { from: lab=, replace_with: lab=1 }
     - { from: Kafka-list=, replace_with: "Kafka-list= {{ kafka_server_details.hostname }}:{{ kafka_port }} ; {{ kafka_server_details.hostname }}:{{ kafka_port }} ; {{ kafka_server_details.hostname }}:{{ kafka_port }} ; " }

 

Reply all
Reply to author
Forward
0 new messages