主要命令
ansible-playbook -i hosts filebeat.yml -k -K
-k 手动输入远程主机(组)密码
-K 手动输入sudo用户密码
命令行方式如下:
ansible -i hosts app01 -m shell -a "sudo systemctl status filebeat" -k -K --become
1.整体文件
2.cat filebeat.yml ansible-playbook主要yml
---
- hosts: appservers
gather_facts: no #关闭 facts 变量
become: true #开启模式
become_method: sudo #使用sudo
tasks:
- name: copy rpm file to other server ......
copy:
src: "/opt/software/filebeat-7.8.0-x86_64.rpm"
dest: "/opt/software/"
- name: install filebeat ......
yum:
name: "/opt/software/filebeat-7.8.0-x86_64.rpm"
- name: set enabled ......
service:
name: filebeat
enabled: yes
- name: started filebeat ......
service:
name: filebeat
state: started
- name: Copy Template File to /etc/filebeat/filebeat.yml
template:
src: filebeat.yml.j2
dest: /etc/filebeat/filebeat.yml
notify: restart filebeat
handlers:
- name: restart filebeat
service:
name: filebeat
state: restarted
3.cat filebeat.yml.j2模板文件
filebeat.inputs:
{% for item in app_msg %}
- type: log
tail_files: true
backoff: "1s"
paths:
- {{ item.app_log }}
fields_under_root: true
fields:
type: {{ item.app_name }}
# ttype: second
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
{% endfor %}
processors:
- drop_fields:
fields: ["agent","ecs","input"]
output.kafka:
hosts: ["ip:port","ip:port","ip:port"]
topic: {{ app_topic }}
required_acks: 1
4.cat group_vars/appservers组变量
app_topic: bobcfc-app-topic
5.cat host_vars/app03主机变量
[root@ansible filebeat]# cat host_vars/app03
app_msg:
- { app_log: "/home/work/test.log",app_name: "suoyin-test" }
- { app_log: "/home/work/123.log",app_name: "suoyin-123" }
- { app_log: "/home/work/456.log",app_name: "suoyin-456" }
6 cat hosts主机清单
[appservers]
app01 ansible_ssh_host=ip ansible_ssh_user=username
app02 ansible_ssh_host=ip ansible_ssh_user=username
app03 ansible_ssh_host=ip ansible_ssh_user=username
app04 ansible_ssh_host=ip ansible_ssh_user=username