Ansible-lamp架构部署
文章目录
- Ansible-lamp架构部署
-
- 准备工作
-
- 部署Apache
-
- 编写角色模板playbook
-
- 编写yum.sh脚本
- 编写变量pkgs
- 放入所需安装包
- 编写编译安装脚本
- 编写环境变量的脚本
- 编写单元文件
- 编写执行playbook
- 执行
- 验证
- 部署mysql
-
- 编写角色模板playbook
- 传输所需mysql安装包
-
- 编写初始化脚本
- 新建模板文件my.cnf.j2
- 编写模板mysql.service.j2--服务单元文件
- 编写环境变量脚本
- 编写执行playbook
- 执行
- 部署php
-
- 编写角色模板playbook
-
- 导入所需php安装包
- 设置变量phppkgs
- 创建编译安装脚本php.sh
- 新建测试网页模板index.php.j2
- 编写执行playbook
- 执行
- 验证
准备工作
[root@server ~]# cd /etc/ansible/
[root@server ansible]# ls
ansible.cfg hosts roles
[root@server ansible]# vim hosts
node1
创建lamp的juese
[root@server ansible]# cd roles/
[root@server roles]# ls
[root@server roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@server roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@server roles]# ansible-galaxy init php2
- Role php was created successfully
[root@server roles]# ls
apache mysql php
部署Apache
编写角色模板playbook
[root@server roles]# cd apache/
[root@server apache]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server apache]# vim tasks/main.yml
[root@server apache]# cat tasks/main.yml
---
# tasks file for apache
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
- name: stop selinux1
shell:
cmd: setenforce 0
- name: set yum
script: yum.sh
- name: install pkgs
yum:
name: "{{ pkgs }}"
state: present
- name: unzip1
unarchive:
src: apr-1.7.0.tar.gz
dest: /opt/
- name: unzip2
unarchive:
src: apr-util-1.6.1.tar.gz
dest: /opt/
- name: unzip3
unarchive:
src: httpd-2.4.54.tar.gz
dest: /opt/
- name: useradd apache
user:
name: apache
system: yes
shell: /sbin/nologin
create_home: no
state: present
- name: apache.sh
script: apache.sh
- name: httpd.sh
script: httpd.sh
- name: cp config
template:
src: httpd.service.j2
dest: /usr/lib/systemd/system/httpd.service
- name: apply config
shell:
cmd: systemctl daemon-reload
- name: restart httpd
service:
name: httpd
state: started
enabled: yes
编写yum.sh脚本
[root@server apache]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server apache]# vim files/yum.sh
[root@server apache]# cat files/yum.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
编写变量pkgs
[root@server apache]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server apache]# vim vars/main.yml
[root@server apache]# cat vars/main.yml
---
# vars file for apache
pkgs:
- bzip2
- vim
- make
- wget
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- libxml2-devel
放入所需安装包
[root@server apache]# cd files/
[root@server files]# ls
yum.sh
[root@server files]# rz -E
rz waiting to receive.
[root@server files]# rz -E
rz waiting to receive.
[root@server files]# rz -E
rz waiting to receive.
[root@server files]# ls
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz yum.sh
编写编译安装脚本
[root@server files]# vim apache.sh
[root@server files]# cat apache.sh
#!/bin/bash
cd /opt/apr-1.7.0
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install
cd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install
cd /opt/httpd-2.4.54
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
make
make install
编写环境变量的脚本
[root@server files]# vim httpd.sh
[root@server files]# cat httpd.sh
export PATH=/usr/local/apache/bin/:$PATH
编写单元文件
[root@server apache]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server apache]# cd templates/
[root@server templates]# vim httpd.service.j2
[root@server templates]# cat httpd.service.j2
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
编写执行playbook
[root@server ansible]# vim apache.yml
[root@server ansible]# cat apache.yml
---
- name: use apache role
hosts: node1
roles:
- apache
执行
[root@server ansible]# ansible-playbook apache.yml
PLAY [use apache role] *****************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [node1]
TASK [apache : stop firewalld] *********************************************************************
ok: [node1]
TASK [apache : stop selinux] ***********************************************************************
ok: [node1]
TASK [apache : stop selinux1] **********************************************************************
changed: [node1]
TASK [apache : set yum] ****************************************************************************
changed: [node1]
TASK [apache : install pkgs] ***********************************************************************
ok: [node1]
TASK [apache : unzip1] *****************************************************************************
changed: [node1]
TASK [apache : unzip2] *****************************************************************************
changed: [node1]
TASK [apache : unzip3] *****************************************************************************
changed: [node1]
TASK [useradd apache] ******************************************************************************
changed: [node1]
TASK [apache.sh] ***********************************************************************************
changed: [node1]
TASK [apache : httpd.sh] ***************************************************************************
changed: [node1]
TASK [apache : cp config] **************************************************************************
changed: [node1]
TASK [apache : apply config] ***********************************************************************
changed: [node1]
TASK [apache : restart httpd] **********************************************************************
changed: [node1]
PLAY RECAP *****************************************************************************************
node1 : ok=15 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证
[root@server ansible]# curl 192.168.87.129
It works!
部署mysql
编写角色模板playbook
[root@server ansible]# cd roles/mysql/
[root@server mysql]# cd tasks/
[root@server tasks]# cat main.yml
---
# tasks file for mysql
- name: create mysql
user:
name: mysql
system: yes
shell: /sbin/nologin
create_home: no
state: present
- name: install pkgs
yum:
name: "libncurses*"
state: present
- name: unzip
unarchive:
src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
dest: /usr/local/
- name: create link
file:
src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64
dest: /usr/local/mysql
owner: mysql
group: mysql
state: link
- name: create data directory
file:
path: /opt/data
owner: mysql
group: mysql
state: directory
- name: mysql-chushi.sh
script: mysql-chushi.sh
- name: cp config
template:
src: my.cnf.j2
dest: /etc/my.cnf
- name: replace file1
replace:
path: /usr/local/mysql/support-files/mysql.server
regexp: "#^(basedir=).*"
replace: "basedir=/usr/local/mysql"
- name: replace file2
replace:
path: /usr/local/mysql/support-files/mysql.server
regexp: "#^(datadir=).*"
replace: "datadir=/opt/data"
- name: cp mysql.service
template:
src: mysql.service.j2
dest: /usr/lib/systemd/system/mysqld.service
- name: apply config
shell:
cmd: systemctl daemon-reload
- name: restart mysqld
service:
name: mysqld
state: started
enabled: yes
- name: set mysql passwd
shell:
cmd: /usr/local/mysql/bin/mysql -uroot -e "set password=password('redhat')"
- name: set mysql env
script: mysql.sh
传输所需mysql安装包
[root@server mysql]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server mysql]# cd files/
[root@server
files]# ls
[root@server files]# rz -E
rz waiting to receive.
[root@server files]# ls
mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
编写初始化脚本
[root@server files]# vim mysql-chushi.sh
[root@server files]# cat mysql-chushi.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
新建模板文件my.cnf.j2
[root@server files]# cd ..
[root@server mysql]# cd templates
[root@server templates]# vim my.cnf.j2
[root@server templates]# cat my.cnf.j2
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
编写模板mysql.service.j2–服务单元文件
[root@server templates]# ls
my.cnf.j2
[root@server templates]# vim mysql.service.j2
[root@server templates]# cat mysql.service.j2
[Unit]
Description=mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
编写环境变量脚本
[root@server files]# vim mysql.sh
[root@server files]# cat mysql.sh
#!/bin/bash
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh
编写执行playbook
[root@server ansible]# vim mysql.yml
[root@server ansible]# cat mysql.yml
---
- name: use mysql role
hosts: node1
roles:
- mysql
执行
[root@server ansible]# ansible-playbook mysql.yml
PLAY [use mysql role] ******************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [node1]
TASK [create mysql] ********************************************************************************
changed: [node1]
TASK [mysql : install pkgs] ************************************************************************
changed: [node1]
TASK [mysql : unzip] *******************************************************************************
changed: [node1]
TASK [mysql : create link] *************************************************************************
changed: [node1]
TASK [mysql : create data directory] ***************************************************************
changed: [node1]
TASK [mysql-chushi.sh] *****************************************************************************
changed: [node1]
TASK [mysql : cp config] ***************************************************************************
changed: [node1]
TASK [mysql : replace file1] ***********************************************************************
ok: [node1]
TASK [mysql : replace file2] ***********************************************************************
ok: [node1]
TASK [cp mysql.service] ****************************************************************************
changed: [node1]
TASK [mysql : apply config] ************************************************************************
changed: [node1]
TASK [restart mysqld] ******************************************************************************
changed: [node1]
TASK [set mysql passwd] ****************************************************************************
changed: [node1]
TASK [set mysql env] *******************************************************************************
changed: [node1]
PLAY RECAP *****************************************************************************************
node1 : ok=15 changed=12 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
部署php
编写角色模板playbook
[root@server ansible]# cd roles/php/
[root@server php]# vim tasks/main.yml
[root@server php]# cat tasks/main.yml
---
# tasks file for php
- name: install pkgs
yum:
name: "{{ phppkgs }}"
state: present
- name: unzip
unarchive:
src: php-7.1.10.tar.gz
dest: /opt/
- name: php.sh
script: php.sh
- name: modify apache config
replace:
path: /etc/httpd24/httpd.conf
regexp: "index.html"
replace: "index.php index.html"
- name: rm index.html
shell:
cmd: rm -rf /usr/local/apache/htdocs/index.html
- name: edit index.php
template:
src: index.php.j2
dest: /usr/local/apache/htdocs/index.php
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
导入所需php安装包
[root@server php]# cd files/
[root@server files]#
[root@server files]# ll
total 0
[root@server files]# rz -E
rz waiting to receive.
[root@server files]# ls
php-7.1.10.tar.gz
设置变量phppkgs
[root@server ansible]# cd roles/php/
[root@server php]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@server php]# vim vars/main.yml
[root@server php]# cat vars/main.yml
---
# vars file for php
phppkgs:
[root@server vars]# cat main.yml
---
# vars file for php
phppkgs:
- libxml2
- libxml2-devel
- openssl
- openssl-devel
- bzip2
- bzip2-devel
- curl
- curl-devel
- libcurl
- libcurl-devel
- libicu-devel
- libjpeg
- libjpeg-devel
- libpng
- libpng-devel
- libzip-devel
- openldap-devel
- pcre-devel
- freetype
- freetype-devel
- gmp
- gmp-devel
- readline
- readline-devel
- libxslt
- libxslt-devel
- sqlite-devel
- php-mysqlnd
- zlib
- zlib-devel
创建编译安装脚本php.sh
[root@server files]# vim php.sh
[root@server files]# cat php.sh
#!/bin/bash
cd /opt/php-7.1.10
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/l ocal/apache/bin/apxs \
--with-mysql-sock=/tmp/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make
make install
cp php.ini-development /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini
echo "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf
新建测试网页模板index.php.j2
[root@server php]# vim templates/index.php.j2
[root@server php]# cat templates/index.php.j2
编写执行playbook
[root@server ansible]# vim php.yml
[root@server ansible]# cat php.yml
---
- name: use php role
hosts: node1
roles:
- php
执行
[root@server ansible]# ansible-playbook php.yml
PLAY [use php role] ********************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [node1]
TASK [php : install pkgs] **************************************************************************
changed: [node1]
TASK [php : unzip] *********************************************************************************
changed: [node1]
TASK [php.sh] **************************************************************************************
changed: [node1]
TASK [php : modify apache config] ******************************************************************
changed: [node1]
TASK [php : rm index.html] *************************************************************************
[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: [node1]
TASK [edit index.php] ******************************************************************************
changed: [node1]
TASK [php : restart httpd] *************************************************************************
changed: [node1]
PLAY RECAP *****************************************************************************************
node1 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证
![Ansible-lamp架构部署_第1张图片](http://img.e-com-net.com/image/info8/0592051c8b344b12be9ecc826c871e2c.jpg)