857432 发表于 2016-7-14 08:42:01

使用Ansible部署LAMP环境

本帖最后由 857432 于 2016-7-14 08:44 编辑

使用Ansible部署LAMP环境前言Ansible在部署实验环境真的很好用,今天向大家分享如何使用Ansible部署LAMP环境。实验环境今天实验环境比较简单, 所以就不画图了
主机IP地址功用
server1.anyisalin.com172.16.1.2控制主机
web.anyisalin.com172.16.1.3httpd和php
data.anyisalin.com172.16.1.4MySQL
实验步骤配置ssh公钥认证ansible是agentless类的工具, 通过ssh管理远程主机, 我们需要配置基于公钥认证的ssh
1
2
3
# ssh-keygen -P '' -f ~/.ssh/id_rsa -t rsa    #生成公钥
# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.1.3
# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.1.4




安装ansible由于ansible的rpm包只有在epel源主提供, 但是一些所依赖组件却在官方的base2中, 所以我们使用阿里云的镜像站
1
2
3
4
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
# yum install ansible --nogpgcheck -y &> /dev/null && echo success#安装ansible
success




配置host iventory将我们所要管理的主机添加到配置文件中
1
2
3
4
5
6
# vim /etc/ansible/hosts   #按需添加以下字段到指定配置文件中
    #组名
    172.16.1.3#IP

   
    172.16.1.4




创建YAML文件我们通过playbook来指挥主机运行特定操作
注意: 笔者的配置只针对笔者的环境, 如需使用请自行修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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并测试总结其实还可以使用role实现, 但是我们这里不做介绍, Ansible上手真的简单, ansible-doc命令查看的帮助也浅显易懂。
页: [1]
查看完整版本: 使用Ansible部署LAMP环境