Linux&&ansible基礎學習
瞭解
優點
Ansible在Github的項目地址:https://github.com/ansible/ansible
工作機制
Ansible在管理節點將Ansible模塊通過SSH協議推送到管理端執行,執行完之後自動刪除,可以使用SVN等,來管理自定義模塊及編排。
如上圖所示,Ansible由5個部分組成
- Ansible : ansible核心
- Inventory : 定義Ansible管理主機的清單
- Playbooks : 劇本,定義Ansible多任務配置文件,由Ansible自動執行
- Modules : 包括Ansible自帶的核心模塊及自定義模塊
- Plugins : 完成模塊功能的補充,包括連接插件、郵件插件等
使用
配置環境
yum安裝ansible
#避免”Too many open file”的錯誤提示
sudo launchctl limit maxfiles 1024 2048
設置EPEL倉庫,安裝EPEL源
yum install epel-release-y
Ansible倉庫默認不在yum倉庫中,因此我們需要使用以上命令啓動epel倉庫
使用yum安裝Ansible:
yum install ansible-y
#檢查ansible版本
ansible --version
通過遠程方式在託管節點上安裝 Python 2.X 和 simplejson 模塊
ansible myhost --sudo -m raw -a "yum install -y python2 python-simplejson"
在託管節點上也需要安裝 Python 2.4 或以上的版本.如果版本低於 Python 2.5 ,還需要額外安裝一個模塊python-simplejson
如果託管節點上開啓了SElinux,你需要安裝libselinux-python,這樣纔可使用Ansible中與copy/file/template相關的函數.你可以通過Ansible的yum模塊在需要的託管節點上安裝libselinux-python.
源碼安裝ansible
#下載ansible源碼壓縮包
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
#安裝pip
sudo easy_install pip
#安裝必要python模塊
sudo pip install paramiko PyYAML Jinja2 httplib2 six
#更新git中指向Ansible自身模塊的“submodules”
git pull --rebase
git submodule update --init --recursive
當更新ansible版本時,不只要更新git的源碼樹,也要更新git中指向Ansible自身模塊的 “submodules” (不是同一種模塊)
#配置ansible
echo "127.0.0.1" > ~/ansible_hosts
export ANSIBLE_HOSTS=~/ansible_hosts
#測試ansible
ansible all -m ping --ask-pass
命令參數
命令語法:
ansible [-i 主機文件] [-f 批次] [組名] [-m 模塊名稱] [-a 模塊參數]
詳細參數:
-i PATH , -inventory=PATH #指定host文件的路徑,默認是在/etc/ansible/hosts
-m NAME , -module-name=NAME #指定使用的module名稱,默認使用command模塊
-a , MODULE_ARGS #指定module模塊的參數
基於ssh密鑰來訪問定義主機清單
實現無密鑰登錄
#服務器端生成密鑰並複製公鑰到節點
ssh-keygen
#使用ssh-copy-id命令來複制Ansible公鑰到節點
ssh-copy-id [email protected]
ssh-copy-id [email protected]
#測試是否實現免交互登錄。
ssh [email protected]
定義主機清單
vim /etc/ansible/hosts
[web-servers]
192.168.1.63
192.168.1.64
#針對整個主機清單內的主機
ansible -i /etc/ansible/hosts
#針對主機清單內的web-server組的全部主機,指定模塊ping,
ansible -i /etc/ansible/hosts web-server -m ping
-i 指定host文件的路徑,默認是/etc/ansible/hosts
-m 指定使用的ping模塊
ansible常見模塊高級使用方法
command模塊,遠程命令模塊
是ansible的默認模塊,當不指定-m參數時,使用的就是command模塊
示例:檢查Ansible節點的運行時間(uptime)
ansible -i /etc/ansible/hosts webserver -m command -a "uptime"
yum模塊,Linux平臺軟件包管理模塊
yum模塊可以提供的status狀態有:latest,present,installed和removed,absent
示例:安裝httpd軟件
#查詢httpd的最新版本
ansible -i /etc/ansible/hosts web-server -m yum -a "name=httpd state=latest"
service模塊,遠程主機系統服務管理模塊
啓動,關閉,重啓系統服務。
- name : 此參數用於指定需要操作的服務名稱,如nginx,httpd
- state : 此參數用於指定服務的狀態,如,想要啓動遠程主機的httpd服務,則將state的值設置爲started;如果想要停止遠程主機中的服務,則將state的值設置爲stopped狀態。
示例:啓動遠程主機的apache服務
ansible -i /etc/ansible/hosts web-server -m service -a "name=httpd state=restarted"
實戰-使用playbook批量管理數百臺web服務器
playbook是一個不同於使用ansible命令執行方式的模式,功能更加強大且靈活。
playbook使用步驟
參數:
- name : task description #name : 任務描述信息
- module_name : module_args #聲明模塊 : 定義ansible模塊參數
執行命令:
ansible-playbook site.yml
playbook常用文件夾作用
- files : 存放需要同步到異地服務器的源碼文件及配置文件
- handlers : 當資源發生變化時需要進行的操作,若沒有此目錄可以不建或爲空
- meta : 角色定義,可留空
- tasks : lamp安裝過程需要進行的執行任務
- templates : 用於執行lamp安裝的模板文件,一般爲腳本
- vars : 本次安裝定義的變量
使用playbook創建一個lamp構建的任務
#創建相關文件
mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}
#配置yum源
vim /etc/ansible/lamp/roles/prepare/tasks/main.yml
- name: delete yum config
shell: rm -rf /etc/yum.repos.d/* #刪除原有的yum配置文件
- name: provide yumrepo file
shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #下載新的yum配置文件
- name: clean the yum repo
shell: yum clean all #清除原有的yum緩存信息
- name: clean the iptables
shell: iptables -F #清除原有防火牆規則
構建httpd的任務:
cd /etc/ansible/lamp/roles
vim httpd/files/index.php
<?php
phpinfo();
?>
vim httpd/tasks/main.yml
- name: web server install
yum: name=httpd state=present #安裝httpd服務
- name: provide test page
copy: src=index.php dest=/var/www/html #提供測試頁
notify: restart httpd #當前面的copy複製成功後,通過notify通知名字爲restart httpd的handlers運行
#構建httpd的handlers
vim httpd/handlers/main.yml
- name: restart httpd
service: name=httpd enabled=yes state=restarted
部署mariadb數據庫
#創建mysql服務的任務,安裝mysql服務,改變屬主信息,啓動mysql服務
cd /etc/ansible/lamp/roles
vim mysql/tasks/main.yml
- name: install the mysql
yum: name=mariadb-server state=present #安裝mysql服務
- name: start mariadb
service: name=mariadb enabled=yes state=started #啓動mysql服務
部署PHP任務:
vim php/tasks/main.yml
- name: install php
yum: name=php state=present #安裝PHP服務
- name: install php-mysql
yum: name=php-mysql state=present #安裝PHP與mysql交互的插件
定義整體任務順序:
cd /etc/ansible/lamp/roles
vim site.yml
- name: LAMP build
remote_user: root
hosts: web-server
roles:
- prepare
- mysql
- php
- httpd
#開始部署
ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml
檢測lamp環境是否部署成功。