使用Ansible部署LAMP环境


使用Ansible部署LAMP环境

  •  

前言

这两天学习了Ansible, 在部署实验环境真的很好用, 今天向大家分享如何使用Ansible部署LAMP环境, 本文不对Ansible的基本使用作解释。

实验环境

今天实验环境比较简单, 所以就不画图了

 

主机IP地址功用
ansible.anyisalin.com 172.16.1.2 控制主机
web.anyisalin.com 172.16.1.3 httpd和php
data.anyisalin.com 172.16.1.4 MySQL

实验步骤

配置ssh公钥认证

ansible是agentless类的工具, 通过ssh管理远程主机, 我们需要配置基于公钥认证的ssh

[root@ansible ~]# ssh-keygen -P '' -f ~/.ssh/id_rsa -t rsa    #生成公钥
[root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.1.3
[root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.1.4

安装ansible

由于ansible的rpm包只有在epel源主提供, 但是一些所依赖组件却在官方的base2中, 所以我们使用阿里云的镜像站

[root@ansible ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@ansible ~]# yum install ansible --nogpgcheck -y &> /dev/null && echo success  #安装ansible
success

配置host iventory

将我们所要管理的主机添加到配置文件中

[root@ansible ~]# vim /etc/ansible/hosts   #按需添加以下字段到指定配置文件中
   [webservers]  #组名
   172.16.1.3  #IP

   [dataserver]
   172.16.1.4

创建YAML文件

我们通过playbook来指挥主机运行特定操作 
注意: 笔者的配置只针对笔者的环境, 如需使用请自行修改

[root@ansible ~]# vim lamp.yml #创建YAML格式的文件
- hosts: webservers
 remote_user: root
 tasks:
 - name: Install Apache Httpd
   yum: name={{ item }} state=present disable_gpg_check=yes
   with_items:
       - httpd
       - php
       - php-mysql
 - name: Install Configuration File
   template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
   notify:
   - restart httpd
 - name: Start Httpd Service
   service: enabled=true name=httpd state=started
 handlers:
 - name: restart httpd
   service: name=httpd state=restart

- hosts: dataserver
 remote_user: root
 tasks:
 - name: Install MySQL Server
   yum: name=mysql-server state=present disable_gpg_check=yes
 - name: Install Configuration File
   template: src=/etc/my.cnf dest=/etc/my.cnf
   notify:
   - restart MySQL
 - name: Start MySQL Server
   service: name=mysqld state=started
 handlers:
 - name: restart MySQL
   service: name=mysqld state=restarted

运行Ansible-Playbook并测试

Alt textblob.png

blob.png

blob.png

blob.png

blob.png

blob.png

Alt text

Alt text

Alt text

Alt text

Alt text

总结

其实还可以使用role实现, 但是我们这里不做介绍, Ansible上手真的简单, ansible-doc命令查看的帮助也浅显易懂, 写这篇博客前mysql_user模块我是不会使用的, 写到最后的时候随便试一下就成功了, 看来ansible的入门真的很容易。

作者水平很低, 如果有错误及时指出, 如果你觉得本文写的好请点一波赞~(≧▽≦)/~
作者: AnyISaIln QQ: 1449472454
感谢: MageEdu

使用 Ansible 高效交付 Docker 容器 

使用Ansible批量管理远程服务器 

Ansible安装配置与简单使用 

在 CentOS 7 中安装并使用自动化工具 Ansible 

Ansible和Docker的作用和用法 

Ansible批量搭建LAMP环境

Ansible :一个配置管理和IT自动化工具 

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址

相关内容