码迷,mamicode.com
首页 > 其他好文 > 详细

ansible-playbook批量搭建LAMP

时间:2019-11-27 10:45:24      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:ali   web   插件   director   防火墙   出现   sdn   ext   mairadb   

先在ansible服务器安装LAMP环境,然后再将配置文件通过ansible拷贝到远程主机

1.安装httpd软件

yum -y install httpd
2.安装mysql
yum -y install mariadb-server mysql
systemctl start mairadb

3.安装php 和php-mysql模块

yum -y install php php-mysql

4.提供php测试页

vim /var/www/html/index.php
<?php
        phpinfo();
?>
systemctl restart httpd

访问ip:80 查看是否出现测试页

技术图片

创建对应的文件

mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}

将搭建成功的LAMP环境的httpd和MySQL的配置文件拷贝到对应目录下

cd /etc/ansible/
cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
cp /etc/my.cnf lamp/roles/mysql/files/

写prepare角色的playbooks #前期准备

vim lamp/roles/prepare/tasks/main.yml
- name: provide yumrepo file
  shell: rm -rf /etc/yum.repos.d/*.repo    #删除原有yum配置文件
  shell: wget - o /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-repo    #下载新的yum配置文件
- name: clean the yum repo
  shell: yum clean all
- name: clean the iptables
  shell: systemctl stop firewalld    #关闭防火墙

构建httpd任务

cp /var/www/html/index.php lamp/roles/httpd/files/
vim lamp/roles/httpd/tasks/main.yml
- name: web server install
  yum: name=httpd state=present    #安装httpd服务
- name: provide test page
  copy: src=index.php dest=/var/www/html    #提供测试页
  notify: restart httpd    #当前面的copy执行成功后,通过notify通知名字为restart httpd的handlers运行
- name: restart httpd
  service: name=httpd enabled=yes state=restarted    #重启httpd服务

部署mariadb数据库

创建MySQL服务任务,需要安装mysql服务,改变属主属性,启动mysql

vim lamp/roles/mysql/tasks/main.yml
- name: install the mysql
  yum: name=mariadb-server state=present    #安装mysql服务
- name: mkdir date directory
  shell: mkdir -p /mydata/data    #创建挂载点目录
- name: provide configration file
  copy: src=my.cnf dest=/etc/my.cnf    #拷贝mysql的配置文件
- name: chage the owner
  shell: chown -R mysql:mysql /mydata/*    #更改属主和属主
- name: start mariadb
  service: name=mariadb enabled=yes state=started    #启动mysql服务

构建php任务

vim lamp/roles/php/tasks/main.yml
- name: install php
  yum: name=php state=present    #安装php
- name: install php-mysql
  yum: name=php-mysql state=present    #安装php与mysql交互插件

定义整个任务

 vim lamp/roles/site.yml
- name: LAMP build
  remote_user: root
  hosts: all
  roles:
    - prepare
    - mysql
    - php
    - httpd

执行playbook

ansible-playbook -i /etc/ansible/hosts ./site.yml

技术图片

访问192.168.1.129 查看是否有测试页

技术图片

欢迎加入

技术图片

ansible-playbook批量搭建LAMP

标签:ali   web   插件   director   防火墙   出现   sdn   ext   mairadb   

原文地址:https://blog.51cto.com/14486455/2453755

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!