通过 roles 实现 lamp
需定制三个⻆⾊ : httpd,mysql,php
第 1 步 : 创建 roles ⽬录及⽂件 , 并确认⽬录结构
master# cd /etc/ansible/roles/
master# mkdir -p
{httpd,mysql,php}/{files,tasks,handlers,templates,v
ars,meta}
master# touch
{httpd,mysql,php}/{tasks,handlers,vars,meta}/main.y
ml
master# yum install tree -y
master# tree /etc/ansible/roles/
/etc/ansible/roles/
"## httpd
$ "## files
$ "## handlers
$ $ %## main.yml
$ "## meta
$ $ %## main.yml
$ "## tasks
$ $ %## main.yml
$ "## templates
$ %## vars
$ %## main.yml
"## mysql
$ "## files
$ "## handlers
$ $ %## main.yml
$ "## meta
$ $ %## main.yml
$ "## tasks
$ $ %## main.yml
$ "## templates
$ %## vars
$ %## main.yml
%## php
"## files
"## handlers
$ %## main.yml
"## meta
$ %## main.yml
"## tasks
$ %## main.yml
"## templates
%## vars
%## main.yml
第2步: 准备httpd服务器的主⻚⽂件,php测试⻚和配置⽂件等
master# echo "test main page" >
/etc/ansible/roles/httpd/files/index.html
master# echo -e "<?php\n\tphpinfo();\n?>" >
/etc/ansible/roles/httpd/files/test.php
master# yum install httpd -y
按需求修改配置⽂件后,拷⻉到httpd⻆⾊⽬录⾥的files⼦⽬录
master# vim /etc/httpd/conf/httpd.conf
master# cp /etc/httpd/conf/httpd.conf
/etc/ansible/roles/httpd/files/
第3步: 编写httpd⻆⾊的main.yml⽂件
---
- name: 安装httpd
yum: name=httpd,httpd-devel state=present
- name: 同步httpd配置⽂件
copy:
src=/etc/ansible/roles/httpd/files/httpd.conf
dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: 同步主⻚⽂件
copy:
src=/etc/ansible/roles/httpd/files/index.html
dest=/var/www/html/index.html
- name: 同步php测试⻚
copy:
src=/etc/ansible/roles/httpd/files/test.php
dest=/var/www/html/test.php
- name: 启动httpd并开机⾃启动
service: name=httpd state=started enabled=yes
第 4 步 : 编写 httpd ⻆⾊⾥的 handler
master# vim
/etc/ansible/roles/httpd/handlers/main.yml
---
- name: restart httpd
service: name=httpd state=restarted
第 5 步 : 编写 mysql ⻆⾊的 main.yml ⽂件
---
- name: 安装mysql
yum: name=mariadb,mariadb-server,mariadb-devel
state=present
- name: 启动mysql并开机⾃启动
service: name=mariadb state=started enabled=yes
第 6 步 : 编写 php ⻆⾊的 main.yml ⽂件
master# vim /etc/ansible/roles/php/tasks/main.yml
---
- name: 安装php及依赖包
yum: name=php,php-gd,php-ldap,php-odbc,phppear,php-xml,php-xmlrpc,php-mbstring,php-snmp,phpsoap,curl,curl-devel,php-bcmath,php-mysql
state=present
notify: restart httpd
第 7 步 : 编写 lamp 的 playbook ⽂件调⽤前⾯定义好的三个⻆⾊
master# vim /etc/ansible/playbook/lamp.yaml
---
- hosts: group1
remote_user: root
roles:
- httpd
- mysql
- php
第 8 步 : 执⾏ lamp 的 playbook ⽂件
master# ansible-playbook
/etc/ansible/playbook/lamp.yaml
拓展案例 : 通过 roles 实现 lamp 并安装 discuz
第 1 步 : 创建 roles ⽬录及⽂件 , 并确认⽬录结构
master# cd /etc/ansible/roles/
master# mkdir -p
{httpd,mysql,php}/{files,tasks,handlers,templates,v
ars,meta}
master# touch
{httpd,mysql,php}/{tasks,handlers,vars,meta}/main.y
ml
第 2 步 : 准备 httpd 相关⽂件
master# ls /etc/ansible/roles/httpd/files/
Discuz_X3.2_SC_UTF8.zip Discuz相关软件包
httpd.conf 配置好的httpd.conf配置⽂
件
第 3 步 : 编写 httpd ⻆⾊的 main.yml ⽂件
master# vim /etc/ansible/roles/httpd/tasks/main.yml
- name: 安装httpd相关软件包
yum: name=httpd,httpd-devel state=latest
- name: 同步配置⽂件
copy:
src=/etc/ansible/roles/httpd/files/httpd.conf
dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: 拷⻉discuz压缩包
copy:
src=/etc/ansible/roles/httpd/files/Discuz_X3.2_SC_U
TF8.zip dest=/tmp/
- name: 解压并mv⽹站⽂件到httpd家⽬录
shell: rm -rf /var/www/html/* && rm -rf /test/
&& mkdir -p /test/ && unzip
/tmp/Discuz_X3.2_SC_UTF8.zip -d /test/ &> /dev/null
&& mv /test/upload/* /var/www/html/ && chown -R
apache.apache /var/www/html/
# 上⾯的命令有点多,可以写成脚本,然后使⽤script模块来调⽤执
⾏
- name: 启动httpd并开机⾃启动
service: name=httpd state=started enabled=on
第 4 步 : 编写httpd ⻆⾊⾥的 handler
master# vim
/etc/ansible/roles/httpd/handlers/main.yml
---
- name: restart httpd
service: name=httpd state=restarted
第 5 步 : 编写 mysql ⻆⾊的 main.yml ⽂件
master# vim /etc/ansible/roles/mysql/tasks/main.yml
---
- name: 安装mariadb相关软件包
yum: name=mariadb-server,mariadb-devel
state=latest
- name: 启动mariadb服务并设置开机⾃启动
service: name=mariadb state=started enabled=on
- name: 执⾏建库脚本
script: /etc/ansible/roles/mysql/files/create.sh
第 6 步 : 编写 mysql 的建库脚本
master# vim
/etc/ansible/roles/mysql/files/create.sh
#!/bin/bash
mysql << EOF
create database if not exists discuz default
charset=utf8;
grant all on discuz.* to 'discuz'@'localhost'
identified by '123';
flush privileges;
EOF
第 7 步 : 编写 php ⻆⾊的 main.yml ⽂件
master# vim /etc/ansible/roles/php/tasks/main.yml
---
- name: 安装php相关软件包
yum: name=php,php-gd,php-ldap,php-odbc,phppear,php-xml,php-xmlrpc,php-mbstring,php-snmp,phpsoap,curl,curl-devel,php-bcmath,php-mysql
state=present
notify: restart httpd
第 8 步 : 编写 lamp 的 playbook ⽂件调⽤前⾯定义好的三个⻆⾊
master# vim /etc/ansible/playbook/lamp.yaml
---
- hosts: group1
remote_user: root
roles:
- httpd
- mysql
- php
第 9 步 : 执⾏ lamp 的 playbook ⽂件
master# ansible-playbook
/etc/ansible/playbook/lamp.yaml