ansible-playbook批量搭建LAMP环境

先在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 查看是否出现测试页

创建对应的文件

将搭建成功的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 查看是否有测试页

欢迎加入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值