Ansible自动化搭建wordpress博客

实验环境:
客户端:Windows
服务器:虚拟机运行CentOS7

LB Nginx1:172.16.80.100
LB Nginx2:172.16.80.101
Web Nginx1:172.16.80.102
Web Nginx2:172.16.80.103
Keepalived双实例双主模式,两个vip分别为172.16.80.200和172.16.80.201

准备:建议使用一个干净的操作系统,关掉selinux,清空iptables。自行搭建好yum源、安装Ansible。。
官网下载wordpress-4.8.1-zh_CN.tar包


1、配置无密钥登录

[root@lb-nginx1 ~]#ssh-keygen -t rsa -P ''
#生成rsa密钥
[root@lb-nginx1 ~]ssh-copy-id -i  ~/.ssh/id_rsa root@172.16.80.100
[root@lb-nginx1 ~]ssh-copy-id -i  ~/.ssh/id_rsa root@172.16.80.101
[root@lb-nginx1 ~]ssh-copy-id -i  ~/.ssh/id_rsa root@172.16.80.102
[root@lb-nginx1 ~]ssh-copy-id -i  ~/.ssh/id_rsa root@172.16.80.103
#把密钥拷贝到各主机
[root@lb-nginx1 ~]ssh 172.16.80.100 'ifconfig';ssh 172.16.80.101 'ifconfig';ssh 172.16.80.102 'ifconfig';ssh 172.16.80.103 'ifconfig'
#验证是否能正常访问各主机

2、根据拓扑图,规划各种roles

  • keepalived
    我们要实现双主双实例模式,因此就设定keepalived1和keepalived2两种角色好了
  • LB
    由于是高可用,两台LB配置一模一样,因此设定LB为一种角色
  • varnish
    同LB一样,设定varinsh一种角色
  • Web服务器:
    web1搭建一个wordpress,web2通过nfs共享web1的wordpress。因此划分两种角色
  • php
    就它一个了
  • mysql
    主从复制, mysql-master、mysql-slave

3、roles配置

调试了好久,直接上配置吧
有空再优化一下配置,补充上注释

[root@centos7a ~]mkdir -pv /etc/ansible/roles/{keepalived1,keepalived2,lb,mysql-master,mysql-slave,nfs,php,varnish,web1,web2}/{files,templates,tasks,handlers,vars,meta,default}
[root@centos7a ~]#cd /etc/ansible/roles/
[root@centos7a roles]#ls
keepalived1  keepalived2  lb  mysql-master  mysql-slave  nfs  php  varnish  web1  web2
[root@centos7c roles]#tree
.
├── keepalived1
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── keepalived2
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── lb
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── mysql-master
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── mysql-slave
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── nfs
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── php
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── varnish
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── web1
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
└── web2
    ├── default
    ├── files
    ├── handlers
    ├── meta
    ├── tasks
    ├── templates
    └── vars

LB配置:

[root@lb-nginx1 roles]#tree lb/
lb/
├── default
├── files
│   └── lb.conf
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars
####################################################################

[root@lb-nginx1 roles]#cat lb/tasks/main.yml 
- name: install nginx
  yum: name=nginx state=present
- name: install conf
  copy: src=lb.conf dest=/etc/nginx/nginx.conf
  tags: conf
  notify: restart nginx
- name: start nginx
  service: name=nginx state=started enabled=yes

####################################################################

[root@lb-nginx1 roles]#cat lb/handlers/main.yml 
- name: restart nginx
  service: name=nginx state=restarted

####################################################################

[root@lb-nginx1 roles]#cat lb/files/lb.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream www.server.pools{
    server 172.16.80.200:6081;
    server 172.16.80.201:6081;
    }
    server {
        listen       80;
        server_name  www.nginx.com;
        location / {
        proxy_pass http://www.server.pools;
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

keepalived1配置

[root@lb-nginx1 roles]#tree keepalived1/
keepalived1/
├── default
├── files
│   └── keepalived1.conf
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

####################################################################

[root@lb-nginx1 roles]#cat keepalived1/tasks/main.yml 
- name: install keepalived
  yum: name=keepalived state=present
- name: install conf
  copy: src=keepalived1.conf dest=/etc/keepalived/keepalived.conf
  tags: conf
  notify: restart keepalived
- name: start keepalived
  service: name=keepalived state=started enabled=yes

####################################################################

[root@lb-nginx1 roles]#cat keepalived1/files/keepalived1.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost
}
   notification_email_from keadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id CentOS7B.luo.com
}


vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 15
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass hahahaha
    }
    virtual_ipaddress {
    172.16.80.200
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 22
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass hehehehe
    }
    virtual_ipaddress {
    172.16.80.201
    }
}

####################################################################


[root@lb-nginx1 roles]#cat keepalived1/handlers/main.yml 
- name: restart keepalived
  service: name=keepalived state=restarted


varnish配置

[root@lb-nginx1 roles]#tree varnish/
varnish/
├── default
├── files
│   └── varnish.vcl
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars


####################################################################

[root@lb-nginx1 roles]#cat varnish/tasks/main.yml 
- name: install varnish
  yum: name=varnish state=present
- name: install conf
  copy: src=varnish.vcl dest=/etc/varnish/default.vcl
  tags: conf
  notify: restart varnish
- name: start varnish
  service: name=varnish state=started enabled=yes

####################################################################

[root@lb-nginx1 roles]#cat varnish/files/varnish.vcl 
vcl 4.0;
import directors;
backend web1 {
    .host = "172.16.80.102";
    .port = "80";
}
backend web2 {
    .host = "172.16.80.103";
    .port = "80";
}
sub vcl_init {                  
    new WEB = directors.round_robin();
    WEB.add_backend(web1);
    WEB.add_backend(web2);
}
sub vcl_recv {
    set req.backend_hint = WEB.backend();        
}
sub vcl_backend_response {
}
sub vcl_deliver {
}

####################################################################

[root@lb-nginx1 roles]#cat varnish/handlers/main.yml 
- name: restart varnish
  service: name=varnish state=restarted

web1配置

[root@lb-nginx1 roles]#tree web1/
web1/
├── default
├── files
│   ├── wordpress-4.8.1-zh_CN.tar.gz
│   └── web.conf
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

[root@lb-nginx1 roles]#cat web1/tasks/main.yml 
- name: add user nginx
  user: name=nginx group=nginx uid=666
- name: install nginx
  yum: name=nginx state=present
- name: install conf
  copy: src=web.conf dest=/etc/nginx/nginx.conf
  tags: conf
  notify: restart nginx
- name: copy blog
  unarchive: src=blog.tar.gz dest=/usr/share/nginx/html/
- name: set mode
  file: name=/usr/share/nginx/html/wordpress recurse=yes  owner=nginx group=nginx
- name: start nginx
  service: name=nginx state=started enabled=yes

[root@lb-nginx1 roles]#cat web1/handlers/main.yml 
- name: restart nginx
  service: name=nginx state=restarted

[root@lb-nginx1 roles]#cat web1/files/web.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.static.com;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
       }
    }
}

Web2配置

[root@lb-nginx1 roles]#tree web2
web2
├── default
├── files
│   └── web.conf
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars


[root@lb-nginx1 roles]#cat web2/tasks/main.yml 
- name: add user nginx
  user: name=nginx group=nginx uid=666
- name: install nginx
  yum: name=nginx state=present
- name: install conf
  copy: src=web.conf dest=/etc/nginx/nginx.conf
  tags: conf
  notify: restart nginx
- name: mkdir
  file: name=/usr/share/nginx/html/wordpress state=directory
- name: mount
  mount: src=172.16.80.102:/usr/share/nginx/html/wordpress/ name=/usr/share/nginx/html/wordpress fstype=nfs state=mounted
- name: start nginx
  service: name=nginx state=started enabled=yes


[root@lb-nginx1 roles]#cat web2/handlers/main.yml 
- name: restart nginx
  service: name=nginx state=restarted

[root@lb-nginx1 roles]#cat web2/files/web.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.static.com;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
       }
    }
}

[root@lb-nginx1 roles]#tree nfs/
nfs/
├── default
├── files
│   └── exports
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

[root@lb-nginx1 roles]#cat nfs/tasks/main.yml 
- name: install nfs
  yum: name=nfs-utils state=present
- name: install conf
  copy: src=exports dest=/etc/exports
- name: start nfs
  service: name=nfs state=started enabled=yes

