Prepare the management machine first (Boss)
1. Install the environment:
yum install epel-release -y
yum install ansible libselinux-python -y
2. View version
ansible --version
Prepare to be managed node (node)
1. Prepare the environment node node:
yum install epel-release libselinux-python -y
Master node configuration host:
vim /etc/ansible/hosts
pull to the end, format
[bobo] Such a naming format is easy to manage
[bobo]
172.28.229.146
172.28.229.145
Try to connect with ansible:
ansible bobo -m command -a "hostname" -k -u root
Enter the password, the connection is successful, and the execution is successful
If the prompt fails, use ssh root@ip to connect again
Ansible's execution template:
ansible bobo -m module name -a "shell command"
ansible chaoge -m command -a "uptime"
Configure the secret-free login ssh key:
1. Create a key pair on the master node
ssh-keygen -f ~/.ssh/id_rsa -P "" > /dev/null 2>&1
2. Check the public key file and private key file
cd ~/.ssh/
ls
authorized_keys id_rsa id_rsa.pub known_hosts
3. The master node writes the public key distribution script:
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. Run key.sh
5. Use ansible sub-test:
ansible bobo -m command -a "uname -a"
ansible-doc command:
1. List all modules supported by ansible
ansible-doc -l |grep ^command
ansible-doc -s command
The command module is the default module
shell module
The shell module is a frequently used module: pear example
ansible bobo -m shell -a "ps -ef|grep vim|grep -v grep"
Execute scripts in batches:
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 module --- focus:
The master writes a script that can be executed on all nodes: the format is similar, and the script does not need to exist on the client
ansible chaoge -m script -a "/myscripts/local_hostname.sh"
copy module: push data to slave nodes
ansible-doc -s copy #查看参数的用法
Example: Batch copy files:
ansible chaoge -m copy -a "src=/etc/hosts
dest=/tmp/m01_hosts owner=learn_ansible group=learn_ansible
mode=0666"
Check the copied information:
ansible chaoge -m command -a "ls -l /tmp/m01_hosts"
yum module practice:
Install nginx in batches:
ansible chaoge -m yum -a "name=nginx state=installed"
Batch check:
ansible chaoge -m shell -a "rpm -qa nginx warn=false"
Uninstall nginx in batches:
ansible chaoge -m yum -a "name=nginx state=absent"
Batch upgrade:
ansible chaoge -m yum -a "name='nginx' state=latest"
playbook script:
The first playbook: yaml format
host host part:
Specifications for script writing:
Execution of the script:
ansible-playbook nginx.yaml
View the verbose output of the playbook:
ansible-playbook nginx.yaml --verbose
Playbook deployment rsyns actual combat topic:
Actual way of writing:
Run the playbook:
ansible-playbook install_rsync.yaml -C
ansible-playbook install_rsync.yaml
Zabbixj monitoring:
The memory requirement is more than 2g:
getenforce #查看防火墙状态
iptables -L #查看iptablees
free -m #服务端内存,尽量给大点
Get the official download source:
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
Modify the mirror source:
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
Install zabbix server and agent:
yum install zabbix-server-mysql zabbix-agent -y
Install Software Collections:
yum install centos-release-scl -y
Modify the source of zabbix front section:
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
Install the zabbix front-end environment:
yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y
Install the zabbix database:
yum install mariadb-server -y
Start the database and configure boot self-discarding:
systemctl enable --now mariadb
Set the database password: follow the prompts:
mysql_secure_installation
Connect to the database:
[root@master ~]# mysql -u root -p
Create a database connection: very important
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;
Import database information: user zabbix:
-p is followed by the database name: password chaoge666
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Try to log in:
[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)]>
View database:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| zabbix |
+--------------------+
2 rows in set (0.01 sec)
MariaDB [(none)]>
use zabbix database:
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;
Modify the configuration file password:
/etc/zabbix/zabbix_server.conf
search:
Check if changed:
[root@zabbix-server01 data]# grep 'timezone' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
Modify time zone:
/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
Start the zabbix service:
Set up to start automatically:
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm