首页 > 其他分享 >Kylin-Ansible-roles自动化部署Msyql-5.7.16

Kylin-Ansible-roles自动化部署Msyql-5.7.16

时间:2022-12-15 22:48:53浏览次数:42  
标签:Ansible Kylin install roles 192.168 vmwarevirtualplatform ansible plengong mysql

Ansible-简介

Ansible是一款开源运维自动化部署工具,它能够提高运维人员的工作效率,并减少人为失误。Ansible基于SSH协议进行自动化控制,受控节点无需安装受控软件。
image

角色-roles

剧本是通过YAML语言编写的可重复执行的任务列表,把常做操作编写为剧本文件,可以重复执行
角色是用于结构化组织Playbook,可将剧本视作功能,使用各种功能组成有个角色。

初始化环境

  • 服务器和客户端基本配置
设备 主机名 IP地址 服务
控制节点 client.kylin.com 192.168.189.120/24 ansible,sshd
受控节点 server.kylin.com 192.168.189.100/24 sshd

项目任务描述

某公司需要部署MySQL服务集群,由于服务器数量众多,基于市面自动化技术,故选择ansible

配置步骤

配置过程

一、配置SSH密钥,采用密钥登陆

plengong@plengong-vmwarevirtualplatform:~/Desktop$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/plengong/.ssh/id_rsa): 
Created directory '/home/plengong/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/plengong/.ssh/id_rsa
Your public key has been saved in /home/plengong/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:tr3v6EgdY42W0fbmAjBNX2fM6C9rMmjWpI/35PtkRx0 plengong@plengong-vmwarevirtualplatform
The key's randomart image is:
+---[RSA 3072]----+
|          .   .+o|
|         o o ..oo|
|        o o +. E |
|         o * .. o|
|        S O . o.o|
|       . * +.o...|
|        o o=. oo+|
|       . .=+=+oo.|
|        .+==+=+o.|
+----[SHA256]-----+
plengong@plengong-vmwarevirtualplatform:~/Desktop$ ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/plengong/.ssh/id_rsa.pub"
The authenticity of host '192.168.85.100 (192.168.85.100)' can't be established.
ECDSA key fingerprint is SHA256:5fU6dObB1SdLgsUmhFAW/SlEjk4AfEIH2CW7pftNw6o.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Authorized users only. All activities may be monitored and reported.
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
plengong@plengong-vmwarevirtualplatform:~/Desktop$ 

二、控制节点安装ansible,修改配置文件

安装Ansible
plengong@plengong-vmwarevirtualplatform:~/Desktop$ sudo apt install ansible -y
Input Password
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Setting up ansible (2.9.6+dfsg-1) ...
修改配置文件和设置主机清单
  • 修改配置文件
行号 参数 解释
14 inventory = /etc/ansible/hosts 指定主机清单路径
68 roles_path = /etc/ansible/roles 指定角色路径
71 host_key_checking = False 禁用主机密钥检测
106 remote_user = root 默认使用登录用户
plengong@plengong-vmwarevirtualplatform:~/Desktop$ sudo vim /etc/ansible/ansible.cfg 
14 inventory      = /etc/ansible/hosts
68 roles_path    = /etc/ansible/roles
71 host_key_checking = False
107 remote_user = root
  • 设置主机清单
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ cat hosts 
[mysql]
192.168.189.100
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ ansible mysql -m ping
[WARNING]: Platform linux on host 192.168.189.100 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change
this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.189.100 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ 

三、创建角色,定义角色任务

创建mysql_install角色

plengong@plengong-vmwarevirtualplatform:~/Desktop$ cp -a /etc/ansible/ ./
plengong@plengong-vmwarevirtualplatform:~/Desktop$ cd ansible/
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ mkdir -p roles
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ cd roles/
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible/roles$ ansible-galaxy init mysq_install
- Role mysq_install was created successfully
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible/roles$ cd mysq_install/
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible/roles/mysq_install$ ls -al
total 48
drwxrwxr-x 10 plengong plengong 4096 Dec 15 21:45 .
drwxrwxr-x  3 plengong plengong 4096 Dec 15 21:45 ..
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 defaults
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 files
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 handlers
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 meta
-rw-rw-r--  1 plengong plengong 1328 Dec 15 21:45 README.md
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 tasks
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 templates
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 tests
-rw-rw-r--  1 plengong plengong  539 Dec 15 21:45 .travis.yml
drwxrwxr-x  2 plengong plengong 4096 Dec 15 21:45 vars
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible/roles/mysq_install$ 

