playbook部署lamp
本次环境说明:
系统平台 | 主机的IP地址 | 需要安装的服务 |
---|---|---|
redhat8 | 192.168.153.10 | ansible |
redhat8 | 192.168.153.11 | httpd |
redhat8 | 192.168.153.12 | mysql |
redhat8 | 192.168.153.13 | php |
准备工作
配置yum源"阿里云官方镜像网站"
//配置centos源
[root@ansible ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@ansible ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo
//配置epel源
[root@ansible ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@ansible ~]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@ansible ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@ansible ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/epel*
//清理yum缓存,建立缓存
[root@ansible ~]# yum clean all
[root@ansible ~]# yum makecach
设置主控机和三台受控主机
//映射主机名
[root@ansible ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.153.10 ansible
192.168.153.11 httpd
192.168.153.12 mysql
192.168.153.13 php
//更改配置文件修改为inventory
[root@ansible ~]# vim /etc/ansible/ansible.cfg
# some basic default values...
inventory = /etc/ansible/inventory
//写一个inventory清单文件
[root@ansible ~]# vim /etc/ansible/inventory
[webservers]
httpd
mysql
php
//个三台受控主机设置免密登录
[root@ansible ~]# ssh-keygen -t rsa //生成密钥,直接回车即可
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:OFB9SUGgUTf2KbgfrEnnDe5vDh0OE2AmqeOjC7UMsqw root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| ++*+Bo |
| ..*.=oo . |
| ... ..o o |
| o. . o o |
|o .. .o S B . |
|o= .o o B O . |
|o.o. . o = + |
|... . .. |
|E .. .+o |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id root@192.168.153.11 //httpd主机
[root@ansible ~]# ssh-copy-id root@192.168.153.12 //mysql主机
[root@ansible ~]# ssh-copy-id root@192.168.153.13 //php主机
//测试能够与三台受控机器ping通
[root@ansible ~]# ansible all -m ping
php | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
mysql | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
httpd | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
下载httpd和mysql服务需要的源码包
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
[root@ansible ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@ansible ~]# ls
anaconda-ks.cfg apr-util-1.6.1.tar.gz apr-1.7.0.tar.gz httpd-2.4.46.tar.bz2 mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
开始部署
给三台受控机配置yum
//创建一个lamp目录,在里面写有关lamp项目的配置
[root@ansible ~]# mkdir /root/lamp
//创建一个yum目录,在里面写执行yum命令的脚本
[root@ansible ~]# cd /root/lamp/
[root@ansible lamp]# mkdir yum
//在ansible主控机上编写yum的playbook
[root@ansible ~]# cd /root/lamp/
[root@ansible lamp]# vim yum.yml
---
- hosts: all
tasks:
- name: set yum
copy:
src: /etc/yum.repos.d/
dest: /etc/yum.repos.d/
- name: set key
copy:
src: /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
dest: /etc/pki/rpm-gpg/
- name: stop firewalld
service:
name: firewalld
state: stopped
- name: set yum command
script: /root/lamp/yum/yumconfig.sh
//编写yumconfig.sh的批处理
[root@ansible ~]# vim /root/lamp/yum/yumconfig.sh
#!/bin/bash
yum clean all
yum makecache
#selinux
setenforce 0
执行/root/lamp/yum.yml
[root@ansible ~]# ansible-playbook /root/lamp/yum.yml
PLAY [all] *************************************************************************
TASK [Gathering Facts] *************************************************************
ok: [httpd]
ok: [mysql]
ok: [php]
TASK [set yum] *********************************************************************
changed: [php]
changed: [mysql]
changed: [httpd]
TASK [set key] *********************************************************************
changed: [httpd]
changed: [mysql]
changed: [php]
TASK [stop firewalld] **************************************************************
changed: [mysql]
changed: [php]
changed: [httpd]
TASK [set yum command] *************************************************************
changed: [httpd]
changed: [php]
changed: [mysql]
PLAY RECAP *************************************************************************
httpd : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
mysql : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
php : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
编写httpd的playbook