通过Playbook部署LAMP(5)
title: 通過Playbook部署LAMP(5)
date: 2018-12-03 13:24:07
tags:
- Ansible
categories: Ansible
copyright: true
---
Ansible是新出現的自動化運維工具,基于Python開發,集合了眾多運維工具 (puppet、cfengine、chef、func、fabric) 的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能,Ansible是基于模塊工作的,本身沒有批量部署的能力,真正具有批量部署的是Ansible所運行的模塊,Ansible只是提供一種框架.
LAMP 架構也是我們每個運維人員熟知的一種網站架構,它是 Linux+Apachc+MariaDB 以及(PHP,Perl,Python)語言的首字母簡稱,這種網站架構很容易實現跨主機的橫向與縱向擴展,可快速組建一個龐大的 Wcb 集群系統,本章我們通過 Ansible 自動化部署 LAMP 架構,這個架構也是我們日常運維過程中經常遇到的一個架構.
Ansible的PlayBook文件格式為YAML語言,所以希望你在編寫PlayBook前對YAML語法有一定的了解,否則在運行PlayBook的時候經常碰到語法錯誤提示,這里我們通過介紹批量部署LAMP為例,介紹一下LAMP.yml這個PlayBook的具體應用寫法,如果你對YAML語言沒有了解的話,請自行去百度學習.
創建準備環境
首先,我們有兩臺虛擬機192.168.10.20 and 192.168.10.30 這兩臺虛擬機,下面我們將寫一個劇本實現批量部署LAMP環境,在這之前我們需要先創建SSH密鑰對并分發到每一臺的主機上去.
[root@localhost ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:xZxM9bunwBsS03gGT5HGT4LvOnJHdr5Bwl/Iit7qQN8 root@localhost.localdomain The keys randomart image is: +---[RSA 2048]----+ | .+o. | | =..=o. | | Bo.+. | | . B...o | | S +.B = .| | . . O+=.o | | . ++Eo+ .| | .o+o.+.+ | | +++o o. | +----[SHA256]-----+[root@localhost ~]# ssh-copy-id root@192.168.10.20 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host 192.168.10.20 (192.168.10.20) can t be established. ECDSA key fingerprint is SHA256:2kWFaV72YVvAl2EU2Zop4uAjP3Gy2jW92d0Va/HrSMM. ECDSA key fingerprint is MD5:fc:6c:91:b0:02:e6:7e:98:52:af:0d:b3:47:d4:69:ef. Are you sure you want to continue connecting (yes/no)? yes root@192.168.10.20 s password: [root@localhost ~]# ssh-copy-id root@192.168.10.30 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host 192.168.10.30 (192.168.10.30) cant be established. ECDSA key fingerprint is SHA256:2kWFaV72YVvAl2EU2Zop4uAjP3Gy2jW92d0Va/HrSMM. ECDSA key fingerprint is MD5:fc:6c:91:b0:02:e6:7e:98:52:af:0d:b3:47:d4:69:ef. Are you sure you want to continue connecting (yes/no)? yes root@192.168.10.30's password:其次創建一個目錄用于存放劇本中需要用到的數據文件等,這里我們只創建兩個index文件,用于后期的測試,如果你有一些配置文件需要拷貝,此時應該提前準備好.
[root@localhost ~]# mkdir playbook[root@localhost ~]# ls -lh total 0 drwxr-xr-x. 2 root root 6 Dec 3 10:44 playbook[root@localhost ~]# cd playbook/[root@localhost playbook]# ls -lh total 8.0K drwxr-xr-x. 2 root root 6 Dec 3 10:46 apache drwxr-xr-x. 2 root root 6 Dec 3 10:46 mariadb drwxr-xr-x. 2 root root 6 Dec 3 10:46 php -rw-r--r--. 1 root root 30 Dec 3 10:45 index.html -rw-r--r--. 1 root root 29 Dec 3 10:46 index.php[root@localhost playbook]# cat index.html hello lysharkwww.mkdirs.com[root@localhost playbook]# cat index.php <?phpphpinfo(); ?>接著創建一個用戶主機列表,這里我們就在當前目錄下創建一個Hosts文件即可,如果有很多太主機可以使用簡寫.
[root@localhost playbook]# vim hosts [root@localhost playbook]# cat hosts [lamp] 192.168.10.20 192.168.10.30#[test] #此處注釋,只做說明,定義從20-100網段的主機 #192.168.10.2[0:100]編寫Apache安裝過程
這里由于我們是第一次編寫劇本,所有我們應該先創建一個文件,編寫一個main.yml劇本,我們來寫一個安裝apache軟件的劇本,先來看一下這個PlayBook的部分代碼:
---- hosts: lamptasks:- name: Yum install httpdyum: name={{item}} state=installedwith_items:- apr- apr-util- httpd- httpd-devel- name: copy index.htmltemplate: src=./index.html dest=/var/www/html/index.html owner=root group=root mode=0755- name: copy index.phptemplate: src=./index.php dest=/var/www/html/index.php owner=root group=root mode=0755notify: #上一個命令執行成功,才會執行notify- start httpdhandlers:- name: start httpdservice: name=httpd state=restarted第一項:hosts指定哪些主機執行操作,此處我們將主機列表規劃成了lamp組,也就是說LAMP組成員都會執行.
第二項:tasks是應用yum模塊來安裝apache服務程序包,name是說明信息,說明這個模塊的功能.
第四項:with_items是一個迭代器,用來批量安裝以下列出的包文件,此處就是apache的相關文件.
第五項:template是一個遠程復制模塊,目的是將當前目錄下的index配置文件復制到遠程主機上去.
第六項:notify發送消息的作用,這里目的是發送給名稱是start httpd的handlers讓其執行操作.
這里有個注意的地方就是關于上方寫了兩個Copy才完成了復制,其實我們可以把它們放入一個迭代器里,代碼如下:
- name: copy index.html and index.phpcopy: src={{item.src}} dest={{item.dest}} owner=root group=root mode=644with_items:- {src: ./index.html,dest:/var/www/html/index.html}- {src: ./index.php,dest:/var/www/html/index.php}接著寫完了這些配置以后,我們運行下面的幾條命令,檢查一下上面的文件是否有語法錯誤,和檢查主機列表是否生效了.
[root@localhost playbook]# ansible-playbook -i hosts main.yml --syntax-checkplaybook: main.yml[root@localhost playbook]# ansible-playbook -i hosts main.yml --list-taskplaybook: main.ymlplay #1 (lamp): lamp TAGS: []tasks:yum install httpd TAGS: []copy index.html TAGS: []copy index.php TAGS: [][root@localhost playbook]# ansible-playbook -i hosts main.yml --list-hostsplaybook: main.ymlplay #1 (lamp): lamp TAGS: []pattern: [u'lamp']hosts (2):192.168.10.20192.168.10.30編寫MariaDB安裝過程
接下來我們,繼續編輯main.yml劇本,寫一個安裝mariadb數據庫的劇本,由于無需規范化,所以我們就把他們寫在一個劇本里就可以了,先來看一下這個PlayBook的部分代碼:
- hosts: lamptasks:- name: install mariadbyum: name={{item}} state=installedwith_items:- mariadb- mariadb-servernotify:- start mariadb- name: set mysql passwordshell: mysql -e "set password for root@localhost=password('123123');"handlers:- name: start mariadbservice: name=mariadb state=restarted上圖的例子,我們在安裝Mariadb數據庫時,可以使用shell模塊直接賦值初始密碼,也可以使用下面聲明變量并調用mysql_user系統模塊完成數據庫密碼的設置,需要注意的是,如果使用系統模塊的話,被控主機必須安裝MySQL-python包.
- hosts: lampvars:- username: root #這里聲明兩個變量- password: 123123tasks:- name: install mariadbyum: name={{item}} state=installedwith_items:- mariadb- mariadb-server- MySQL-python #如果要使用MySQL函數,這里需要安裝這個包notify:- start mariadb # - name: set mysql password # shell: mysql -e "set password=password('123123');"- name: set mysql password #這里使用兩個變量來賦值mysql_user: name={{username}} password={{password}} priv=*.*:ALL host='localhost' state=presenthandlers:- name: start mariadbservice: name=mariadb state=restarted寫完了這些配置以后,我們運行下面的幾條命令,檢查一下上面的文件是否有語法錯誤,和檢查主機列表是否生效了.
[root@localhost playbook]# ansible-playbook -i hosts main.yml --syntax-checkplaybook: main.yml[root@localhost playbook]# ansible-playbook -i hosts main.yml --list-taskplaybook: main.ymlplay #1 (lamp): lamp TAGS: []tasks:yum install httpd TAGS: []copy index.html TAGS: []copy index.php TAGS: [][root@localhost playbook]# ansible-playbook -i hosts main.yml --list-hostsplaybook: main.ymlplay #1 (lamp): lamp TAGS: []pattern: [u'lamp']hosts (2):192.168.10.20192.168.10.30編寫PHP環境安裝過程
最后編輯main.yml劇本,來寫一個安裝PHP的劇本,先來看一下這個PlayBook的部分代碼:
- hosts: lamptasks:- name: install PHPyum: name={{item}} state=installedwith_items:- php- php-mysqlnotify:- start apachehandlers:- name: start apacheservice: name=apache state=restarted寫完了這些配置以后,我們運行下面的幾條命令,檢查一下上面的文件是否有語法錯誤,和檢查主機列表是否生效了.
[root@localhost playbook]# ansible-playbook -i hosts main.yml --syntax-checkplaybook: main.yml[root@localhost playbook]# ansible-playbook -i hosts main.yml --list-taskplaybook: main.ymlplay #1 (lamp): lamp TAGS: []tasks:yum install httpd TAGS: []copy index.html TAGS: []copy index.php TAGS: [][root@localhost playbook]# ansible-playbook -i hosts main.yml --list-hostsplaybook: main.ymlplay #1 (lamp): lamp TAGS: []pattern: [u'lamp']hosts (2):192.168.10.20192.168.10.30將劇本合并起來并執行
將劇本串聯起來,然后我們在最后再次添加以下內容,目的是關閉防火墻,關閉SELinux,重啟http服務.
- hosts: lamptasks:- name: check iptablesshell: iptables -F- name: check selinuxshell: setenforce 0- name: restart httpdshell: systemctl restart httpd最后我們得到了,整個LAMP的劇本安裝過程,完整代碼如下所示:
[root@localhost playbook]# cat main.yml ---- hosts: lamptasks:- name: yum install httpdyum: name={{item}} state=installedwith_items:- apr- apr-util- httpd- httpd-devel- name: copy index.htmltemplate: src=./index.html dest=/var/www/html/index.html owner=root group=root mode=0755- name: copy index.phptemplate: src=./index.php dest=/var/www/html/index.php owner=root group=root mode=0755notify:- Start httpdhandlers:- name: Start httpdservice: name=httpd state=restarted #-------------------------------------------------------------------- hosts: lamptasks:- name: install mariadbyum: name={{item}} state=installedwith_items:- mariadb- mariadb-servernotify:- start mariadb- name: set mysql passwordshell: mysql -e "set password for root@localhost=password('123123');"handlers:- name: start mariadbservice: name=mariadb state=restarted #-------------------------------------------------------------------- hosts: lamptasks:- name: install PHPyum: name={{item}} state=installedwith_items:- php- php-mysql # notify: # - start apache# handlers: # - name: start apache # service: name=apache state=restarted #-------------------------------------------------------------------- hosts: lamptasks:- name: check iptablesshell: iptables -F- name: check selinuxshell: setenforce 0- name: restart httpdshell: systemctl restart httpd接著我們執行檢測程序,檢查整體是否有語法錯誤.
[root@localhost playbook]# ansible-playbook -i hosts main.yml --syntax-checkplaybook: main.yml[root@localhost playbook]# ansible-playbook -i hosts main.yml --list-taskplaybook: main.ymlplay #1 (lamp): lamp TAGS: []tasks:yum install httpd TAGS: []copy index.html TAGS: []copy index.php TAGS: [][root@localhost playbook]# ansible-playbook -i hosts main.yml --list-hostsplaybook: main.ymlplay #1 (lamp): lamp TAGS: []pattern: [u'lamp']hosts (2):192.168.10.20192.168.10.30執行劇本: 確認過以后,直接使用下面的命令一鍵部署,我們寫好的PlayBook劇本,此時我們等它一會.
[root@localhost playbook]# ansible-playbook -i hosts main.ymlPLAY [lamp] ******************************************************************************TASK [Gathering Facts] ******************************************************************* ok: [192.168.10.30] ok: [192.168.10.20] ....省略.... PLAY RECAP ******************************************************************************* 192.168.10.20 : ok=5 changed=4 unreachable=0 failed=0 192.168.10.30 : ok=5 changed=4 unreachable=0 failed=0最后說明,本小結內容通過一個簡單案例介紹如何利用 Ansiblc 部署 LAMP 架構,這是 Ansible 在構建集群甚至跨機器部署上面的人門案例,通過本章案例可以清晰地了解到如何用 Ansible 在配置部署過程中實現一個業務邏輯架構,這也是我們在實際工作作中經常遇到的,隨著公司業務的擴張,會有很多需要維護和部署的集群架構,而這些繁復的下作對于 Ansible 來說易如反掌.
轉載于:https://www.cnblogs.com/LyShark/p/10886478.html
總結
以上是生活随笔為你收集整理的通过Playbook部署LAMP(5)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#解析JSON字符串总结
- 下一篇: 网络篇:朋友面试之TCP/IP,回去等通