[root@lb-nginx1 roles]#cat nfs/files/exports 
/usr/share/nginx/html/wordpress 172.16.80.103(rw,all_squash,anonuid=666)

php配置:

[root@lb-nginx1 roles]#tree php/
php/
├── default
├── files
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

[root@lb-nginx1 roles]#cat php/tasks/main.yml 
- name: install php
  yum: name={{ item }} state=present
  with_items:
  - php-mysql
  - php-fpm
- name: start php-fpm
  service: name=php-fpm state=started enabled=yes

mysql-master

[root@lb-nginx1 roles]#tree mysql-master/
mysql-master/
├── default
├── files
│   └── my.cnf
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars

[root@lb-nginx1 roles]#cat mysql-master/tasks/main.yml 
- name: install mariadb
  yum: name={{ item }} state=present
  with_items:
  - mariadb
  - mariadb-server
- name: install conf
  copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
  service: name=mariadb state=started enabled=yes
- name: mysql
  command: mysql -e "create database wordpress;grant all on wordpress.* to wordpress@'172.16.80.%' identified by '123456';"
- name: command
  command: mysql -e "grant replication slave,replication client on *.* to 'backuper'@'172.16.%.%' identified by 'backuper';"

[root@lb-nginx1 roles]#cat mysql-master/files/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master_bin
server_id=1
innodb_file_per_table=ON
skip_name-resolve=ON

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid



mysql-slave

[root@lb-nginx1 roles]#tree mysql-slave/
mysql-slave/
├── default
├── files
│   └── my.cnf
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars


[root@lb-nginx1 roles]#cat mysql-slave/tasks/main.yml 
- name: install mariadb
  yum: name={{ item }} state=present
  with_items:
  - mariadb
  - mariadb-server
- name: install conf
  copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
  service: name=mariadb state=started enabled=yes
- name: command
  command: mysql -e "change master to master_host='172.16.80.102',master_user='backuper',master_password='backuper',master_log_file='master_bin.000001',master_log_pos=30364;"
- name: command
  command: mysql -e "start slave;"

[root@lb-nginx1 roles]#cat mysql-slave/files/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

relay-log=relay-log
server-id=22
innodb_file_per_table=ON
skip_name_resolve=ON

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d

4、调用role

[root@lb-nginx1 ~]#vim /etc/ansible/hosts
[proxy]
172.16.80.100
172.16.80.101
[web]
172.16.80.102
172.16.80.103
#在/etc/ansible/hosts加上上面几行

[root@centos7a ~]#cat role.yml 
- hosts: proxy
  remote_user: root
  roles:
  - lb
  - varnish

- hosts: 172.16.80.100
  remote_user: root
  roles:
  - keepalived1

- hosts: 172.16.80.101
  remote_user: root
  roles:
  - keepalived2

- hosts: 172.16.80.102
  remote_user: root
  roles:
  - php
  - web1
  - nfs

- hosts: 172.16.80.103
  remote_user: root
  roles:
  - php
  - nfs
  - web2

- hosts: 172.16.80.102
  remote_user: root
  roles:
  - mysql-master


- hosts: 172.16.80.103
  remote_user: root
  roles: 
  - mysql-slave


测试

[root@lb-nginx1 ~]#ansible-playbook -C role.yml

如果没有问题,部署

[root@lb-nginx1 ~]#ansible-playbookrole.yml

安装好是这样的:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容

  • 基本信息:系统平台:VMware WorkStation系统版本: CentOS Linux release 7....
    lijincheng阅读 1,079评论 0 1
  • 缓存的基础知识 1、程序本身具有局部性 时间局部性过去访问到的数据,也有可能被两次访问 空间局部性一个数据被访问到...
    魏镇坪阅读 2,010评论 1 3
  • Varnish是一款高性能的开源HTTP加速器,可以实现缓存和代理功能。varnish的每个版本的功能功能有很大的...
    fengkaipeng阅读 534评论 0 1
  • 实验目的: 两个lamp部署wordpress,用Nginx反代,压测以下。然后nginx之后部署varnish缓...
    geekdeedy阅读 343评论 0 0
  • 一、ansible基础 1、介绍 ansible是一个自动化运维工具,基于Python开发,集合了众多运维工具(p...
    姜饼人_9e7b阅读 4,554评论 0 1