Ansible自動化搭建wordpress博客

實驗環(huán)境:
客戶端:Windows
服務(wù)器:虛擬機運行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

準備:建議使用一個干凈的操作系統(tǒng),關(guān)掉selinux昙楚,清空iptables轮洋。自行搭建好yum源衰猛、安裝Ansible福稳。儒恋。
官網(wǎng)下載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黄刚、根據(jù)拓撲圖捎谨,規(guī)劃各種roles

  • keepalived
    我們要實現(xiàn)雙主雙實例模式民效,因此就設(shè)定keepalived1和keepalived2兩種角色好了
  • LB
    由于是高可用憔维,兩臺LB配置一模一樣,因此設(shè)定LB為一種角色
  • varnish
    同LB一樣畏邢,設(shè)定varinsh一種角色
  • Web服務(wù)器:
    web1搭建一個wordpress业扒,web2通過nfs共享web1的wordpress。因此劃分兩種角色
  • php
    就它一個了
  • mysql
    主從復制舒萎, mysql-master程储、mysql-slave

3、roles配置

調(diào)試了好久臂寝,直接上配置吧
有空再優(yōu)化一下配置章鲤,補充上注釋

[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、調(diào)用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

安裝好是這樣的:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末败徊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子掏缎,更是在濱河造成了極大的恐慌皱蹦,老刑警劉巖煤杀,帶你破解...
    沈念sama閱讀 207,113評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異沪哺,居然都是意外死亡沈自,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評論 2 381
  • 文/潘曉璐 我一進店門辜妓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枯途,“玉大人,你說我怎么就攤上這事嫌拣∪嵩” “怎么了?”我有些...
    開封第一講書人閱讀 153,340評論 0 344
  • 文/不壞的土叔 我叫張陵异逐,是天一觀的道長捶索。 經(jīng)常有香客問我,道長灰瞻,這世上最難降的妖魔是什么腥例? 我笑而不...
    開封第一講書人閱讀 55,449評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮酝润,結(jié)果婚禮上燎竖,老公的妹妹穿的比我還像新娘。我一直安慰自己要销,他們只是感情好构回,可當我...
    茶點故事閱讀 64,445評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著疏咐,像睡著了一般纤掸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浑塞,一...
    開封第一講書人閱讀 49,166評論 1 284
  • 那天借跪,我揣著相機與錄音,去河邊找鬼酌壕。 笑死掏愁,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的卵牍。 我是一名探鬼主播果港,決...
    沈念sama閱讀 38,442評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼糊昙!你這毒婦竟也來了辛掠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,105評論 0 261
  • 序言:老撾萬榮一對情侶失蹤溅蛉,失蹤者是張志新(化名)和其女友劉穎公浪,沒想到半個月后他宛,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,601評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡欠气,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,066評論 2 325
  • 正文 我和宋清朗相戀三年厅各,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片预柒。...
    茶點故事閱讀 38,161評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡队塘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宜鸯,到底是詐尸還是另有隱情憔古,我是刑警寧澤,帶...
    沈念sama閱讀 33,792評論 4 323
  • 正文 年R本政府宣布淋袖,位于F島的核電站鸿市,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏即碗。R本人自食惡果不足惜焰情,卻給世界環(huán)境...
    茶點故事閱讀 39,351評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望剥懒。 院中可真熱鬧内舟,春花似錦、人聲如沸初橘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽保檐。三九已至耕蝉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間展东,已是汗流浹背赔硫。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評論 1 261
  • 我被黑心中介騙來泰國打工炒俱, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留盐肃,地道東北人。 一個月前我還...
    沈念sama閱讀 45,618評論 2 355
  • 正文 我出身青樓权悟,卻偏偏與公主長得像砸王,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子峦阁,可洞房花燭夜當晚...
    茶點故事閱讀 42,916評論 2 344

推薦閱讀更多精彩內(nèi)容

  • 基本信息:系統(tǒng)平臺:VMware WorkStation系統(tǒng)版本: CentOS Linux release 7....
    lijincheng閱讀 1,077評論 0 1
  • 緩存的基礎(chǔ)知識 1谦铃、程序本身具有局部性 時間局部性過去訪問到的數(shù)據(jù),也有可能被兩次訪問 空間局部性一個數(shù)據(jù)被訪問到...
    魏鎮(zhèn)坪閱讀 2,005評論 1 3
  • Varnish是一款高性能的開源HTTP加速器,可以實現(xiàn)緩存和代理功能榔昔。varnish的每個版本的功能功能有很大的...
    fengkaipeng閱讀 529評論 0 1
  • 實驗目的: 兩個lamp部署wordpress驹闰,用Nginx反代瘪菌,壓測以下。然后nginx之后部署varnish緩...
    geekdeedy閱讀 343評論 0 0
  • 一嘹朗、ansible基礎(chǔ) 1师妙、介紹 ansible是一個自動化運維工具,基于Python開發(fā)屹培,集合了眾多運維工具(p...
    姜餅人_9e7b閱讀 4,553評論 0 1