定义角色任务

目录 备注
tasks 包含角色执行的任务
files 包含角色使用的静态文件
  • files添加boost和mysql源码,执行脚本
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible/roles/mysql_install/files$ ls -l
total 131084
-rwxrw-rw- 1 plengong plengong 83709983 Dec 15 00:24 boost_1_59_0.tar.gz
-rwxrw-rw- 1 plengong plengong       75 Dec 15 14:07 env.sh
-rwxrw-rw- 1 plengong plengong 50509574 Dec 13 20:50 mysql-5.7.16.tar.gz
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible/roles/mysql_install/files$ 
  • tasks部署任务
  • 模块以及在该剧本的作用
模块 说明
script 脚本模块:用于执行脚本
file 文件模块:创建文件夹
unarchive 解压模块:解压压缩包并复制受控节点
user 用户模块:创建用户
shell 命令模块:执行命令
service 服务模块:配置服务状态
  • env.yml
---
- name: Create ENV
  script:
          env.sh
- name: Mysql Program Folder
  file:
          path: /usr/local/mysql/var
          state: directory
- name: Decompress Boost Source Code
  unarchive:
          src: boost_1_59_0.tar.gz
          dest: /usr/local

- name: Decompress Mysql Source Code
  unarchive:
          src: mysql-5.7.16.tar.gz
          dest: /usr/local
  • install.yml
---
- name: Compile Mysql Source Code
  shell:
          cmd: "cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc -DWITH_BOOST=/usr/local/boost_1_59_0 && make && make install"
          chdir: /usr/local/mysql-5.7.16
- name: Create User
  user:
          name: mysql
          shell: /sbin/nologin
- name: Initialize Mysql
  shell:
          /usr/local/mysql/bin/mysql_install_db --user mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
- name: Create Mysql Configuration File and Server
  shell:
          rm -rf /etc/my.cnf&&cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf&&cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld&&chmod a+x /etc/rc.d/init.d/mysqld
- name: Create Server Start item
  shell:
          chkconfig --add mysqld&&chkconfig mysqld on
- name: Add mysql environment variables
  shell:
          echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile&&source /etc/profile
- name: start Server and enabled
  service:
          name: mysqld
          state: started
          enabled: yes
  • main.yml
---
# tasks file for mysql_install
- include: env.yml
- include: install.yml

执行剧本调用角色,编译安装Mysql

  • 配置剧本调用角色
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ cat mysql.yml 
--- 
 - name: one
   hosts: mysql
   roles:
           - mysql_install
plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ ansible-playbook mysql.yml 

PLAY [one] *********************************************************************

TASK [Gathering Facts] *********************************************************
[WARNING]: Platform linux on host 192.168.189.100 is using the discovered
Python interpreter at /usr/bin/python, but future installation of another
Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/
reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.189.100]

TASK [mysql_install : Create ENV] **********************************************
changed: [192.168.189.100]

TASK [mysql_install : Mysql Program Folder] ************************************
changed: [192.168.189.100]

TASK [mysql_install : Decompress Boost Source Code] ****************************
changed: [192.168.189.100]

TASK [mysql_install : Decompress Mysql Source Code] ****************************
changed: [192.168.189.100]

TASK [mysql_install : Compile Mysql Source Code] *******************************
changed: [192.168.189.100]

TASK [mysql_install : Create User] *********************************************
changed: [192.168.189.100]

TASK [mysql_install : Initialize Mysql] ****************************************
changed: [192.168.189.100]

