使用自动化运维工具Ansible集中管理服务器
1 安装MySQL数据库并去除安全隐患
1.1 安装完mariadb-server后,运行mysql_secure_installation去除安全隐患
mysql_secure_installation会执行几个设置:
(1)为root用户设置密码
(2)删除匿名账号
(3)取消root用户远程登录
(4)删除test库和对test库的访问权限
(5)刷新授权表使修改生效
进入安全设置向导:
[[email protected] Packages]# mysql_secure_installation
初始root用户没有密码,直接回车。为root设置密码123456:
删除匿名用户:
不允许root用户远程登录:
删除test数据库:
重新加载权限表:
如果不做安全配置,设置root用户密码的方法:
[[email protected] ~]# mysqladmin -u root password “123456”
1.2 测试数据库是否正常
登录数据库并显示所有数据库:
[[email protected] ~]# mysql -u root -p123456
注意:在-p选项和密码之间不能有空格,否则在命令执行后需要再次输入密码,幵将有空格的密码串识别为登录的数据库。
每条sql语句的后面都有一个分号。
退出数据库:
MariaDB [(none)]> exit
3.清空防火墙规则并关闭selinux
[[email protected] html]# iptables -F
临时关闭selinux:
[[email protected] html]# setenforce 0
永久关闭selinux:
[[email protected] html]# vim /etc/selinux/config
改成disabled并重启虚拟机。
2 搭建ansible环境
2.1 安装ansible服务
实验环境:
Ansible服务器:centos7.5 192.168.1.118
Ansible节点1:centos7.7 192.168.1.119
Ansible节点2:centos7.5 192.168.1.118
在centos7.5上安装ansible
安装EPEL 仓库
Ansible 仓库默认不在 yum 仓库中,因此我们需要使用下面的命令启用 epel 仓库。
[[email protected] ~]# yum install epel-release -y
如果无法安装epel仓库,应该是yum源的问题,直接用wget下载:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum repolist
使用yum安装ansible
[[email protected] ~]# yum install ansible -y
安装完成后,检查 ansible 版本:
[[email protected] ~]# ansible --version
2.2 定义主机清单
1、基于端口,用户,密码定义主机清单
[[email protected] ~]# vim /etc/ansible/hosts
在文档中添加以下内容:
[web-servers]
192.168.1.119 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
[[email protected] ~]# ansible -i /etc/ansible/hosts web-servers -m ping
-i # 指定host 文件的路径,默认是在 /etc/ansible/hosts
-m # 指定使用ping 模块
发现报错:
解决:
手动连接主机清单中的主机,这样就可以在 ansible 服务器上保存目标主机的 fingerprint 指纹。后面就可以正常连接了。
[[email protected] ~]# ssh [email protected]
[[email protected] ~]# exit
logout
Connection to 192.168.1.119 closed.
[[email protected] ~]# ansible -i /etc/ansible/hosts web-servers -m ping
192.168.1.119 | SUCCESS => { #表示执行成功
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false, #状态未改变,因为ping命令不改变被管理的服务器状态
“ping”: “pong”
}
2、基于 ssh **来访问定义主机清单
一般来说,使用明文密码不安全,所以增加主机无密码访问。
在 Ansible 服务端生成**,并且复制公钥到节点中。
[[email protected] ~]# ssh-******
一直按回车即可:
使用 ssh-copy-id 命令来复制 Ansible 公钥到被管理的两个节点:
[[email protected] ~]# ssh-copy-id [email protected]
[[email protected] ~]# ssh-copy-id [email protected]
这样直接使用ssh+空格+被管理节点IP就可以远程登录了:
[[email protected] ~]# ssh 192.168.1.119
Last login: Sun Feb 23 16:56:44 2020 from 192.168.1.118
[[email protected] ~]# exit
logout
Connection to 192.168.1.119 closed.
[[email protected] ~]#
可以把主机清单作如下修改:
[[email protected] ~]# vim /etc/ansible/hosts
删除:
[web-servers]
192.168.1.119 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
:wq #保存退出
2.3 在 Ansible 服务端运行命令
1)ping 模块检查网络连通性
command 模块执行 shell命令,command:作为 ansible 的默认模块,可以运行进程权限范围内的所有 shell命令
使用ping检查web-servers组的连通性
[[email protected] ~]# ansible -i /etc/ansible/hosts web-servers -m ping
[[email protected] ~]# ansible web-servers -m ping #不指定,默认使用/etc/ansible/hosts
2)检查ansible节点运行时间
[[email protected] ~]# ansible -m command -a “uptime” web-servers
192.168.1.119 | CHANGED | rc=0 >>
17:21:58 up 1:11, 3 users, load average: 0.00, 0.01, 0.05
192.168.1.118 | CHANGED | rc=0 >>
01:21:58 up 1:11, 3 users, load average: 0.01, 0.09, 0.08
3)检查节点的内核版本
[[email protected] ~]# ansible -m command -a “uname -r” web-servers
4)给节点增加用户
[[email protected] ~]# ansible -m command -a “useradd wangbin2” web-servers
筛选passwd文件中相关用户:
[[email protected] ~]# ansible -m command -a “grep wangbin2 /etc/passwd” web-servers
说明添加成功。
5)将 df 命令在所有节点执行后,重定向输出到本机的/tmp/command-output.txt 文件中
[[email protected] ~]# ansible -m command -a “df -Th” web-servers > /tmp/command-output.txt
[[email protected] ~]# cat /tmp/command-output.txt
3 使用Playbook批量部署两台LAMP环境
3.1 playbook使用步骤
playbook是一个不同于使用ansible命令行执行方式的模式,功能更强大更灵活。
1)在playbooks 中定义任务:
- name: task description #任务描述信息
module_name: module_args #需要使用的模块名字: 模块参数
2)ansible-playbook 执行 命令:
[[email protected] ~]# ansible-playbook site.yml
playbook是由一个或多个"play"组成的列表。play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task定义好的角色。
github上的实例 https://github.com/ansible/ansible-examples
3.2 实战1:使用Playbook批量部署多台LAMP环境
Playbook常用文件夹作用:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件,handlers ['hændləz] 处理程序
meta:角色定义,可留空; meta ['metə] 元
tasks:需要进行的执行的任务;
templates:用于执行lamp安装的模板文件,一般为脚本; templates ['templɪts] 模板
vars:本次安装定义的变量
首先,在ansible服务器上安装LAMP环境,然后,再将配置文件通过ansible拷贝到远程主机上
第一步:安装httpd软件
[[email protected] ~]# yum install httpd -y
第二步:安装mariadb客户端和服务器
[[email protected] ~]# yum install mariadb mariadb-server
创建目录存放数据
[[email protected] ~]# mkdir -p /mydata/data
[[email protected] ~]# chown -R mysql:mysql /mydata/
修改配置文件以改变数据默认存放目录
[[email protected] ~]# vim /etc/my.cnf
改:
2 datadir=/var/lib/mysql
为
2 datadir=/mydata/data
重启MariaDB
[[email protected] ~]# systemctl restart mariadb.service
第三步:安装PHP和php-mysql模块
[[email protected] ~]# yum install php php-mysql
第四步:提供PHP的测试页
[[email protected] ~]# vim /var/www/html/index.php
启动Apache,清空防火墙规则
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# iptables -F
在浏览器中访问192.168.1.118:
确保已经出现上面的测试页,而且,要看到MySQL已经被整合进来了,才能进行下一步操作
4.3 使用playbook创建一个LAMP构建的任务
1)创建相关文件
[[email protected] ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers} #-p 递归创建 -v显示创建过程
将搭建好的httpd和MySQL配置文件拷贝到对应的目录下
[[email protected] ~]# cd /etc/ansible/
[[email protected] ansible]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
[[email protected] ansible]# cp /etc/my.cnf lamp/roles/mysql/files/
写prepare(前期准备)的playbooks
[[email protected] ansible]# vim lamp/roles/prepare/tasks/main.yml
输入以下内容:
(显示点的地方都为减号)
- name: delete yum config
shell: rm -rf /etc/yum.repos.d/* #delete - name: provide yumrepo file
shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo - name: clean the yum repo
shell: yum clean all - name: clean the iptables
shell: iptables -F
注:yml的语法比较严格,各个字段一定要严格对齐,否则执行时候会报错。
2)构建httpd的任务
[[email protected] roles]# vim httpd/tasks/main.yml
输入以下内容: - name: web server install
yum: name=httpd state=present - name: provide test page
copy: src=index.php dest=/var/www/html - name: delete apache config
shell: rm -rf /etc/httpd/conf/httpd.conf - name: provide configuration file
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
3)构建httpd 的handlers
[[email protected] roles]# vim httpd/handlers/main.yml
输入以下内容: - name: restart httpd
service: name=httpd enabled=yes state=restarted
4)部署MySQL数据库
[[email protected] roles]# vim mysql/tasks/main.yml
输入以下内容: - name: install the mysql
yum: name=mariadb-server state=present - name: mkdir date directory
shell: mkdir -p /mydata/data #创建挂载点目录 - name: provide configration file
copy: src=my.cnf dest=/etc/my.cnf #提供mysql的配置文件 - name: chage the owner
shell: chown -R mysql:mysql /mydata/* #更改属主和属组 - name: start mariadb
service: name=mariadb enabled=yes state=started #启动mysql服务
5)构建PHP的任务
[[email protected] roles]# vim php/tasks/main.yml
输入以下内容 - name: install php
yum: name=php state=present - name: install php-mysql
yum: name=php-mysql state=present
6)定义整个任务
[[email protected] roles]# vim site.yml
输入以下内容: - name: LAMP build
remote_user: root
hosts: web-servers
roles:- prepare
- mysql
- php
- httpd
注意:空格必须严格对齐!!!
开始部署:
[[email protected] roles]# ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml
部署成功,在浏览器访问这两天节点主机: