Ansible自动运维工具 (3)通过PlayBook安装管理LNMP

1.通过playbook 安装管理 LNMP

通过rpm包部署LNMP环境(基于aliyun和epel源),如果要通过源码包部署的话,可以编写shell脚本,通过script模块进行批量部署LNMP

epel镜像-epel下载地址-epel安装教程-阿里巴巴开源镜像站 (aliyun.com)

2.yum安装LNMP环境

首先,我们可以在ansible服务器上安装LNMP环境,然后,再将配置文件通过ansible拷贝到远程主机上

1)安装nginx

 yum -y install nginx     #通过aliyun和epel源
[root@ansible ~]# systemctl start nginx

访问nginx默认测试页:

 2)安装MySQL

yum install -y mariadb-server  mariadb
mkdir -p /data/mysql/data   #创建数据库保存目录
chown -R mysql:mysql /data/mysql/  #修改权限
vim /etc/my.cnf         #修改数据保存目录
[mysqld]
datadir=/data/mysql/data    #修改此行,其他参数保持默认即可

启动 mariadb 

systemctl start mariadb

3)安装 PHP 和 php-fpm模块和php-mysql模块

yum -y install php php-mysql php-fpm
systemctl start php-fpm

4)提供php的测试页

vim /usr/share/nginx/html/index.php
<?php
phpinfo();
?>

5)修改nginx.conf,支持php访问

 vim /etc/nginx/nginx.conf  #在server中添加圈红部分

重启ngixn服务

systemctl restart nginx

6)测试 LNMP 

 

 确保已经出现上面的测试页,而且,要看到MySQL已经被整合进来了,才能进行下一步操作

 3.roles角色

roles相当于我们剧本当中的角色,每个角色都有不同的事情要做,只需调用不同的角色,执行不同需求的任务

Roles 角色:以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files等;相当于函数的调用把各个事件切割成片段来执行

4.基于角色的方式部署 LNMP

当我们在做一个自动化部署案例时,往往不会不会使用默认的配置项以及配置文件,都会单独创建自己的配置文件,也会根据功能不同,划分出不同的目录,因此,一个成熟的ansible项目会具备自己所需求的目录结构

其中有文件有目录: 

ansible.cfg 对于整个项目的配置文件 

hosts 定义主机配置文件

roles 定义角色的目录(名字不能修改)

prepare 角色目录

nginx角色目录

mysql 角色目录

php 角色目录

role_name/   #在相关角色目录下通常要创建如下目录

files/:存储由copy或script等模块调用的文件,例如存放需要同步到远程服务器的源码文件及配置文件等

tasks/:此目录至少有一个名为main.yml(入口文件)的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用

handlers/:此目录中至少应该有一个名为main.yml的文件,用于定义各handler,其它的文件需要由main.yml进行“包含”调用;例如当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件

vars/:此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调

templates/:存储由template模块调用的模板文本。如用于执行lnmp相关配置文件的模板文件

meta/:角色定义,可留空。此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用

site.yml 整个项目的入口文件

webserver.yml  webserver分支的入口文件

注:这些目录、文件都需要自己创建

5.配置playbook创建LNMP构建的任务

1)创建相关目录

mkdir -p /ansible/lnmp/roles/{prepare,nginx,mysql,php}/{tasks,files,templates,vars,meta,handlers}
touch /ansible/lnmp/{ansible.cfg,hosts,site.yml,webserver.yml}

6.配置被管理主机

开始编辑各自的文件,首先是hosts文件

1)设置ssh免密连接

注:设置过请跳过此步骤

 ssh-keygen
ssh-copy-id root@192.168.1.12 
ssh-copy-id root@192.168.1.13

2)定义hosts文件

vim /ansible/lnmp/hosts 
[nginx]
nginx01 ansible_ssh_host=192.168.1.12 ansible_ssh_port=22 ansible_ssh_user=root
nginx02 ansible_ssh_host=192.168.1.13 ansible_ssh_port=22 ansible_ssh_user=root

[mysql]
mysql01 ansible_ssh_host=192.168.1.12 ansible_ssh_port=22 ansible_ssh_user=root

[php]
php01 ansible_ssh_host=192.168.1.12 ansible_ssh_port=22 ansible_ssh_user=root
php02 ansible_ssh_host=192.168.1.13 ansible_ssh_port=22 ansible_ssh_user=root

[lnmp:children]
nginx
mysql
php

