记录一次ansible批量推送不同主机不同配置

主要命令

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值