Ansible自动化运维、ZABBIX监控

先准备管理机器(Boss)

1.装好环境:

yum install epel-release -y
yum install ansible libselinux-python -y

2.查看版本

ansible --version

准备被管理节点(node)

1.准备环境node节点:

yum install epel-release libselinux-python -y

master节点配置host:

vim /etc/ansible/hosts

 拉到最后、格式

[bobo]这样的得命名格式好管理

[bobo]
172.28.229.146
172.28.229.145

尝试用ansible连接:

ansible bobo -m command -a "hostname" -k -u root

输入密码,连接成功,执行成功

 如果提示失败用ssh root@ip连一遍就行了

ansible的执行模板:

ansible    bobo  -m     模块名  -a "shell命令"

ansible chaoge -m command -a "uptime"

配置免密登陆ssh密钥:

1.在master节点创建密钥对

ssh-keygen -f ~/.ssh/id_rsa -P "" > /dev/null 2>&1

2.检查公钥文件私钥文件

cd ~/.ssh/
 ls
authorized_keys id_rsa id_rsa.pub known_hosts

3.master节点编写公钥分发脚本:

vim kry.sh
#!/bin/bash
rm -rf ~/.ssh/id_rsa*
ssh-keygen -f ~/.ssh/id_rsa -P "" > /dev/null 2>&1
SSH_Pass=111111
Key_Path=~/.ssh/id_rsa.pub
for ip in 138 139
do
 sshpass -p$SSH_Pass ssh-copy-id -i $Key_Path "-o
StrictHostKeyChecking=no" 192.168.178.$ip
done

 4.运行key.sh

5.利用ansible分测试:

ansible bobo -m command -a "uname -a"

ansible-doc命令:

1.列出所有ansible支持的模块

ansible-doc -l |grep ^command
ັ查看该模块支持的参数
ansible-doc -s command

 command模块是默认模块

shell模块

shell模块是经常使用的模块:梨子例子

 ansible bobo -m shell -a "ps -ef|grep vim|grep -v grep"

批量执行脚本:

ansible chaoge -m shell -a "mkdir -p /server/myscripts/;echo
'hostname' > /server/myscripts/hostname.sh;chmod +x
/server/myscripts/hostname.sh;bash
/server/myscripts/hostname.sh warn=False"

script模块---重点:

master写一遍脚本可在所有节点执行:格式差不多,该脚本不用再客户机存在

ansible chaoge -m script -a "/myscripts/local_hostname.sh"

copy模块:把数据推到从节点

ansible-doc -s copy  #查看参数的用法

例子:批量拷贝文件:

ansible chaoge -m copy -a "src=/etc/hosts 
dest=/tmp/m01_hosts owner=learn_ansible group=learn_ansible
mode=0666"

检查拷贝后的信息:

 ansible chaoge -m command -a "ls -l /tmp/m01_hosts"

yum模块练习:

批量安装nginx:

ansible chaoge -m yum -a "name=nginx state=installed"

批量检查:

ansible chaoge -m shell -a "rpm -qa nginx warn=false"

批量卸载nginx:

ansible chaoge -m yum -a "name=nginx  state=absent"

批量升级:

ansible chaoge -m yum -a "name='nginx' state=latest"

playbook剧本:

第一个剧本:yaml格式

 host主机部分:

 剧本的编写规范:

 剧本的执行:

ansible-playbook nginx.yaml

查看剧本的详细输出:

ansible-playbook nginx.yaml --verbose

playbook部署rsyns实战题目:

 实际写法:

 运行剧本:

ansible-playbook install_rsync.yaml -C
ansible-playbook install_rsync.yaml

ZABBIXj监控:

内存要求2g以上:

getenforce  #查看防火墙状态
iptables -L  #查看iptablees
free -m  #服务端内存,尽量给大点

获取官方下载源:

rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

修改镜像源:

sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

安装zabbix  server和agent:

yum install zabbix-server-mysql zabbix-agent -y

安装Software Collections:

yum install centos-release-scl -y

修改zabbix前段源:

vim /etc/yum.repos.d/zabbix.repo 
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$b
asearch/frontend
enabled=1 #修改这里
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

安装zabbix前端环境:

yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y

安装zabbix数据库:

yum install mariadb-server -y

启动数据库,配置开机自弃:

systemctl enable --now mariadb

设置数据库密码:按提示:

mysql_secure_installation

连接数据库:

[root@master ~]# mysql -u root -p

创建数据库连接:很重要

create database zabbix character set utf8 collate utf8_bin;
create user zabbix@localhost identified by 'chaoge666';
grant all privileges on zabbix.* to zabbix@localhost;
flush privileges;
quit;

 导入数据库信息:用户zabbix:

-p后面是数据库名字:密码chaoge666

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

尝试登陆:

[root@master ~]# mysql -uzabbix -pchaoge666
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

查看database :

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| zabbix             |
+--------------------+
2 rows in set (0.01 sec)

MariaDB [(none)]> 

use zabbix数据库:

MariaDB [(none)]> use zabbix
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [zabbix]> 
show tables;

修改配置文件密码:

/etc/zabbix/zabbix_server.conf

搜索:

 检查是否改好了:

[root@zabbix-server01 data]# grep 'timezone' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

修改时区:

/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

 启动zabbix服务:

 设置开机自启:

systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

猜你喜欢

转载自blog.csdn.net/weixin_42435798/article/details/126111663