判断kafka服务是否启动,并尝试启动kakfa,启动kafka失败开始安装kafka、jdk,添加kafka server定时删除脚本,完成以上步骤清理安装包
---
- name: check kafka service shell: "ps -ef|grep kafka|grep -v |awk '{print $2}'"
register: kafka_process_result
- name: try to start kafka service
shell: "/usr/local/kafka/{{brokers.package_name}}/bin/kafka-server-start.sh -daemon /usr/local/kafka/{{brokers.package_name}}/config/kraft/server.properties"
ignore_errors: true
register: kafka_start_process
when: kafka_process_result.stdout_lines==[]
- fail:
msg: "try to start kafka fail, start setup kafka later"
register: kafka_setup
ignore_errors: true
when: kafka_start_process is not skip and kafka_start_process is failure
- name: setup kafka
block:
- name: copy packages
copy: src: "{{copy_path}}/{{brokers.package_name}}.tgz"
dest: "/usr/local/kafka/"
owner: root
group: root
mode: 0755
ignore_errors: yes
- name: unzip kafka package
shell: cd /usr/local/kafka && tar -zxvf {{brokers.package_name}}.tgz
- name: copy server.properties
template:
src=server.kraft.properties.j2
dest=/usr/local/kafka/{{brokers.package_name}}/config/kraft/server.properties
- name: copy kafka service
template:
src=kafka.service.j2
dest=/usr/lib/systemd/system/kafka.service
- name: enable kafka service
shell: systemctl enable kafka
- name: copy jdk
copy:
src: "{{copy_path}}/{{jdk.version}}.tar"
dest: "/usr/local"
owner: root
group: root
mode: 0755
ignore_errors: yes
- name: unzip jdk
shell: cd /usr/local && tar -xvf {{jdk.version}}.tar
- name: set JAVA_HOME
lineinfile:
dest=/etc/profile insertafter="{{item.position}}" line="{{item.value}}" state=present
with_items:
- {position: EOF, value: "export JAVA_HOME=/usr/local/{{jdk.version}}"}
- {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"}
- name: copy jdk to ~/.bashrc
lineinfile: dest=~/.bashrc insertafter="{{item.position}}" line="{{item.value}}" state=present
with_items:
- {position: EOF, value: "export JAVA_HOME=/usr/local/{{jdk.version}}"}
- {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"}
- name: source profile
shell: source /etc/profile && source ~/.bashrc
when: jdk_setup is not skip
- name: clean install package
shell: rm -f /usr/local/kafka/{{brokers.package_name}}.tgz
- name: copy kafka clean log script
template:
src: clean_kafka_logs.j2
dest: /usr/local/kafka/{{brokers.package_name}}/bin/clean_kafka_logs.sh
owner: root
group: root
mode: 0777
rescue:
- name:
shell: "echo setup end"
when: kafka_setup is not skip
配置kafka log地址,初始化kafka集群,清除集群初始化脚本,添加清除server日志定时任务,启动kafka服务
- name: mkdir kafka-logs
shell: mkdir -p /usr/local/kafka/{{brokers.package_name}}/kafka-logs
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: set JAVA_HOME
shell: export JAVA_HOME=/usr/local/{{jdk.version}} && export PATH=$JAVA_HOME/bin:$PATH
- name: create meta-data uuId
shell: source /etc/profile && source ~/.bashrc && sh /usr/local/kafka/{{brokers.package_name}}/bin/kafka-storage.sh random-uuid
register: kafka_metadata_uuId
- name: copy init kafka cluster script
template:
src: init_kafka_cluster.j2
dest: /tmp/init_kafka_cluster.sh
owner: root
group: root
mode: 0777
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: init kafka cluster
shell: source /etc/profile && source ~/.bashrc && sh /tmp/init_kafka_cluster.sh
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: clean kafka cluster script
shell: rm -f /tmp/init_kafka_cluster.sh
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: stop old kafka service
shell: service kafka stop
ignore_errors: yes
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: delete old kafka service
shell: rm -f /usr/lib/systemd/system/kafka.service
ignore_errors: yes
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: copy kafka service
template: src=kafka.service.j2 dest=/usr/lib/systemd/system/kafka.service
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: reload systemctl deamon
shell: systemctl daemon-reload
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: enable kafka service
shell: systemctl enable kafka
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: start kafka service
shell: service kafka restart
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
可通过以下语句执行kafka部署初始化工作
ansible-playbook -i hosts playbook/kafka.yml --tag install
ansible-playbook -i hosts playbook/kafka.yml --tag init --limit ipaddress