TASK [mysql_install : Create Mysql Configuration File and Server] **************
[WARNING]: Consider using the file module with state=absent rather than running
'rm'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [192.168.189.100]

TASK [mysql_install : Create Server Start item] ********************************
changed: [192.168.189.100]

TASK [mysql_install : Add mysql environment variables] *************************
changed: [192.168.189.100]

TASK [mysql_install : start Server and enabled] ********************************
changed: [192.168.189.100]

PLAY RECAP *********************************************************************
192.168.189.100            : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

plengong@plengong-vmwarevirtualplatform:~/Desktop/ansible$ 

安全加固MySQL

[root@localhost ~]# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL server using password in '/root/.mysql_secret'

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: yes

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : yes

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : yes
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : yes
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : yes
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes
Success.

All done!

项目任务验证

[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.16

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> alter user 'root'@'localhost' identified by 'MMS12ge**hao';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

多数资料网上参考,如有错误麻烦指出

标签:Ansible,Kylin,install,roles,192.168,vmwarevirtualplatform,ansible,plengong,mysql
From: https://www.cnblogs.com/wm-plengong/p/16986159.html

相关文章

  • 第21章: Ansible自动化部署K8S-v1.20集群(离线版)
    1.1Ansible介绍Ansible是一种IT自动化工具。它可以配置系统,部署软件以及协调更高级的IT任务,例如持续部署,滚动更新。Ansible适用于管理企业IT基础设施,从具有少数主机的小规......
  • Ansible之Playbook介绍和使用
    1、https://blog.csdn.net/zfw_666666/article/details/1246918771.Playbook介绍       Playbook与ad-hoc相比,是一种完全不同的运用ansible的方式,类似与saltstac......
  • Ansible 服务器主机配置
    使用ansible来对远程主机进行部署的话。我们需要首先对远程注解进行配置。配置文件路径配置文件位于:/etc/ansible/hosts 路径下面。可以使用vi工具进行打开。配置......
  • Ansible自动化部署K8S集群
    一、Ansible自动化部署K8S集群1.1Ansible介绍Ansible是一种IT自动化工具。它可以配置系统,部署软件以及协调更高级的IT任务,例如持续部署,滚动更新。Ansible适用于管理企业I......
  • Ansible最佳实践之 AWX 启用facts缓存和模板问卷调查
    写在前面分享一些AWX启用facts缓存和模板问卷调查的笔记博文内容涉及:启动facts缓存相关配置Demo启用模板调查来设置变量demo食用方式:需要了解Ansible理解不足小伙伴......
  • 关于如何控制Ansible Playbook的执行顺序、运行选定的剧本资源的一些笔记
    写在前面和小伙伴们分享一些​​Ansible​​​中​​Playbook​​执行顺序控制的手段以及运行选定的任务的方法不知道小伙伴们有么有遇到这样的情况一些运维场景,​​Github......
  • Ansible如何使用lookup插件模板化外部数据
    写在前面今天和小伙伴分享使用lookup插件模板化外部数据博文内容比较简单主要介绍的常用lookup插件和对应的Demo外部数据如何代替cat等通过lookup插件读取理解不足小伙伴帮......
  • 如何编写清晰的Ansible剧本(复杂剧本如何构建)
    写在前面嗯,学习Ansible高级特性,整理这部分笔记博文内容涉及复杂Ansible剧本的编写规范一个具体的编写Demo食用方式:理论有些枯燥,不感兴趣小伙伴可以直接跳过去看Demo......
  • pxe安装kylinos,almalinux,centos7,centos8
    一、PXE无盘简介预启动执行环境(PrebooteXecution Environment,PXE)也被称为预执行环境,提供了一种使用网络接口(NetworkInterface)启动计算机的机制。这种机制让计算机的启......
  • linux(centos 6/7/8,kylin)下记录所有用户的操作以及ip、时间
    [root@sdw~]#mkdir/var/log/history[root@sdw~]#chmod777/var/log/history编辑/etc/profile文件,在文件末尾加入下面代码:[root@iZ23nn1p4mjZroot]#vi/etc/profileh......