ansible角色
- 一、ansible角色 role
- 1、创建角色
- 2、角色目录结构
- 二、部署zabbix-agent
- 1、创建角色
- 2、编写剧本
- 3、执行角色中的操作
- 三、部署分布式zabbix
- 1、zabbixproxy角色结构
- 2、zabbixProxy task/main.yml
- 3、zabbixAgentByProxy角色结构
- 4、zabbixAgentByProxy tasks/main.yml
一、ansible角色 role
本质上就是个目录
一个需求对应一个角色
1、创建角色
[root@zabbix_server roles]# pwd
/etc/ansible/roles[root@zabbix_server roles]# ansible-galaxy init nginx
- Role nginx was created successfully
2、角色目录结构
[root@zabbix_server roles]# tree nginx/
nginx/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars└── main.yml8 directories, 8 files
二、部署zabbix-agent
1、创建角色
[root@zabbix_server roles]# ansible-galaxy init zabbixAgent
- Role zabbixAgent was created successfully
[root@zabbix_server roles]# tree zabbixAgent/
zabbixAgent/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── zabbix_agentd.conf.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars└── main.yml8 directories, 9 files
zabbix_agentd.conf.j2核心内容:
Server=192.168.140.10
ServerActive=192.168.140.10
Hostname={{ ansible_fqdn }}
2、编写剧本
[root@zabbix_server roles]# cat /etc/ansible/roles/zabbixAgent/tasks/main.yml
---
# tasks file for zabbixAgent- name: add zabbix yum repoyum_repository:name: zabbixdescription: zabbixfile: zabbixbaseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/gpgcheck: noenabled: yes- name: install zabbix-agentyum:name: zabbix-agentstate: present- name: push zabbix config filetemplate:src: zabbix_agentd.conf.j2dest: /etc/zabbix/zabbix_agentd.confnotify: restart zabbix-agent- name: start zabbix-agentservice:name: zabbix-agentstate: startedenabled: yes
[root@zabbix_server roles]# cat /etc/ansible/roles/zabbixAgent/handlers/main.yml
---
# handlers file for zabbixAgent- name: restart zabbix-agentservice:name: zabbix-agentstate: restarted
3、执行角色中的操作
[root@zabbix_server roles]# cat /opt/work/installZabbixAgent.yaml
- hosts: dbroles:- zabbixAgent[root@zabbix_server roles]# ansible-playbook /opt/work/installZabbixAgent.yaml
三、部署分布式zabbix
代码仓库:
https://gitee.com/martinwjc/playbook.git
1、zabbixproxy角色结构
[root@zabbix_server roles]# tree zabbixProxy/
zabbixProxy/
├── defaults
│ └── main.yml
├── files
│ └── my.cnf
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── zabbix_proxy.conf.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars└── main.yml8 directories, 10 files
2、zabbixProxy task/main.yml
[root@zabbix_server roles]# cat zabbixProxy/tasks/main.yml
---
# tasks file for zabbixProxy- name: add epel reposhell: wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo- name: add zabbix yum repoyum_repository:name: zabbixdescription: zabbixfile: zabbixbaseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/gpgcheck: noenabled: yes- name: install zabbix-proxyyum:name: "{{ item }}"loop:- mariadb-server- zabbix-proxy-mysql- python2-PyMySQL- name: push mysql configcopy:src: my.cnfdest: /etc/my.cnf- name: start MySQL daemonservice:name: mariadbstate: startedenabled: yes- name: create zabbix proxy databasemysql_db:name: zabbixproxystate: present- name: create zabbix proxy user connect to mysql mysql_user:name: puserpassword: 'redhat'host: localhostpriv: 'zabbixproxy.*:ALL'state: present- name: load zabbix proxy tableshell: zcat /usr/share/doc/zabbix-proxy-mysql-4.4.10/schema.sql.gz | mysql -uroot zabbixproxy- name: push zabbix proxy configtemplate:src: zabbix_proxy.conf.j2dest: /etc/zabbix/zabbix_proxy.conf- name: start zabbix proxy daemonservice:name: zabbix-proxystate: startedenabled: yes
3、zabbixAgentByProxy角色结构
[root@zabbix_server roles]# tree zabbixAgentByProxy/
zabbixAgentByProxy/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── zabbix_agentd.conf.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars└── main.yml
4、zabbixAgentByProxy tasks/main.yml
---
# tasks file for zabbixAgent- name: add zabbix yum repoyum_repository:name: zabbixdescription: zabbixfile: zabbixbaseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/gpgcheck: noenabled: yes- name: install zabbix-agentyum:name: zabbix-agentstate: present- name: push zabbix config filetemplate:src: zabbix_agentd.conf.j2dest: /etc/zabbix/zabbix_agentd.confnotify: restart zabbix-agent- name: start zabbix-agentservice:name: zabbix-agentstate: startedenabled: yes