目錄php
1.手動運維時代
html
2.自動化運維時代
python
3.自動化運維工具給運維帶來的好處
mysql
1.什麼是Ansibleios
Ansible是一個自動化統一配置管理工具nginx
2.同類型軟件對比web
1.puppet 學習難,安裝ruby環境難,沒有遠程執行功能
2.ansible 輕量級,大規模環境下只經過ssh會很慢,串行的
3.saltstack 通常選擇salt會使用C/S
結構的模式,salt-master
和salt-minion
,並行的,大規模批量操做的狀況下,會比Ansible速度快一些,底層使用的是zero-MQ
消協隊列sql
Ansible使用的是python2shell
saltstack即有python2也有python3apache
Ansible的功能及優勢
1.遠程執行
批量執行遠程命令,能夠對多臺主機進行遠程操做
2.配置管理
批量配置軟件服務,能夠進行自動化方式配置,服務的統一配置管理,和啓停
3.事件驅動
經過Ansible的模塊,對服務進行不一樣的事件驅動
好比:
1)修改配置後重啓
2)只修改配置文件,不重啓
3)修改配置文件後,從新加載
4)遠程啓停服務管理
4.管理公有云
經過API接口的方式管理公有云,不過這方面作的不如saltstack
.
saltstack自己能夠經過saltcloud管理各大雲廠商的雲平臺。
5.二次開發
由於語法是Python,因此便於運維進行二次開發。
6.任務編排
能夠經過playbook的方式來統一管理服務,而且可使用一條命令,實現一套架構的部署
7.跨平臺,跨系統
幾乎不受到平臺和系統的限制,好比安裝apache
和啓動服務
在Ubuntu上安裝apache服務名字叫apache2
在CentOS上安裝apache服務名字叫httpd
在CentOS6上啓動服務器使用命令:/etc/init.d/nginx start
在CentOS7上啓動服務器使用命令:systemctl start nginx
一、鏈接插件connectior plugins用於鏈接主機 用來鏈接被管理端
二、核心模塊 core modules 鏈接主機實現操做, 它依賴於具體的模塊來作具體的事情
三、自定義模塊 custom modules,根據本身的需求編寫具體的模塊
四、插件 plugins,完成模塊功能的補充
五、劇本 playbooks,ansible的配置文件,將多個任務定義在劇本中,由ansible自動執行
六、主機清單 inventor,定義ansible須要操做主機的範圍最重要的一點是 ansible是模塊化的 它全部的操做都依賴於模塊
1.Ansible讀取playbook劇本,劇本中會記錄對哪些主機執行哪些任務。
2.首先Ansible經過主機清單找到要執行的主機,而後調用具體的模塊。
3.其次Ansible會經過鏈接插件鏈接對應的主機並推送對應的任務列表。
4.最後被管理的主機會將Ansible發送過來的任務解析爲本地Shell命令執行。
安裝Ansible
1.環境準備
主機名 | wanIP | lanIP | 角色 |
---|---|---|---|
m01 | 10.0.0.61 | 172.16.1.61 | Ansible控制端 |
web01 | 10.0.0.7 | 172.16.1.7 | Ansible被控端 |
web02 | 10.0.0.8 | 172.16.1.8 | Ansible被控端 |
2.安裝ansible
[root@m01 ~]# yum install -y ansible
3.查看ansible模塊及版本
[root@m01 ~]# ansible --version ansible 2.8.4 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
4.ansible參數
# ansible <host-pattern> [options] --version #ansible版本信息 -v #顯示詳細信息 -i #主機清單文件路徑,默認是在/etc/ansible/hosts -m #使用的模塊名稱,默認使用command模塊 -a #使用的模塊參數,模塊的具體動做 -k #提示輸入ssh密碼,而不使用基於ssh的密鑰認證 -C #模擬執行測試,但不會真的執行 -T #執行命令的超時
5.ansible配置文件讀取順序
[root@m01 ~]# vim /etc/ansible/ansible.cfg # nearly all parameters can be overridden in ansible-playbook # or with command line flags. ansible will read ANSIBLE_CONFIG, # ansible.cfg in the current working directory, .ansible.cfg in # the home directory or /etc/ansible/ansible.cfg, whichever it # finds first 一、$ANSIBLE_CONFIG 二、./ansible.cfg 三、~/.ansible.cfg 四、/etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts #主機列表配置文件 #library = /usr/share/my_modules/ #庫文件存放目錄 #remote_tmp = ~/.ansible/tmp #臨時py文件存放在遠程主機目錄 #local_tmp = ~/.ansible/tmp #本機的臨時執行目錄 #forks = 5 #默認併發數 #sudo_user = root #默認sudo用戶 #ask_sudo_pass = True #每次執行是否詢問sudo的ssh密碼 #ask_pass = True #每次執行是否詢問ssh密碼 #remote_port = 22 #遠程主機端口 host_key_checking = False #跳過檢查主機指紋 log_path = /var/log/ansible.log #ansible日誌 #普通用戶提權操做 [privilege_escalation] #become=True #become_method=sudo #become_user=root #become_ask_pass=False
/etc/ansible/hosts
是ansible 默認主機資產清單文件,用於定義被管理機的認證信息,例如ssh登陸用戶名、密碼、以及key相關信息。Inventory文件中填寫須要被管理的主機與主機組信息。還能夠自定義Inventory 主機清單的位置,使用-i 指定文件位置便可。
場景一:密碼方式鏈接
[root@m01 ~]# cat /etc/ansible/hosts #方式1、IP+端口+用戶+密碼 [webs] 10.0.0.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' 10.0.0.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1' #方式2、主機名+密碼 [webs] web0[1:2] ansible_ssh_pass='123456' #方式3、主機+密碼 [webs] web0[1:2] [webs:vars] ansible_ssh_pass='123456' 注意:方式二和方式三,都須要作hosts解析
場景二:密鑰方式鏈接,須要先建立公鑰和私鑰,並下發公鑰至被控端
#建立密鑰對 [root@m01 ~]# ssh-keygen #推送公鑰 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8 #方式一:主機+端口+密鑰 [web_group] 172.16.1.7 172.16.1.8 #方式二:別名+主機+端口+密鑰 [webs] web01 ansible_ssh_host=172.16.1.7 web02 ansible_ssh_host=172.16.1.8
場景三:主機組定義方式
[root@m01 ~]# vim hosts [web_group] web01 ansible_ssh_host=172.16.1.7 web02 ansible_ssh_host=172.16.1.8 [db_group] db01 ansible_ssh_host=172.16.1.51 lb01 ansible_ssh_host=172.16.1.5 [db_group:vars] ansible_ssh_pass='1' [nfs_group] nfs ansible_ssh_host=172.16.1.31 #定義多組,多組彙總整合 [nfs_server:children] web_group nfs_group [lnmp:children] web_group db_group #查看全部主機數量 [root@m01 ~]# ansible 'all' --list-host hosts (5): nfs web01 web02 db01 lb01 [root@m01 ~]# ansible 'web_group' --list-host hosts (2): web01 web02 [root@m01 ~]# ansible 'db_group' --list-host hosts (2): db01 lb01 [root@m01 ~]# ansible 'lnmp' --list-host hosts (4): web01 web02 db01 lb01
1.什麼是AD-HOC
ad-hoc 簡而言之就是臨時命令,執行完即結束,並不會保存
2.ad-hoc模式的使用場景
好比在多臺機器上查看某個進程是否啓動,或拷貝指定文件到本地,等等。
3.ad-hoc模式的命令使用
ad-hoc 執行結果返回顏色說明
綠色: 表明被管理端主機沒有被修改,或者只是對遠程節點信息進行查看 黃色: 表明被管理端主機發現變動 紅色: 表明出現了故障,注意查看提示 紫色:表示對命令執行發出警告信息(可能存在的問題,給你一下建議)
1.command命令模塊
# 默認模塊, 執行命令 [root@m01 ~]# ansible 'web_group' -a "hostname" # 若是須要一些管道操做,則使用shell [root@m01 ~]# ansible 'web_group' -m shell -a "ifconfig|grep eth0" -f 50 # -f =forks /etc/ansible/ansible.cfg #結果返回的數量 [root@m01 ~]# ansible 'web_group' -m command -a 'free -m' web02 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 972 140 489 7 342 658 Swap: 1023 0 1023 web01 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 972 113 412 13 446 669 Swap: 1023 0 1023
2.shell
[root@m01 ~]# ansible 'web_group' -m shell -a 'ps -ef|grep nginx' web02 | CHANGED | rc=0 >> root 12584 12583 0 20:16 pts/1 00:00:00 /bin/sh -c ps -ef|grep nginx root 12586 12584 0 20:16 pts/1 00:00:00 grep nginx web01 | CHANGED | rc=0 >> root 14575 14570 0 12:16 pts/1 00:00:00 /bin/sh -c ps -ef|grep nginx root 14577 14575 0 12:16 pts/1 00:00:00 grep nginx
注意:
1)command不支持特殊符號
2)shell模塊支持特殊符號
3)不指定-m 默認使用的是command模塊
3.script腳本模塊
#編寫腳本 [root@m01 ~]# vim vsftpd.sh #!/usr/bin/bash mkdir /tmp/zls #在本地運行模塊,等同於在遠程執行,不須要將腳本文件進行推送目標主機執行 [root@m01 ~]# ansible 'web01' -m script -a '/root/vsftpd.sh' [root@m01 ~]# ansible 'web01' -m shell -a 'ls -l /tmp'
4.yum安裝軟件模塊
[root@m01 ~]# ansible 'web_group' -m yum -a 'name=vsftpd state=present' #至關於:yum install -y vsftpd [root@m01 ~]# ansible 'web_group' -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present' #至關於:yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm [root@m01 ~]# ansible 'web_group' -m yum -a 'name=file:///root/nagios-4.4.3-1.el7.x86_64.rpm state=present' #至關於:yum localinstall -y nagios-4.4.3-1.el7.x86_64.rpm [root@m01 ~]# ansible 'web_group' -m yum -a 'name=vsftpd state=absent' #至關於:yum remove -y vsftpd name httpd #指定要安裝的軟件包名稱 file:// #指定本地安裝路徑(yum localinstall 本地rpm包) http:// #指定yum源(從遠程倉庫獲取rpm包) state #指定使用yum的方法 installed,present #安裝軟件包 removed,absent #移除軟件包 latest #安裝最新軟件包
5.yum_repository
- name: Add repository yum_repository: name: epel description: EPEL YUM repo baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ #添加yum倉庫 ansible 'web_group' -m yum_repository -a 'name=zls_epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=no enabled=yes file=zls_epel' #添加mirrorlist ansible 'web_group' -m yum_repository -a 'name=zls_epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=no enabled=yes file=epel mirrorlist=http://mirrorlist.repoforge.org/el7/mirrors-rpmforge' #刪除yum倉庫 ansible 'web_group' -m yum_repository -a 'name=zls_epel,zls_base file=zls_epel state=absent' #修改yum倉庫 ansible 'web_group' -m yum_repository -a 'name=epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=no enabled=no file=epel' name #指定倉庫名字 description #添加描述(repo文件中的name) baseurl #指定yum倉庫的地址 gpgcheck #是否開啓校驗 yes no enabled #是否啓用yum倉庫 yes no file #指定倉庫文件名 state absent #刪除yum倉庫 present #建立yum倉庫 ansible 'web_group' -m yum_repository -a 'name=zls_yum description=EPEL baseurl=http://www.driverzeng.com gpgcheck=no enabled=no file=zls'
ansible文件管理模塊
1.copy
- name: Copy file with owner and permissions copy: src: /srv/myfiles/foo.conf dest: /etc/foo.conf owner: foo group: foo mode: '0644' #推送文件 [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/index.html dest=/var/www/html owner=root group=root mode=0644' #推送文件並備份 [root@m01 ~]# ansible 'web_group' -m copy -a 'src=/root/index.html dest=/var/www/html owner=root group=root mode=0644 backup=yes' #編輯nfs配置文件 [root@m01 ~]# ansible 'web_group' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' src #指定推送的源文件 dest #指定推送的目標位置 owner #指定屬主 group #指定屬組 mode #指定權限(數字方式) content #在指定文件中添加內容 backup #是否備份(注意:控制端和被控端,內容不一致纔會備份) yes no
2.file文件配置模塊
- name: Create an insecure file file: path: /work owner: root group: root mode: 0755 #建立目錄 mkdir [root@m01 ~]# ansible 'web_group' -m file -a 'path=/backup state=directory owner=adm group=adm mode=0700' #遞歸建立目錄並受權chown -R chmod -R [root@m01 ~]# ansible 'web_group' -m file -a 'path=/zls/mysql/db01 state=directory owner=adm group=adm mode=0700 recurse=yes' #建立文件(前提條件,上級目錄必須存在) touch [root@m01 ~]# ansible 'web_group' -m file -a 'path=/root/zls.txt state=touch' #刪除目錄 rm -fr [root@m01 ~]# ansible 'web_group' -m file -a 'path=/backup state=absent' #作軟連接 ln -s [root@m01 ~]# ansible 'web_group' -m file -a 'src=/root/zls.txt dest=/root/zls.txt.ori state=link' src #指定軟連接的源文件 dest #指定軟鏈接的目標文件 path #指定建立目錄或文件 state touch #建立文件 directory #建立目錄 absent #刪除目錄或文件 link #作軟連接 owner #指定屬主 group #指定屬組 mode #指定權限 recurse #遞歸受權 yes no
3.get_url服務模塊
- name: Download foo.conf get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf mode: '0440' #下載worldpress代碼 [root@m01 ~]# ansible 'web_group' -m get_url -a 'url=http://test.driverzeng.com/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz dest=/root mode=0777' #下載並校驗MD5 [root@m01 ~]# ansible 'web_group' -m get_url -a 'url=http://test.driverzeng.com/Nginx_Code/test.txt dest=/root mode=0777 checksum=md5:ba1f2511fc30423bdbb183fe33f3dd0f' url #指定下載文件的url dest #指定下載的位置 mode #指定下載後的權限 checksum #校驗 md5 #md5校驗 sha256 #sha256校驗
ansible服務管理模塊
1.service,systemd
[root@m01 ~]# ansible 'web_group' -m systemd -a 'name=httpd state=stopped enabled=yes' [root@m01 ~]# ansible 'web_group' -m systemd -a 'name=httpd state=started enabled=yes' [root@m01 ~]# ansible 'web_group' -m systemd -a 'name=httpd state=restarted enabled=yes' [root@m01 ~]# ansible 'web_group' -m systemd -a 'name=httpd state=reloaded enabled=yes' name #指定服務名稱 state started #啓動 stopped #中止 restarted #重啓 reloaded #重載 enabled #是否開機自啓 yes no
ansible用戶管理模塊
1.group
- name: Ensure group "somegroup" exists group: name: somegroup state: present #建立組 [root@m01 ~]# ansible 'web_group' -m group -a 'name=www gid=666 state=present' #刪除組 [root@m01 ~]# ansible 'web_group' -m group -a 'name=www gid=666 state=absent' name #指定組名 gid #指定gid state present #建立 absent #刪除
2.user
- name: Create a 2048-bit SSH key for user jsmith in ~jsmith/.ssh/id_rsa user: name: jsmith generate_ssh_key: yes ssh_key_bits: 2048 ssh_key_file: .ssh/id_rsa #建立用戶 [root@m01 ~]# ansible 'web_group' -m user -a 'name=www uid=666 group=www state=present shell=/sbin/nologin create_home=false' #刪除用戶 [root@m01 ~]# ansible 'web_group' -m user -a 'name=www uid=666 state=absent' #建立用戶的同時建立密鑰對 [root@m01 ~]# ansible 'web_group' -m user -a 'name=zls generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa' name #指定用戶名 uid #指定uid group #指定屬組 groups #指定附加組 state present #建立用戶 absent #刪除用戶 shell #指定用戶登陸的shell /bin/bash /sbin/nologin create_home #是否建立家目錄 true false comment #添加註釋 generate_ssh_key #建立密鑰對 ssh_key_bits #指定密鑰對長度 ssh_key_file #指定密鑰文件
寫主機清單,一鍵部署rsync,nfs,nginx,httpd,上傳做業代碼
1.配置主機
[root@m01 ~]# yum install -y ansible #建立密鑰對 [root@m01 ~]# ssh-keygen #推送公鑰 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.52 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.5 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.6 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61
2.編寫主機清單
[web_group] gjy_web01 ansible_ssh_host=172.16.1.7 gjy_web02 ansible_ssh_host=172.16.1.8 gjy_web03 ansible_ssh_host=172.16.1.9 [db_group] gjy_db01 ansible_ssh_host=172.16.1.51 gjy_db02 ansible_ssh_host=172.16.1.52 [nfs_group] gjy_nfs ansible_ssh_host=172.16.1.31 [backup_group] gjy_backup ansible_ssh_host=172.16.1.41 [daili_group] gjy_lb01 ansible_ssh_host=172.16.1.5 gjy_lb02 ansible_ssh_host=172.16.1.6 [m01] gjy_m01 ansible_ssh_host=172.16.1.61 [rsync_server:children] nfs_group backup_group [nfs_server:children] web_group nfs_group [lnmp_server:children] web_group daili_group
3.先編寫一個比較low的腳本測試下
#!/bin/bash #建立用戶及組 ansible 'all' -m group -a 'name=www gid=666 state=present' &&\ ansible 'all' -m user -a 'name=www uid=666 group=www state=present shell=/sbin/nologin create_home=false' &&\ #部署httpd服務 ansible 'web_group' -m yum -a 'name=httpd,php state=present' &&\ #替換http服務啓動用戶及組 ansible 'web_group' -m shell -a "sed -i '/^User/c User www' /etc/httpd/conf/httpd.conf" &&\ ansible 'web_group' -m shell -a "sed -i '/^Group/c Group www' /etc/httpd/conf/httpd.conf" &&\ #啓動httpd服務 ansible 'web_group' -m systemd -a 'name=httpd state=started enabled=yes' &&\ #上傳代碼,並修改圖片路徑 ansible 'web_group' -m copy -a 'src=/root/httpd_file/ dest=/var/www/html/ owner=www group=www ' #遠程推送做業代碼 ansible 'web_group' -m file -a 'path=/var/www/html/uploads state=directory owner=www group=www' &&\ #部署nfs服務 ansible 'nfs_server' -m yum -a 'name=nfs-utils state=present' &&\ #推送nfs服務配置文件 ansible 'nfs_group' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' &&\ #啓動nfs服務並開機自啓 ansible 'nfs_server' -m systemd -a 'name=nfs-server state=started enabled=yes' &&\ #遠程下載rsync服務 ansible 'rsync_server' -m yum -a 'name=rsync state=present' &&\ #推送rsync配置文件 ansible 'backup_group' -m copy -a 'src=/root/rsync_file dest=/etc/rsyncd.conf' &&\ #推送密碼文件至rsync服務端 ansible 'backup_group' -m copy -a 'content=rsync_backup:123 dest=/etc/rsync.passwd mode=0600' &&\ #推送密碼文件至rsync客戶端 ansible 'nfs_group' -m copy -a 'content=123 dest=/etc/rsync.pass mode=0600' &&\ #啓動rsync服務,並加入開機自啓 ansible 'rsync_server' -m systemd -a 'name=rsyncd state=started enabled=yes' &&\ #遠程下載mariadb服務 ansible 'db_group' -m yum -a 'name=mariadb-server state=present' &&\ #啓動並加入開機自啓 ansible 'db_group' -m systemd -a 'name=mariadb state=started enabled=yes' &&\ #推送nginx 官方源 ansible 'lnmp_server' -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/' &&\ #遠程下載nginx ansible 'lnmp_server' -m yum -a 'name=nginx state=present' &&\ #遠程修改nginx配置文件 ansible 'lnmp_server' -m shell -a "sed -i '/^user/c user www;' /etc/nginx/nginx.conf" &&\ #啓動nginx ansible 'lnmp_server' -m systemd -a 'name=nginx state=started enabled=yes'
4.寫與腳本對應的配置文件
[root@m01 ~]# vim /root/rsync_file uid = www gid = www port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.passwd log file = /var/log/rsyncd.log ##################################### [backup] comment = welcome to oldboyedu backup! path = /backup [data] comment = welcome to oldboyedu nfs! path = /data #傳做業壓縮包到目錄裏,並解壓修改上傳圖片路徑 [root@m01 ~]# cd httpd_file #編寫nginx官方源 [root@m01 ~]# vim /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
5.執行腳本
測試:1.web上http服務,上傳做業代碼
2.lb_group,nginx服務
3.測試db_group ,mysql服務
4.測試nfs配置
5.測試rsync服務