使用自动化运维工具Ansible集中管理服务器

1 安装MySQL数据库并去除安全隐患

1.1 安装完mariadb-server后,运行mysql_secure_installation去除安全隐患

mysql_secure_installation会执行几个设置:
(1)为root用户设置密码
(2)删除匿名账号
(3)取消root用户远程登录
(4)删除test库和对test库的访问权限
(5)刷新授权表使修改生效
进入安全设置向导:
[root@localhost Packages]# mysql_secure_installation
在这里插入图片描述
初始root用户没有密码,直接回车。为root设置密码123456:
在这里插入图片描述
删除匿名用户:
在这里插入图片描述
不允许root用户远程登录:
在这里插入图片描述
删除test数据库:
在这里插入图片描述
重新加载权限表:
在这里插入图片描述
如果不做安全配置,设置root用户密码的方法:
[root@xuegod63 ~]# mysqladmin -u root password “123456”

1.2 测试数据库是否正常

登录数据库并显示所有数据库:
[root@localhost ~]# mysql -u root -p123456
在这里插入图片描述
注意:在-p选项和密码之间不能有空格,否则在命令执行后需要再次输入密码,幵将有空格的密码串识别为登录的数据库。
每条sql语句的后面都有一个分号。
退出数据库:
MariaDB [(none)]> exit
3.清空防火墙规则并关闭selinux
[root@localhost html]# iptables -F
临时关闭selinux:
[root@localhost html]# setenforce 0
永久关闭selinux:
[root@localhost 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 仓库。
[root@localhost ~]# 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
[root@localhost ~]# yum install ansible -y
在这里插入图片描述
安装完成后,检查 ansible 版本:
[root@localhost ~]# ansible --version
在这里插入图片描述

2.2 定义主机清单

1、基于端口,用户,密码定义主机清单
[root@localhost ~]# vim /etc/ansible/hosts
在文档中添加以下内容:
[web-servers]
192.168.1.119 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
在这里插入图片描述
[root@localhost ~]# ansible -i /etc/ansible/hosts web-servers -m ping
-i # 指定host 文件的路径,默认是在 /etc/ansible/hosts
-m # 指定使用ping 模块
发现报错:
在这里插入图片描述
解决:
手动连接主机清单中的主机,这样就可以在 ansible 服务器上保存目标主机的 fingerprint 指纹。后面就可以正常连接了。
[root@localhost ~]# ssh root@192.168.1.119
在这里插入图片描述
[root@localhost ~]# exit
logout
Connection to 192.168.1.119 closed.
[root@localhost ~]# 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 服务端生成密钥,并且复制公钥到节点中。
[root@localhost ~]# ssh-keygen
一直按回车即可:
在这里插入图片描述
使用 ssh-copy-id 命令来复制 Ansible 公钥到被管理的两个节点:
[root@localhost ~]# ssh-copy-id root@192.168.1.119
[root@localhost ~]# ssh-copy-id root@192.168.1.118
在这里插入图片描述
这样直接使用ssh+空格+被管理节点IP就可以远程登录了:
[root@localhost ~]# ssh 192.168.1.119
Last login: Sun Feb 23 16:56:44 2020 from 192.168.1.118
[root@localhost ~]# exit
logout
Connection to 192.168.1.119 closed.
[root@localhost ~]#
可以把主机清单作如下修改:
[root@localhost ~]# 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组的连通性
[root@localhost ~]# ansible -i /etc/ansible/hosts web-servers -m ping
[root@localhost ~]# ansible web-servers -m ping #不指定,默认使用/etc/ansible/hosts
在这里插入图片描述
2)检查ansible节点运行时间
[root@localhost ~]# 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)检查节点的内核版本
[root@localhost ~]# ansible -m command -a “uname -r” web-servers
在这里插入图片描述
4)给节点增加用户
[root@localhost ~]# ansible -m command -a “useradd wangbin2” web-servers
在这里插入图片描述
筛选passwd文件中相关用户:
[root@localhost ~]# ansible -m command -a “grep wangbin2 /etc/passwd” web-servers
在这里插入图片描述
说明添加成功。
5)将 df 命令在所有节点执行后,重定向输出到本机的/tmp/command-output.txt 文件中
[root@localhost ~]# ansible -m command -a “df -Th” web-servers > /tmp/command-output.txt
[root@localhost ~]# 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 执行 命令:
[root@localhost ~]# 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软件
[root@localhost ~]# yum install httpd -y
第二步:安装mariadb客户端和服务器
[root@localhost ~]# yum install mariadb mariadb-server
创建目录存放数据
[root@localhost ~]# mkdir -p /mydata/data
[root@localhost ~]# chown -R mysql:mysql /mydata/
修改配置文件以改变数据默认存放目录
[root@localhost ~]# vim /etc/my.cnf
改:
2 datadir=/var/lib/mysql

2 datadir=/mydata/data
重启MariaDB
[root@localhost ~]# systemctl restart mariadb.service

第三步:安装PHP和php-mysql模块
[root@localhost ~]# yum install php php-mysql
第四步:提供PHP的测试页
[root@localhost ~]# vim /var/www/html/index.php

<?php phpinfo(); ?>

启动Apache,清空防火墙规则
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# iptables -F
在浏览器中访问192.168.1.118:
在这里插入图片描述
确保已经出现上面的测试页,而且,要看到MySQL已经被整合进来了,才能进行下一步操作

4.3 使用playbook创建一个LAMP构建的任务

1)创建相关文件
[root@localhost ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers} #-p 递归创建 -v显示创建过程
在这里插入图片描述
将搭建好的httpd和MySQL配置文件拷贝到对应的目录下
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
[root@localhost ansible]# cp /etc/my.cnf lamp/roles/mysql/files/

写prepare(前期准备)的playbooks
[root@localhost 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的任务
    [root@localhost 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
    [root@localhost roles]# vim httpd/handlers/main.yml
    输入以下内容:
  • name: restart httpd
    service: name=httpd enabled=yes state=restarted
    在这里插入图片描述
    4)部署MySQL数据库
    [root@localhost 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的任务
    [root@localhost 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)定义整个任务
    [root@localhost roles]# vim site.yml
    输入以下内容:
  • name: LAMP build
    remote_user: root
    hosts: web-servers
    roles:
    • prepare
    • mysql
    • php
    • httpd
      在这里插入图片描述
      注意:空格必须严格对齐!!!
      开始部署:
      [root@localhost roles]# ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml
      在这里插入图片描述
      部署成功,在浏览器访问这两天节点主机:
      在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值