Ansible 批量部署多台Wordpress博客系统


一、测试环境

节点名称IP地址
node1192.168.111.10
node2192.168.111.20
node3192.168.111.20

二、搭建过程

1. 创建相关目录

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

2. ansible服务器的配置

准备相关服务的配置文件、以及wordpress源码包

默认需要的文件这些都不存在,我们在ansible服务器上搭建一遍wordpress博客系统
[root@node1 ~]# yum install -y httpd mariadb-server php php-mysql  搭建lamp环境
[root@node1 ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz  下载wordpress

拷贝对应的文件到对应的目录
[root@node1 ~]# cp /etc/httpd/conf/httpd.conf /etc/ansible/lamp/roles/httpd/files/
[root@node1 ~]# cp wordpress-4.9.4-zh_CN.tar.gz /etc/ansible/lamp/roles/httpd/files/

数据库这些需要的文件根据自行配置设置

3. 环境准备

prepare 目录为被控端环境初始化用的
[root@node1 ~]# vi /etc/ansible/lamp/roles/prepare/tasks/main.yaml 
- name: install wget 
  yum: name=wget state=present 
- name: rm -rf *.repo
  shell: rm -rf /etc/yum.repos.d/*
- name: wget 163.repo
  shell: wget -O /etc/yum.repos.d/Centos-7.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
- name: updaet yum 
  shell: yum repolist 
- name: stop firewalld 
  service: name=firewalld state=stopped enabled=no
- name: stop iptables
  shell: iptables -F

4. 编写httpd剧本

[root@node1 ~]# vi /etc/ansible/lamp/roles/httpd/tasks/main.yaml 
- name: install httpd 
  yum: name=httpd state=present
- name: copy wordpress
  copy: src=wordpress-4.9.4-zh_CN.tar.gz dest=/opt
- name: copy httpd.conf
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf 
  notify:
    - restart httpd 
- name: tar wordperss
  shell: tar -xf /opt/wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html/
- name: start httpd 
  service: name=httpd state=started enabled=yes 

编写触发条件
[root@node1 ~]# vi /etc/ansible/lamp/roles/httpd/handlers/main.yaml  
- name: restart httpd  
  service: name=httpd state=restarted

5. 编写mysql剧本

[root@node1 ~]# vi /etc/ansible/lamp/roles/mysql/tasks/main.yaml 
- name: install mysql
  yum: name=mariadb-server state=present
- name: start mariadb 
  service: name=mariadb state=started enabled=yes
- name: create database wordpress
  shell: mysql -e "create database wordpress;"     #创建数据库
- name: cretae wordpress user   #创建授权用户
  shell: mysql -e "grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by '123456';"

6. 构建PHP剧本

[root@node1 ~]# vi /etc/ansible/lamp/roles/php/tasks/main.yaml 
- name: install php
  yum: name=php state=present 
- name: install php-mysql
  yum: name=php-mysql state=present
~                                    

7. 总剧本

[root@node1 ~]# vi /etc/ansible/lamp/roles/all.yaml 
- hosts: all
  remote_user: root
  roles:
    - prepare
    - httpd
    - mysql
    - php
 
 
目录层级关系
[root@node1 roles]# tree .
.
├── httpd
│   ├── files
│   │   ├── httpd.conf
│   │   └── wordpress-4.9.4-zh_CN.tar.gz
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
├── mysql
│   ├── files
│   ├── handlers
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
├── php
│   ├── files
│   ├── handlers
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
└── prepare
    ├── files
    ├── handlers
    ├── tasks
    │   └── main.yaml
    ├── templates
    └── vars

8. 执行效果

[root@node1 ~]# ansible-playbook /etc/ansible/lamp/roles/all.yaml 

浏览器访问测试
在这里插入图片描述
连接数据库
在这里插入图片描述
最终效果图

在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ball-4444

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

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

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

打赏作者

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

抵扣说明:

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

余额充值