说明:

[lnmp:children],它的意思是,lnmp组由别的组成,具体成员是其他组的组名,这样当调用lnmp组时就会调用其他组内的组员了。

测试所有主机的连通性:

 发现出现警告,原因是在执行命令 无法找到lnmp组,我们默认是用的是/etc/ansible/hosts文件,这个文件中并没有定义lnmp组,我们设置的组在根目录(/ansible/lnmp/)的hosts文件内,需要修改ansible.cfg配置文件:

vim /ansible/lnmp/ansible.cfg 
cat /ansible/lnmp/ansible.cfg
[defaults]
host_key_checking = True
inventory = ./hosts

注意:如果要使用相对路径,或要使用/etc/ansible/ansible.cfg,则必须要在项目的根目录下执行命令

测试:

Ansible默认有个配置文件查找顺序, ,Ansible在执行程序时查找配置文件的顺序是 ANSIBLE_CONFIG (环境变量) -> ansible.cfg (当前目录中) -> ~/.ansible.cfg (家目录中的) -> /etc/ansible/ansible.cfg

们的配置文件,位于当前目录,是Ansible查找的第二个选项,因此,当他找到本地的配置文件后,就不会再向下查找了,所以我们本地的配置文件生效

又因为我们本地的配置文件中指定了Inventory的路径,所以针对于当前目录的配置文件来说,当前目录下的hosts文件生效,而不是/etc/ansible/hosts生效

7.配置site.yml

调整好了配置,设置好了主机,接下来将开始写 roles以及Playbook,site.yml是文件的一切任务的入口文件

 vim site.yml 
---
- import_playbook: webserver.yml

Import_playbook意为包含webserver.yml这个文件,当执行到site.yml文件中的这一行时,就会跑去执行webserver.yml里面的内容

8.配置webserver.yml,构建prepare角色

webserver.yml 当中写对应的模块功能需要的角色,也就是Role。

例如:我们要完成lnmp项目的第一个role,也就是prepare角色(prepare角色主要任务是:远程主机配置yum源,关闭防火墙,关闭selinux),webserver.yml文件内容如下:

vim webserver.yml 

 hosts:all的意思是,让所有主机去执行prepare这个role,如果想要针对于组执行role,就将all 换成组名,针对某个主机执行,就写主机别名或IP

roles列表当中的prepare是指,我们要执行 prepare这个 role,需要在roles这个目录下创建一个叫做prepare的文件夹,保证我们的Role能够执行,我们roles选项下的名称就是我们在roles目录中文件夹的名称

 gather_facts: True开启收集主机静态信息,如版本号,操作系统内容等

tags: prepare  设置标签,在执行时可以使用标签从指定位置开始向下执行

prepare内容说明,存在两个目录,一个是tasks,执行对应任务的目录,一个是templates,进行模版文件复制的目录

当执行到对应的Role时,Ansbile就会调用对应Role目下的Playbook,也就是调用我们的tasks,所以我们在对应的Role目录下都会存在一个叫做tasks的目录

 tasks目录当中文件说明,main.yml,任务的入口文件,执行任务时从此文件开始执行,yum.yml、firewalld.yml、selinux.yml完成对应功能的目录

 目录内文件说明,我们把main.yml文件作为task执行的入口文件,帮助我们加载对应的功能,yum.yml文件用于配合yum源,firewalld.yml文件,关闭防火墙,selinux.yml执行关闭selinux

执行tasks时,Ansible默认会找对应tasks下的main.yml文件,这个文件是 tasks的入口文件,所有tasks都是从这个文件开始执行的,所以main.yml文件名不能改变

将要执行的内容写入 main.yml当中

 对于关键字段说明:

import_tasks字段,是在main.yml文件当中导入相关功能文件的内容,来执行相关文件name字段

yum.yml 文件要执行的内容:

