linux-ansible-06 ansible-playbook部署LNMP

linux-ansible-06 ansible-playbook部署LNMP

实验准备

实验涉及到了unarchive模块,这个模块之前没有学到,这里加以补充。

ansible unarchive模块

unarchive模块:http://docs.ansible.com/ansible/unarchive_module.html

功能:解压缩

用法

1、将ansible主机上的压缩包在本地解压缩后传到远程主机上,这种情况下,copy=yes. 本地解压缩,解压缩位置不是默认的目录,没找到或传完删了 后传到远程主机

2、将远程主机上的某个压缩包解压缩到指定路径下。这种情况下,需要设置copy=no 远程主机上面的操作,不涉及ansible服务端

参数

copy:默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件

src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no

dest:远程主机上的目标路径

mode:设置解压缩后的文件权限

实验环境

准备两台干净的虚拟机

node1(ansible管理机):192.168.141.53

node2(客户端):192.168.141.69

配置准备

node1安装ansible

1
2
[root@node1 ~]# yum install epel-release -y
[root@node1 ~]# yum install ansible -y

服务端修改主机清单配置文件host

1
2
3
4
5
6
[root@node1 ~]# vim /etc/ansible/hosts

#在文档末尾加上一下这几行

[node]
node2

服务端修改host文件

1
2
3
4
5
[root@node1 ~]# vim /etc/hosts

#在文档中加入下列一行

192.168.141.69 node2

实现管理机对所有客户端的免密钥登录—ssh-key

在node1生成密钥

#提示生成的密钥放在/root/.ssh/id_dsa

#提示是否给生成的密钥再加密一次,回车即可

#让你再确认一次,回车即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@node1 ~]# 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:ekKryIWOOpY+BnW5QhineRozN2nLqYz1EEwM8fa1BoE root@node1
The key's randomart image is:
+---[RSA 2048]----+
|o+ .. |
|..E . |
| Oo.... |
|B.@.oo . |
| % *..+ S |
|o B..o o |
|+oo+. + . |
|+X o.. o |
|Bo= . |
+----[SHA256]-----+


[root@node1 ~]# ll /root/.ssh/
总用量 8
-rw-------. 1 root root 1675 4月 11 11:30 id_rsa #生成后的私钥文件
-rw-r--r--. 1 root root 392 4月 11 11:30 id_rsa.pub #公钥文件
[root@node1 ~]#

把公钥发送到客户端机器上,第一次连接要输入yes,记录主机名跟IP,然后输出对方的密码即可

1
[root@node1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.141.69

#公钥发送过去之后,登录对方的机器不用密码了。

1
2
3
4
5
6
7
[root@node1 ~]# ssh 192.168.141.69
Last login: Thu Apr 11 11:24:30 2019 from 192.168.141.1
[root@node2 ~]#
[root@node2 ~]# exit
登出
Connection to 192.168.141.69 closed.
[root@node1 ~]#

ansible测试

第一次执行ansible的命令需要加yes回车,但是之后就可以直接执行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@node1 ~]# ansible node2 -m command -a "echo hello"
The authenticity of host 'node2 (192.168.141.69)' can't be established.
ECDSA key fingerprint is SHA256:cFZVkPh6j57TyfDgnCxFrAXPxeifK3T2cUCZGde78uE.
ECDSA key fingerprint is MD5:74:27:63:2b:dd:8e:28:b5:a0:fa:01:5d:75:5d:8e:b1.
Are you sure you want to continue connecting (yes/no)? yes
node2 | CHANGED | rc=0 >>
hello

[root@node1 ~]# ansible node2 -m command -a "echo hello"
node2 | CHANGED | rc=0 >>
hello

[root@node1 ~]#

实验步骤

node1上roles文件夹里定义文件结构tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@node1 ~]# yum install tree -y

[root@node1 ~]# mkdir -pv /etc/ansible/roles/{mysql,php,web}/{files,templates,tasks,handlers,vars,meta,default}

[root@node1 ~]# cd /etc/ansible/roles/
[root@node1 roles]# ls
mysql php web
[root@node1 roles]# tree
.
├── mysql
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── php
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
└── web
├── default
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars

24 directories, 0 files
[root@node1 roles]#

可以看到roles文件夹下主要有三个文件夹:web、php、mysql。

web

