续我的上篇博文:CSDN。即自动化部署zabbix已经配置好(但是需要在此基础上再添加一个主机server3)
一、实验环境(rhel7.3版本)
1.selinux和firewalld状态为disabled
2.各主机信息如下:
主机 | ip |
---|---|
server1(ansible的服务端) | 172.25.83.1 |
server2(ansible的客户端,Ansible nginx,Ansible Keepalived,Ansible httpd) | 172.25.83.2 |
server3(ansible的客户端,Ansible nginx,Ansible Keepalived,Ansible httpd) | 172.25.83.3 |
虚拟IP | 172.25.83.100 |
3.使虚拟机能够上网:
[root@foundation83 ~]# iptables -t nat -I POSTROUTING -s 172.25.83.0/24 -j MASQUERADE
4.在server2和server3端搭建好yum源
[root@server2 ~]# cd /etc/yum.repos.d/
[root@server2 yum.repos.d]# vim nginx.repo
[centos7]
name=centeros7 base
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck=0
[epel]
name=epel base
baseurl=http://mirrors.aliyun.com/epel/7/x86_64
gpgcheck=0
#server3端的yum源的配置同上
扫描二维码关注公众号,回复:
13367878 查看本文章
5.编辑host
[root@server1 ansible]# vim /etc/ansible/hosts
44 [web]
45 server1
46
47 [db]
48 server2
49 server3
二、keepalived+lvs+nginx+httpd的自动化部署
1、nginx目录
[root@server1 ~]# cd /etc/ansible/roles/
[root@server1 roles]# ls
apache mariadb php zabbix-server
[root@server1 roles]# mkdir nginx/{defaults,files,handlers,meta,tasks,templates,vars} -p
[root@server1 roles]# cd nginx/files/
[root@server1 files]# vim index.html
hello world
[root@server1 files]# cd ../tasks/
[root@server1 tasks]# vim install_nginx.yml
- name: install nginx
yum: name=nginx state=present
- name: install nginx index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
notify: restart nginx
tags: modify nginx config copy
- name: install config
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
tags: modify nginx config
- name: start nginx
service: name=nginx state=started enabled=true
[root@server1 tasks]# vim remove_nginx.yml
- name: remove nginx
yum: name=nginx state=absent
[root@server1 tasks]# vim main.yml
- include: tasks/install_nginx.yml
tags: install
when: ansible_eth0.ipv4.address == '172.25.83.2' or ansible_eth0.ipv4.address == '172.25.83.3'
- include: tasks/remove_nginx.yml
tags: remove
when: ansible_eth0.ipv4.address == '172.25.83.2' or ansible_eth0.ipv4.address == '172.25.83.3'
[root@server1 tasks]# cd ../templates/
[root@server1 templates]# vim nginx.conf.j2
user {
{ runuser }};
worker_processes {
{ ansible_processor_vcpus-1 }};
listen {
{ nginx_prot }};
[root@server1 templates]# cd ../vars/
[root@server1 vars]# vim main.yml
runuser: daemon
nginx_prot: 80
[root@server1 templates]# cd ../handlers/
[root@server1 handlers]# vim main.yml
- name: restart nginx
service: name=nginx state=restarted
2、keepalived目录
[root@server1 ~]# cd /etc/ansible/roles/
[root@server1 roles]# ls
apache mariadb nginx php zabbix-server
[root@server1 roles]# mkdir keepalived/{defaults,files,handlers,meta,tasks,templates,vars} -p
[root@server1 roles]# cd keepalived/tasks/
[root@server1 tasks]# vim install_keepalived.yml
- name: install keepalived
yum: name=keepalived state=present
- name: install keepalived config
template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
notify: restart keepalived
tags: modify keepalived config
- name: start keepalived
service: name=keepalived state=started enabled=true
[root@server1 tasks]# vim remove_keepalived.yml
- name: remove keepalived
yum: name=keepalived state=absent
[root@server1 tasks]# vim main.yml
- include: tasks/install_keepalived.yml
tags: install
when: ansible_eth0.ipv4.address == '172.25.83.2' or ansible_eth0.ipv4.address == '172.25.83.3'
- include: tasks/remove_keepalived.yml
tags: remove
when: ansible_eth0.ipv4.address == '172.25.83.2' or ansible_eth0.ipv4.address == '172.25.83.3'
[root@server1 tasks]# cd ../templates/
[root@server1 templates]# vim keepalived.conf.j2
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_mcast_group4 224.0.100.18
}
vrrp_instance VI_1 {
state {
{ mb }}
interface eth0
virtual_router_id 51
priority {
{ prioroty }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { 172.25.83.100/24
}
}
[root@server1 templates]# cd ../handlers/
[root@server1 handlers]# vim main.yml
- name: restart keepalived
service: name=keepalived state=restarted
3、httpd目录
[root@server1 ~]# cd /etc/ansible/roles/
[root@server1 roles]# ls
apache keepalived mariadb nginx php zabbix-server
[root@server1 roles]# mkdir httpd/{defaults,files,handlers,meta,tasks,templates,vars} -p
[root@server1 roles]# cd httpd/files/
[root@server1 files]# vim index.html
<h1>Test file.</h1>
[root@server1 files]# vim index.php
<?php
phpinfo();
?>
[root@server1 files]# cd ../tasks/
[root@server1 tasks]# vim install_httpd.yml
- name: install httpd
yum: name=httpd state=present
- name: install php
yum: name=php state=present
- name: install httpd index.html
copy: src=index.html dest=/var/www/html/index.html
notify: restart httpd
tags: modify httpd config copy
- name: install httpd index.php
copy: src=index.php dest=/var/www/html/index.php
notify: restart httpd
tags: modify httpd config copy
- name: install config
template: src=httpd.conf.j2 dest=/etc/nginx/httpd.conf
notify: restart httpd
tags: modify httpd config
- name: start httpd
service: name=httpd state=started enabled=true
[root@server1 tasks]# vim remove_httpd.yml
- name: remove httpd
yum: name=httpd state=absent
- name: remove php
yum: name=php state=absent
[root@server1 tasks]# vim main.yml
- include: tasks/install_httpd.yml
when: ansible_eth0.ipv4.address == '172.25.83.2' or ansible_eth0.ipv4.address == '172.25.83.3'
tags: install
- include: tasks/remove_httpd.yml
tags: remove
when: ansible_eth0.ipv4.address == '172.25.83.2' or ansible_eth0.ipv4.address == '172.25.83.3'
[root@server1 tasks]# cd ../templates
[root@server1 tepmlates]# cp /etc/ansible/roles/apache/templates/httpd.conf.j2 . #将之前搭建lamp架构用到的httpd服务的模板拷贝过来
[root@server1 templates]# vim httpd.conf.j2 #定义变量
42 Listen {
{ http_port }}
[root@server1 templates]# cd ../vars/
[root@server1 vars]# vim main.yml
index:
- index.php
- index.html
http_port: 8080
[root@server1 tasks]# cd ../handlers/
[root@server1 handlers]# vim main.yml
- name: restart httpd
service: name=httpd state=restarted
4、.yml文件的编写
[root@server1 ~]# cd /etc/ansible/
[root@server1 ansible]# vim service.yml
- hosts: db
remote_user: root
roles:
- nginx
- httpd
- keepalived
5、测试
[root@server1 ansible]# ansible-playbook service.yml --syntax-check
[root@server1 ansible]# ansible-playbook service.yml -C
[root@server1 ansible]# ansible-playbook -t "install" service.yml