1. 实验环境:
1.1 准备虚拟机3台:
Ansible:192.168.153.183
lamp: 192.168.153.184
mysql: 192.168.153.185
1.2 实验逻辑图如图:
2. Ansible服务器配置
2.1 添加主机清单并测试主机的连通性
添加主机清单
[root@localhost ~]# vim /etc/ansible/hosts
[webservers]
192.168.153.184
[dbservers]
192.168.153.185
测试主机连通性
[root@localhost ~]# ansible webservers -m ping
192.168.153.184 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ~]# ansible dbservers -m ping
192.168.153.185 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ~]#
2.2 安装Apache,获取Apache配置文件模板
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mv /etc/httpd/conf/httpd.conf lamp/
修改部分配置文件如下
[root@localhost ~]# cd lamp/
[root@localhost lamp]# vim httpd.conf
163 <IfModule dir_module>
164 DirectoryIndex index.php
165 </IfModule>
2.3 上传电商包
[root@localhost lamp]# ls
httpd.conf tinyshopV2.5_data.zip
[root@localhost lamp]#
2.4 编写playbook文件
---
- hosts: dbservers
vars:
db_user: tom
db_passwd: 123
db_name: tinyshop
tasks:
- name: "安装mariadb,mariadb-server"
yum: name=mariadb,mariadb-server state=latest
- name: "启动mariadb服务"
service: name=mariadb state=started
- name: "关闭firewalld服务"
service: name=firewalld state=stopped
- name: "创建电商数据库"
shell: mysql -e "create database {{ db_name }} charset utf8;" || /bin/true
- name: "给电商平台授权"
shell: mysql -e "grant all on tinyshop.* to '{{ db_user }}'@'%' identified by '{{ db_passwd }}';"
- hosts: webservers
tasks:
- name: "安装httpd,php,php-gd,php-mysql,unzip"
yum: name=httpd,php,php-gd,php-mysql,unzip state=latest
- name: "启动Apache服务"
service: name=httpd state=started
- name: "关闭firewalld服务"
service: name=firewalld state=stopped
- name: "推送配置文件"
template: src=/root/lamp/httpd.conf dest=/etc/httpd/conf/
notify:
- restart httpd
tags:
cfg_httpd
- name: "推送电商包"
unarchive: src=/root/lamp/tinyshopV2.5_data.zip dest=/var/www/html/
- name: "给网站根目录授权"
shell: chmod -R 777 /var/www/html/
- name: "关闭selinux"
shell: setenforce 0
handlers:
- name: "restart httpd"
service: name=httpd state=restarted
2.5 检查并playbook文件
检查
[root@localhost lamp]# ansible-playbook --syntax-check lamp.yml
playbook: lamp.yml
[root@localhost lamp]#
执行
[root@localhost lamp]# ansible-playbook lamp.yml
PLAY [dbservers] ******************************************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.153.185]
TASK [安装mariadb,mariadb-server] ***************************************************************************************************************************************
ok: [192.168.153.185]
TASK [启动mariadb服务] ****************************************************************************************************************************************************
ok: [192.168.153.185]
TASK [关闭firewalld服务] **************************************************************************************************************************************************
ok: [192.168.153.185]
TASK [创建电商数据库] ********************************************************************************************************************************************************
changed: [192.168.153.185]
TASK [给电商平台授权] ********************************************************************************************************************************************************
changed: [192.168.153.185]
PLAY [webservers] *****************************************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.153.184]
TASK [安装httpd,php,php-gd,php-mysql,unzip] *****************************************************************************************************************************
ok: [192.168.153.184]
TASK [启动Apache服务] *****************************************************************************************************************************************************
ok: [192.168.153.184]
TASK [关闭firewalld服务] **************************************************************************************************************************************************
ok: [192.168.153.184]
TASK [推送配置文件] *********************************************************************************************************************************************************
ok: [192.168.153.184]
TASK [推送电商包] **********************************************************************************************************************************************************
changed: [192.168.153.184]
TASK [给网站根目录授权] *******************************************************************************************************************************************************
[WARNING]: Consider using the file module with mode rather than running 'chmod'. If you need to use command because file is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.153.184]
TASK [关闭selinux] ******************************************************************************************************************************************************
changed: [192.168.153.184]
PLAY RECAP ************************************************************************************************************************************************************
192.168.153.184 : ok=8 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.153.185 : ok=6 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost lamp]#
2.6 浏览器输入lamp服务器的地址测试
访问成功
登陆成功