在files文件夹准备好wordpress的安装包,准备好web.conf作为配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@node1 roles]# cd web/
[root@node1 web]# ls
default files handlers meta tasks templates vars
[root@node1 web]# cd files
[root@node1 files]# wget http://wordpress.org/latest.tar.gz
[root@node1 files]# vim web.conf
[root@node1 files]# cat web.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name www.static.com;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
[root@node1 files]# ls
latest.tar.gz web.conf
[root@node1 files]#

在tasks、handlers文件夹下创建编辑main.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@node1 files]# cd ..
[root@node1 web]# ls
default files handlers meta tasks templates vars
[root@node1 web]# cd tasks
[root@node1 tasks]# vim main.yml
[root@node1 tasks]# cat main.yml
- name: add user nginx
user: name=nginx group=root uid=666
- name: install epel-release
yum: name=epel-release state=latest
- name: disable selinux
selinux: state=disabled
- name: firewalld enabled ports
firewalld: port=80/tcp state=enabled permanent=yes
- name: firewalld reload
command: firewall-cmd --reload
- name: install nginx
yum: name=nginx state=latest
notify: restart nginx
- name: install conf
copy: src=web.conf dest=/etc/nginx/nginx.conf
tags: conf
- name: copy and unzip blog
unarchive: src=latest.tar.gz dest=/usr/share/nginx/html/
- name: set mode
file: name=/usr/share/nginx/html/wordpress recurse=yes owner=nginx group=nginx
- name: start nginx
service: name=nginx state=started enabled=yes
[root@node1 tasks]# cd ..
[root@node1 web]# cd handlers/
[root@node1 handlers]# vim main.yml
[root@node1 handlers]# cat main.yml
- name: restart nginx
service: name=nginx state=restarted
[root@node1 handlers]# cd ..
[root@node1 web]# cd ..
[root@node1 roles]#

php

在tasks文件夹下创建编辑main.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@node1 roles]# cd php/tasks/
[root@node1 tasks]# vim main.yml
[root@node1 tasks]# cat main.yml
- name: install php
yum: name={{ item }} state=present
with_items:
- php-mysql
- php-fpm
- name: start php-fpm
service: name=php-fpm state=started enabled=yes
[root@node1 tasks]# cd ..
[root@node1 php]# tree
.
├── default
├── files
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

7 directories, 1 file
[root@node1 php]# cd ..
[root@node1 roles]#

mysql

在tasks文件夹下创建编辑main.yml,在files文件夹下创建编辑my.cnf。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[root@node1 roles]# cd mysql
[root@node1 mysql]# vim tasks/main.yml
[root@node1 mysql]# cat tasks/main.yml
- name: install mariadb
yum: name={{ item }} state=present
with_items:
- mariadb
- mariadb-server
- name: install conf
copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
service: name=mariadb state=started enabled=yes
- name: mysql
command: mysql -e "create database wordpress;grant all on wordpress.* to wordpress@'192.168.141.69' identified by '123456';"
- name: command
command: mysql -e "grant replication slave,replication client on *.* to 'backuper'@'192.168.141.69' identified by 'backuper';"
[root@node1 mysql]#
[root@node1 mysql]# vim files/my.cnf
[root@node1 mysql]# cat files/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master_bin
server_id=1
innodb_file_per_table=ON
skip_name-resolve=ON

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[root@node1 mysql]# tree
.
├── default
├── files
│   └── my.cnf
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

7 directories, 2 files
[root@node1 mysql]# cd ..
[root@node1 roles]#

roles

在roles文件夹创建编辑调用role的文件lnmp.yml。

1
2
3
4
5
6
7
8
9
[root@node1 roles]# vim lnmp.yml
[root@node1 roles]# cat lnmp.yml
- hosts: node2
remote_user: root
roles:
- php
- web
- mysql
[root@node1 roles]#

部署

1
[root@node1 roles]# ansible-playbook lnmp.yml

执行结果中,警告不用管,没有报错,执行完毕。

接下来我们到node2上验证一下防火墙和selinux状态

1
2
3
4
5
[root@node2 ~]# firewall-cmd --list-ports
80/tcp
[root@node2 ~]# getenforce
Permissive
[root@node2 ~]#

接下来验证lnmp架构的可用性

  浏览器打开http://192.168.141.69/wordpress ,即可看到wordpress的安装界面了,其中192.168.141.69是node2的ip地址。

  至于后面在这个网页端安装wordpress就省略了,本次验证已经完毕,结果是我们通过ansible的playbook成功部署出了一个采用LNMP架构的网站。

欢迎打赏,谢谢
------ 本文结束------
0%