ansible 部署 filebeat

准备工作

创建roles目录

# mkdir -pv  /data/apps/ansible/roles/filebeat/{files,tasks,handlers,templates,vars}

hosts

[filebeat]
172.16.18.31 ansible_ssh_port=22  ansible_ssh_user=ubuntu hostname=filebeat-01

测试连通性

# ansible filebeat -m ping
172.16.18.247 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}

创建角色相关文件

tasks/main.yml

- include: unarchive.yml
- include: conf.yml
- include: service.yml

tasks/unarchive.yml

- name: Create a directory if it does not exist
  ansible.builtin.file:
    path: /data/apps/filebeat
    state: directory
- name: copy tar to remote host and file mode
  unarchive: 
    src: filebeat-8.7.0-linux-x86_64.tar.gz 
    dest: /data/apps/filebeat/
- name: create ln 
  file: 
    src: /data/apps/filebeat/filebeat-8.7.0-linux-x86_64
    dest: /usr/local/filebeat
    state: link

tasks/conf.yml

- name: filebeat.yml
  copy: 
    src: filebeat.yml  
    dest: /usr/local/filebeat/filebeat.yml

tasks/service.yml

- name: service script
  copy: 
    src: filebeat.service  
    dest: /lib/systemd/system/filebeat.service
- name: start service
  service: 
    name: filebeat
    state: started 
    enabled: yes

查看相关文件

# tree /data/apps/ansible/roles/filebeat/
/data/apps/ansible/roles/filebeat/
├── files
│   ├── filebeat-8.7.0-linux-x86_64.tar.gz
│   ├── filebeat.service
│   └── filebeat.yml
├── handlers
├── tasks
│   ├── conf.yml
│   ├── main.yml
│   ├── service.yml
│   └── unarchive.yml
├── templates
└── vars

5 directories, 7 files

playbook调用角色

filebeat_roles.yml

- hosts: filebeat
  remote_user: ubuntu
  become: yes
  roles:
   - role: filebeat

运行playbook

# ansible-playbook filebeat_roles.yml

 

posted @ 2023-04-12 14:47  小吉猫  阅读(112)  评论(0编辑  收藏  举报