---
- name: Delete repo for system
  shell: rm -rf /etc/yum.repos.d/*

- name: Get repo centos6 for aliyun
  get_url:
      url: https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
      dest: /etc/yum.repos.d/CentOS-Base.repo
  when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6")

- name: Get repo centos7 for aliyun
  get_url:
      url: https://mirrors.aliyun.com/repo/Centos-7.repo
      dest: /etc/yum.repos.d/CentOS-Base.repo
  when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "7")

- name: Get epel centos6 for aliyun
  get_url:
      url: http://mirrors.aliyun.com/repo/epel-archive-6.repo
      dest: /etc/yum.repos.d/epel.repo
  when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6")

- name: Get epel centos7 for aliyun
  get_url:
      url: http://mirrors.aliyun.com/repo/epel-7.repo
      dest: /etc/yum.repos.d/epel.repo
  when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "7")

 firewalld.yml文件要执行的内容:

vim firewalld.yml 
---
- name: close firewalld
  service: name=firewalld state=stopped enabled=no

selinux.yml文件要执行的内容:

[root@ansible tasks]# vim selinux.yml
---
- name: forever close selinux
  template: src=config,j2 dest=/etc/selinux/config
  ignore_errors: true

对于关键字说明:

 template字段,指定模版文件,该文件必须存在于prepare角色目录下的templates目录中,起名需要加上.j2结尾,将templates下的文件复制到对应主机的对应目录下,src指定源文件,dest指定目标目录及复制到目录后改成什么名字

生成selinux配置文件的模板,并修改模板文件,永久禁用selinux

cp /etc/selinux/config /ansible/lnmp/roles/prepare/templates/config.j2
vim /ansible/lnmp/roles/prepare/templates/config.j2
SELINUX=disabled  #将enforcing改为disabled

测试结果,需要再次注意,执行ansible-playbook时,需要到ansible项目的根目录下执行

[root@ansible ~]# cd /ansible/lnmp/

[root@ansible lnmp]# ansible-playbook site.yml 

PLAY [prepare system] *******************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [php02]
ok: [nginx02]
ok: [php01]
ok: [mysql01]
ok: [nginx01]

TASK [prepare : Delete repo for system] *************************************************
[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: [php02]
changed: [nginx02]
changed: [php01]
changed: [mysql01]
changed: [nginx01]

TASK [prepare : Get repo centos6 for aliyun] ********************************************
skipping: [nginx01]
skipping: [nginx02]
skipping: [mysql01]
skipping: [php01]
skipping: [php02]

TASK [prepare : Get repo centos7 for aliyun] ********************************************
changed: [nginx02]
ok: [php02]
changed: [php01]
ok: [nginx01]
ok: [mysql01]

TASK [prepare : Get epel centos6 for aliyun] ********************************************
skipping: [nginx01]
skipping: [nginx02]
skipping: [mysql01]
skipping: [php01]
skipping: [php02]

TASK [prepare : Get epel centos7 for aliyun] ********************************************
changed: [nginx01]
ok: [php02]
changed: [nginx02]
ok: [php01]
ok: [mysql01]

TASK [prepare : close firewalld] ********************************************************
ok: [nginx02]
ok: [php02]
ok: [php01]
ok: [mysql01]
ok: [nginx01]

TASK [prepare : forever close selinux] **************************************************
fatal: [nginx01]: FAILED! => {"changed": false, "msg": "Could not find or access 'config,j2'\nSearched in:\n\t/ansible/lnmp/roles/prepare/templates/config,j2\n\t/ansible/lnmp/roles/prepare/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/templates/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/config,j2\n\t/ansible/lnmp/templates/config,j2\n\t/ansible/lnmp/config,j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
...ignoring
fatal: [nginx02]: FAILED! => {"changed": false, "msg": "Could not find or access 'config,j2'\nSearched in:\n\t/ansible/lnmp/roles/prepare/templates/config,j2\n\t/ansible/lnmp/roles/prepare/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/templates/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/config,j2\n\t/ansible/lnmp/templates/config,j2\n\t/ansible/lnmp/config,j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
...ignoring
fatal: [mysql01]: FAILED! => {"changed": false, "msg": "Could not find or access 'config,j2'\nSearched in:\n\t/ansible/lnmp/roles/prepare/templates/config,j2\n\t/ansible/lnmp/roles/prepare/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/templates/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/config,j2\n\t/ansible/lnmp/templates/config,j2\n\t/ansible/lnmp/config,j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
...ignoring
fatal: [php01]: FAILED! => {"changed": false, "msg": "Could not find or access 'config,j2'\nSearched in:\n\t/ansible/lnmp/roles/prepare/templates/config,j2\n\t/ansible/lnmp/roles/prepare/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/templates/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/config,j2\n\t/ansible/lnmp/templates/config,j2\n\t/ansible/lnmp/config,j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
...ignoring
fatal: [php02]: FAILED! => {"changed": false, "msg": "Could not find or access 'config,j2'\nSearched in:\n\t/ansible/lnmp/roles/prepare/templates/config,j2\n\t/ansible/lnmp/roles/prepare/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/templates/config,j2\n\t/ansible/lnmp/roles/prepare/tasks/config,j2\n\t/ansible/lnmp/templates/config,j2\n\t/ansible/lnmp/config,j2 on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
...ignoring

PLAY RECAP ******************************************************************************
mysql01                    : ok=6    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=1   
nginx01                    : ok=6    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=1   
nginx02                    : ok=6    changed=3    unreachable=0    failed=0    skipped=2    rescued=0    ignored=1   
php01                      : ok=6    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=1   
php02                      : ok=6    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=1   

说明:

在测试显示中的 ...ignoring 是我们在selinux.yml文件中配置了 ignore_errors: true,当关闭selinux时远程执行的主机若以关闭selinux则返回值不是0,剧本则不会向下执行,ignore_errors 的值为true是忽略错误。

下面就是我们继续完善我们的项目,首先,在webserver.yml文件将我们要干的事情,全部用角色的身份填写完毕,然后注释掉暂时不执行的内容,以便后续修改。

9.构建nginx角色

 在nginx目录中除了和prepare目录中一样,具备的tasks 和templates 以外,还有一个目录叫做 handlers,该目录使用 notify(通知)handler功能。

1)配置nginx的tasks

 tasks,templates下目录内容和prepare 一样

 目录结构和功能与 prepare类似,文件内容

main.yml 文件内容如下:

vim main.yml 
---
- import_tasks: install.yml
- import_tasks: config.yml
- ipport_tasks: webpage.yml

 install.yml 文件内如下:

vim install.yml
---
- name: install nginx
  yum: name=nginx state=present

 config.yml 文件内容:

vim config.yml
---
- name: config nginx
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx
- name: start nginx
  service: name=nginx enabled=yes state=started

说明:

template 模块和 copy模块作用基本一样,都是把某个文件复制到远端主机,区别在于template 模块可以获取变量的值,而 copy则是把文件内容原封不动的复制过去

notify 模块:消息通知,notify上面的内容如果执行成功,就会发出消息,通知notify下方的内容执行,内容没有执行则不发送通知。

webpage.yml 文件内容:

vim webpage.yml 
---
- name: provide web page
  copy: src=/ansible/lnmp/roles/nginx/files/ dest=/usr/share/nginx/html

2)扩展:notify和 handlers

1. handlers:由特定条件触发 tasks:

- name: TASK_NAME
 module: arguments
 notify: HANDLER_NAME   #notify触发handlers
handlers:
- name: HANDLER_NAME
 module: arguments

例如;

 3)复制相应的安装包和模板到对应目录下

vim /ansible/lnmp/roles/nginx/files/testdb.php
<?php
$link=mysql_connect('192.168.1.12','zhangsan','123456');
if ($link)echo "connection success......";
mysql_close();
?>
 cp /usr/share/nginx/html/index.php  /ansible/lnmp/roles/nginx/files/
 cp /etc/nginx/nginx.conf /ansible/lnmp/roles/nginx/templates/nginx.conf.j2

4)根据修改nginx配置文件模板(这里改的是work进程生成数和监听的端口)

vim /ansible/lnmp/roles/nginx/templates/nginx.conf.j2

 5)构建nginx的 handlers 目录和vars/下的 main.yml

vim /ansible/lnmp/roles/nginx/handlers/main.yml
---
- name: restart nginx
  service: name=nginx state=restarted
vim /ansible/lnmp/roles/nginx/vars/main.yml
---
ngxport: 80   #定义变量的值

打开nginx角色注释:

vim /ansible/lnmp/webserver.yml

执行测试:

[root@ansible lnmp]# ansible-playbook site.yml -t nginx

PLAY [prepare system] *******************************************************************************************

PLAY [install and config nginx] *********************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [nginx02]
ok: [nginx01]

TASK [install nginx] ********************************************************************************************
changed: [nginx02]
changed: [nginx01]

TASK [config nginx] *********************************************************************************************
changed: [nginx02]
changed: [nginx01]

TASK [start nginx] **********************************************************************************************
changed: [nginx02]
changed: [nginx01]

TASK [nginx : provide web page] *********************************************************************************
changed: [nginx02]
changed: [nginx01]

PLAY RECAP ******************************************************************************************************
nginx01                    : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
nginx02                    : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/tasks/install.yml 
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/tasks/
config.yml   install.yml  main.yml     webpage.yml  
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/tasks/main.yml 
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/tasks/config.yml 
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/handlers/
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/handlers/main.yml 
ansible.cfg    hosts          roles/         site.yml       webserver.yml  
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/handlers/main.yml 
ansible.cfg    hosts          roles/         site.yml       webserver.yml  
[root@ansible lnmp]# ls /ansible/lnmp/roles/nginx/handlers/
main.yml
[root@ansible lnmp]# vim /ansible/lnmp/roles/nginx/handlers/main.yml 
[root@ansible lnmp]# ansible-playbook site.yml -t nginx

PLAY [prepare system] *******************************************************************************************

PLAY [install and config nginx] *********************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [nginx02]
ok: [nginx01]

TASK [install nginx] ********************************************************************************************
ok: [nginx02]
ok: [nginx01]

TASK [config nginx] *********************************************************************************************
ok: [nginx02]
ok: [nginx01]

TASK [start nginx] **********************************************************************************************
ok: [nginx02]
ok: [nginx01]

TASK [nginx : provide web page] *********************************************************************************
ok: [nginx01]
ok: [nginx02]

PLAY RECAP ******************************************************************************************************
nginx01                    : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
nginx02                    : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 测试访问192.168.1.12和12 测试页

 10.构建PHP角色

构建php任务,这里我们只用到了 tasks,templates 和 handlers目录,tasks当中内容查看

1)配置php的tasks

main.yml 文件内容:

 

 install.yml文件内容:

 说明:

yum 模块使用了  {{ item }}变量,with_items当中是给 item变量赋的值,每个减号开头字符,都是一个实际软件名,task会在执行yum模块时,循环执行,有多少个变量item就执行几回,直到将所有with_items当中的字符全部带入到yum模块中。

只要涉及到循环执行都可以使用 with_items。

2)扩展:循环语句

循环:迭代,需要重复执行的任务,对迭代项的引用,固定变量名为 "item",使用with_items 属性给要迭代的元素。

元素:要迭代的元素可以是字符串、字典

基于字符串列表给出元素示例:

- name: install packages
 yum: name={{ item }} state=latest
 with_items:
   - httpd
   - php
   - php-mysql
   - php-mbstring
   - php-gd
基于字典列表给元素示例:item.name   "."后边的表示键

- name: create groups
 group: name={{ item }} state=present
 with_items:
   - groupx1
   - groupx2
   - groupx3
- name: create users
 user: name={{ item.name }} group={{ item.group }} state=present
 with_items:
   - {name: 'userx1', group: 'groupx1'}
   - {name: 'userx2', group: 'groupx2'}
   - {name: 'userx3', group: 'groupx3'}

 config.yml 文件内容:

2)复制相应的安装包和模板到对应的目录下

cp /etc/php.ini /ansible/lnmp/roles/php/templates/php.ini.j2
cp /etc/php-fpm.conf /ansible/lnmp/roles/php/templates/php-fpm.conf.j2
cp /etc/php-fpm.d/www.conf /ansible/lnmp/roles/php/templates/www.conf.j2

3)构建 php 的 handlers 目录下的 main.yml 文件

 

 打开php角色的注释:

 执行测试:

cd /ansible/lnmp/
ansible-playbook site.yml -t php

PLAY [prepare system] *******************************************************************************************

PLAY [install and config nginx] *********************************************************************************

PLAY [install and congig php] ***********************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [php02]
ok: [php01]

TASK [install php php-mysql php-fpm] ****************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of 
using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['php', 'php-
mysql', 'php-fpm']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can 
be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of 
using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['php', 'php-
mysql', 'php-fpm']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can 
be disabled by setting deprecation_warnings=False in ansible.cfg.

ok: [php02] => (item=[u'php', u'php-mysql', u'php-fpm'])
ok: [php01] => (item=[u'php', u'php-mysql', u'php-fpm'])

TASK [congif php] ***********************************************************************************************
ok: [php02]
ok: [php01]

TASK [config php-fpm] *******************************************************************************************
ok: [php02] => (item={u'dest': u'/etc/php-fpm.conf', u'src': u'php-fpm.conf.j2', u'mode': u'0644'})
ok: [php01] => (item={u'dest': u'/etc/php-fpm.conf', u'src': u'php-fpm.conf.j2', u'mode': u'0644'})
ok: [php02] => (item={u'dest': u'/etc/php-fpm.d/www.conf', u'src': u'www.conf.j2', u'mode': u'0644'})
ok: [php01] => (item={u'dest': u'/etc/php-fpm.d/www.conf', u'src': u'www.conf.j2', u'mode': u'0644'})

TASK [start php-fpm] ********************************************************************************************
changed: [php02]
changed: [php01]

PLAY RECAP ******************************************************************************************************
php01                      : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
php02                      : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

针对警告解决方法:

 测试访问 192.168.1.12 和 13 的测试页 index.php

 11.构建mysql角色

构建mysql任务,这里我们只用到了tasks,templates和handlers目录,tasks当中的内容查看

1)配置mysql 的 tasks

 mysql相关配置,首先打开webserver.yml下,打开mysql角色的注释

 

打开注释后,仿照nginx配置,写mysql的配置文件,具体目录结构参考nginx,不与说明,具体内容略有变化,进行详解

main.yml 文件内容: 

 

 install.yml 文件内容:

说明:

安装 MySQL-python的包,这个包是在我们即将配置mysql时使用的,因为我们在配置mysql时调用了Ansible的mysql模块,所以需要由这个包对我们的操作进行支持,没有这个包我们无法使用Ansible的mysql模块

config.yml 文件的内容:

 说明:

mysql_db模块,对数据库当中的数据库进行操作,可以帮助我们完成create,drop,show等针对数据库的操作。

mysql_user模块,针对于数据库当中用户进行操作,可以帮助我们完成,create,delete,select,grant等对于用户的操作。

在调用mysql_db和mysql_user时,统一会用到的参数login_host,login_user,login_password,login_port都是在指定登录时相关信息。

对于mysql_db模块来说,name指定是数据库名,encoding指定创建数据库或操作数据库时使用的字符集,state是指对数据库进行什么样的操作,persent相当于创建数据库,import的是导入数据库,导入数据时使用的数据库文件由target指定,那么如果你想删除一个数据库,只需要将state由persent改成asbent

在调用mysql_user模块时,我们除了使用了登录的参数外,还有几个额外的参数,name创建或者操作用户时指定用户名,password指定创建用户的密码,host指定创建的用户能在哪里登录,priv指定创建出来的用户具备什么样的权限,state当中作用就和数据库一样了,persent表示创建,absent表示删除

对mysql_user模块的priv书写方式为 “在哪个数据库上.对哪个表:有什么权限”的方式书写,举例:“*.*:all”表示,对所有数据库当中的所有表具备所有权限

2)复制相应的安装包和模板到对应目录下

cp /etc/my.cnf /ansible/lnmp/roles/mysql/templates/my.cnf.j2
vim /ansible/lnmp/roles/mysql/templates/my.cnf.j2
[mysqld]
datadir=/data/mysql/data  #修改此行,其他参数保持默认即可

 3)构建mysql的handlers目录下的main.yml 文件

 执行测试:

cd /ansible/lnmp/

[root@ansible lnmp]# ansible-playbook site.yml -t mysql

PLAY [prepare system] *******************************************************************

PLAY [install and config nginx] *********************************************************

PLAY [install and config php] ***********************************************************

PLAY [install and config mysql] *********************************************************

TASK [Gathering Facts] ******************************************************************
ok: [mysql01]

TASK [mysql : install mariadb] **********************************************************
changed: [mysql01]

TASK [mysql : create data directory] ****************************************************
changed: [mysql01]

TASK [copy mysql config] ****************************************************************
changed: [mysql01]

TASK [start mysql] **********************************************************************
changed: [mysql01]

TASK [mysql : set root password] ********************************************************
changed: [mysql01]

TASK [mysql : create a database] ********************************************************
ok: [mysql01]

TASK [mysql : create a user] ************************************************************
[WARNING]: Module did not set no_log for update_password
changed: [mysql01]

RUNNING HANDLER [restart mysql] *********************************************************
changed: [mysql01]

PLAY RECAP ******************************************************************************
mysql01                    : ok=9    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0    

到 192.168.1.12(数据库主机) 查看执行结果

mysql -uroot -p123456
MariaDB [(none)]> grant all on test.* to zhangsan@'192.168.1.12' ideitied by '123456';

 访问之前复制到远程主机的 testdb.php测试页 

 至此通过ansible playbook部署lnmp就完成了。 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹黄堡!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值