一、ansible
1.簡介
ansible是新出現的自動化運維工具,基于Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實作了批量系統配置、批量程式部署、批量運作指令等功能。
ansible是基于子產品工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運作的子產品,ansible隻是提供一種架構。主要包括:
(1)、連接配接插件connection plugins:負責和被監控端實作通信;
(2)、host inventory:指定操作的主機,是一個配置檔案裡面定義監控的主機;
(3)、各種子產品核心子產品、command子產品、自定義子產品;
(4)、借助于插件完成記錄日志郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運作多個任務。
2.特點
- 沒有用戶端,輕量級
- 通過ssh連接配接,無需agent
- 可實作大規模部署,效率高
二、ansible部署
環境:
ansible1:rhel7,hosts解析要做好,控制節點
ansible2:rhel7,hosts解析要做好,被控制節點
軟體:ansible ==> 點選下載下傳 提取碼: kuhr
1.安裝ansible
ansible1
[[email protected] ansible]# ls
ansible-2.7.8-1.el7.noarch.rpm
ansible-tower-setup-bundle-3.4.2-1.el7.tar.gz
libtomcrypt-1.17-25.el7.x86_64.rpm
libtommath-0.42.0-5.el7.x86_64.rpm
python2-crypto-2.6.1-13.el7.x86_64.rpm
python2-jmespath-0.9.0-1.el7.noarch.rpm
python-httplib2-0.9.2-0.1.el7.noarch.rpm
python-keyczar-0.71c-2.el7.noarch.rpm
python-paramiko-2.1.1-0.9.el7.noarch.rpm
sshpass-1.06-1.el7.x86_64.rpm
[[email protected] ansible]# yum install * -y
2.配置ansible
[[email protected] ansible]# cd /etc/ansible/
[[email protected] ansible]# ls
ansible.cfg hosts roles ##host是節點主機資訊,cfg主配置檔案,roles:放角色
[[email protected] ansible]# vim hosts ##最後添加
[web]
ansible1
[db]
ansible2
3.設定ansible1免密登陸
[[email protected] .ssh]# ssh-keygen
[[email protected] .ssh]# ssh-copy-id -i id_rsa.pub [email protected]
[[email protected] .ssh]# ssh-copy-id -i id_rsa.pub [email protected]
3.測試
[[email protected] ~]# ansible all -m ping
ansible2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ok,可以通過ansible對主機進行操作
三、模拟生産環境
1.檢視ansible的文檔
[[email protected] ~]# ansible-doc -l
[[email protected] ~]# ansible-doc -l | wc -l
2080 ##一共有2080個也是很可怕了
2.準備工作
在開始之前,我們需要在ansible1和ansible2上建立使用者,因為大多數伺服器不允許遠端連接配接使用root使用者
[[email protected] ~]# ansible all -m user -a "name=orange password=westos"
但是還是需要在ansible1和ansible2上更改orange使用者的密碼才可以,因為這樣建立的使用者密碼是明文儲存的,不可以ansible方式登陸,然後像之前一樣,做一個免密登陸,然後修改/etc/sudoers檔案,以ansible1為例,ansible2一樣的操作
[[email protected] ~]# passwd orange
[[email protected] ~]# ssh-copy-id [email protected] ##對ansible2也需要免密登陸
[roo[email protected] ~]# vim /etc/sudoers ##添加如下圖内容
3.測試
[[email protected] ~]# ansible all -m ping -u orange -b
ansible2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
成功
四、子產品應用
1.擷取hostname主機名
[[email protected] ~]# ansible all -u orange -b -a "hostname"
ansible1 | CHANGED | rc=0 >>
ansible1
ansible2 | CHANGED | rc=0 >>
ansible2
2.yum子產品
安裝httpd
[[email protected] ~]# ansible ansible2 -u orange -b -m yum -a "name=httpd state=present"
然後到ansible2上進行檢視
[[email protected] ~]# ansible ansible2 -u orange -b -m service -a "name=httpd state=started"
然後到ansible2上進行檢視
安裝啟動mairadb
[[email protected] ~]# ansible ansible2 -u orange -b -m yum -a "name=mariadb-server state=present"
[[email protected] ~]# ansible ansible2 -u orange -b -m service -a "name=mariadb state=started"
3.file子產品
建立軟連接配接
[[email protected] ~]# ansible ansible2 -u orange -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"
然後到ansible2上進行檢視
删除檔案
[[email protected] ~]# ansible ansible2 -u orange -b -m file -a "dest=/tmp/fstab state=absent"
檢視
建立檔案
[[email protected] ~]# ansible ansible2 -u orange -b -m file -a "dest=/tmp/dir1/dir2 state=directory mode=755"
檢視
4.mysql子產品
嘗試建立資料庫使用者
[[email protected] ~]# ansible ansible2 -m mysql_user -a "name=orange password=orange priv=test.*:ALL state=present"
錯誤,需要先安裝軟體才可以
[[email protected] ~]# ansible ansible2 -m yum -a "name=MySQL-python state=present"
檢視
五、playbook的使用
playbook我個人的了解就是将上面那些指令使用yml文法寫成了類似于腳本的方式,進行了一個自動化部署,可以更友善快捷的使用
例1-安裝配置httpd
[[email protected] ~]# cd /etc/ansible/
[[email protected] ansible]# ls
ansible.cfg hosts roles
[[email protected] ansible]# mkdir playbooks
[[email protected] ansible]# cd playbooks/
[[email protected] playbooks]# mkdir httpd
[[email protected] playbooks]# cd httpd/
[[email protected] httpd]# vim httpd.yml
---
# http部署
- hosts: ansible2 ##主機
remote_user: root ##使用者
tasks: ##任務
- name: install httpd
yum: name=httpd state=present
- name: config httpd
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restart
開始執行之前,我們檢視以下是否有文法錯誤和一些資訊
[[email protected] httpd]# ansible-playbook httpd.yml --syntax-check ##檢視是否有文法錯誤
playbook: httpd.yml
[[email protected] httpd]# ansible-playbook httpd.yml --list-host ##列出主機
playbook: httpd.yml
play #1 (ansible2): ansible2 TAGS: []
pattern: [u'ansible2']
hosts (1):
ansible2
[[email protected] httpd]# ansible-playbook httpd.yml --list-task ##列出任務
playbook: httpd.yml
play #1 (ansible2): ansible2 TAGS: []
tasks:
install httpd TAGS: []
config httpd TAGS: []
start httpd TAGS: []
ok了,我們試着執行一下
[[email protected] httpd]# ansible-playbook httpd.yml
我們也可以選擇隻部署其中一個服務
[[email protected] httpd]# ansible-playbook httpd.yml --start-at-task="start httpd"
例2-部署zabbix監控
1.首先建立檔案夾
[[email protected] ~]# cd /etc/ansible/roles
[[email protected] roles]# mkdir mariadb/files tasks handlers vars templates meta defaults -p
[[email protected] roles]# mkdir zabbix-server/{files,tasks,templates,vars} -p
2.然後編輯主yml配置檔案
[[email protected] roles]# vim zabbix-server.yml
---
#zabbix-server部署
- hosts: ansible2
roles:
- mariadb
- zabbix-server
3.編輯zabbix的配置檔案
[[email protected] roles]# cd /etc/ansible/roles/zabbix-server/files ##鏡像資訊
[[email protected] files]# vim zabbix.repo ##需要在真機提前配置好倉庫
[zabbix]
name=zabbix4.0
baseurl=ftp://172.25.1.254/pub/zabbix/4.0
gpgcheck=0
[[email protected] files]# vim zabbix_server.conf ##zabbix的配置檔案
zabbix的配置檔案 ==> 點選下載下傳 提取碼: fh64 ,因為實在太長,故貼出下載下傳連結
編輯zabbix觸發器
[email protected] zabbix-server]# cd handlers/
[[email protected] handlers]# vim main.yml
[[email protected] handlers]# cat main.yml
- name: create datbase
mysql_db: name=zabbix state=present
listen: "init zabbix db"
- name: create zabbix user
mysql_user: name=zabbix password=zabbix priv=zabbix.*:ALL state=present
listen: "init zabbix db"
- name: import create.sql.gz
mysql_db: name=zabbix state=import target=/usr/share/doc/zabbix-server-mysql-4.0.5/create.sql.gz
listen: "init zabbix db"
- name: restart zabbix server
service: name=zabbix-server state=restarted
編輯zabbix的tasks
[[email protected] handlers]# cd ../tasks/
[[email protected] tasks]# vim main.yml
[[email protected] tasks]# cat main.yml
- name: copy zabbix.repo
copy: src=zabbix.repo dest=/etc/yum.repos.d/zabbix.repo
- name: install zabbix-server
yum: name=zabbix-server,zabbix-agent state=present
notify: "init zabbix db"
- name: config zabbix server
copy: src=zabbix_server.conf dest=/etc/zabbix/zabbix_server.conf
notify: restart zabbix server
- name: start zabbix server
service: name={{ item }} state=started
with_items:
- zabbix-server
- zabbix-agent
4.編輯mariadb配置檔案
編輯mariadb的files
[[email protected] mariadb]# cd files/
[[email protected] files]# vim my.cnf
[[email protected] files]# cat my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#
#
character-set-server=utf8
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
編輯mariadb的觸發器
[[email protected] files]# cd ../handlers/
[[email protected] handlers]# vim main.yml
[[email protected] handlers]# cat main.yml
- name: restart mariadb
service: name=mariadb state=restarted
編輯mariadb的tasks
[[email protected] handlers]# cd ../tasks/
[[email protected] tasks]# vim main.yml
[[email protected] tasks]# cat main.yml
- name: install mariadb server
yum: name=mariadb-server,MySQL-python state=present
- name: config mariadb
copy: src=my.cnf dest=/etc/my.cnf
notify: restart mariadb
- name: start mariadb server
service: name=mariadb state=started
5.測試
[[email protected] roles]# ansible-playbook zabbix-server.yml
其他更多功能和用法請檢視官方網站~