運維自動化工具ansible
1. 簡介 ansible基於python開發,集合了眾多運維工具的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工作的,本身沒有批量部署的能力。 真正具有批量部署的是ansible運行的模塊,ansible只是一個框架 ansible架構 (1) 連接插件connection plugins: 負責和被監控端實現通信; (2) host inventory: 指定操作的主機,是一個配置文件裏面定義監控的主機 (3) 各種模塊核心模塊、command模塊、自定義模塊; (4) 借助於插件完成記錄日誌郵件等功能; (5) playbook: 劇本執行多個任務時,非必須可以讓節點一次性運行多個任務。 2、特性: (1) no agents: 不需要在被管理主機上安裝任務agent (2) no server: 無服務器端,使用時,直接運行命令即可 (3) modules in any languages: 基於模塊工作,可使用任意語言開發模塊 (4) yaml not code:使用yaml語言定制劇本playbook (5) ssh by default:基於SSH工作 (6) strong multi-tier solution: 可實現多級指揮 3、優點: (1) 輕量級,無需在客戶端安裝agent,更新時,只需要在操作機上進行一次更新即可; (2) 批量任務可以寫成腳本,而且不用分發到遠程就可以執行 (3) 使用python編寫,維護簡單 (4) 支持sudo ansible原理
二、ansible安裝
1.1 rpm包安裝 epel源: centos6: [epel] name=Extra Packages for Enterprise Linux 6 - $basearch baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch enabled=1 gpgcheck=0 centos7 [epel] name=qinghuadaxue centos $releasever dvd baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/ enabled=1 gpgcheck=0 [root@localhost ~]# yum install ansible -y
三、ansible常用選項
1、常用參數:
ansible <host-pattern> [-m module_name][-a args]
--version 顯示版本
-m module 指定模塊,默認為command
-v 詳細過程 –vv -vvv更詳細
--list-hosts 顯示主機列表,可簡寫—list
-k, --ask-pass 提示輸入ssh連接密碼,默認Key驗證
-K, --ask-become-pass 提示輸入sudo時的口令
-C, --check 檢查,並不執行
-T, --timeout=TIMEOUT 執行命令的超時時間,默認10s
-u, --user=REMOTE_USER 執行遠程執行的用戶
-b, --become 代替舊版的sudo 切換
ansible-doc: 顯示模塊幫助
ansible-doc options
-a 顯示所有模塊的文檔
-l, --list 列出可用模塊
-s, --snippet顯示指定模塊的playbook片段
示例:
ansible-doc –l 列出所有模塊
ansible-doc ping 查看指定模塊幫助用法
ansible-doc –s ping 查看指定模塊幫助用法
2、ansible的Host-pattern
邏輯與
ansible “websrvs:&dbsrvs” –m ping
在websrvs組並且在dbsrvs組中的主機
邏輯非
ansible ‘websrvs:!dbsrvs’ –m ping
在websrvs組,但不在dbsrvs組中的主機
註意:此處為單引號
綜合邏輯
ansible ‘websrvs:dbsrvs:&appsrvs:!ftpsrvs’ –m ping
正則表達式
ansible “websrvs:&dbsrvs” –m ping
ansible “~(web|db).*.magedu.com” –m ping
四、常用模塊介紹
copy模塊
目的:把主控本地文件拷貝到遠程節點上
[root@localhost ~]# ansible 192.168.118.14 -m copy -a "src=/root/bigfile dest=/tmp"
192.168.118.14 | SUCCESS => {
......
}
file模塊
目的:更改指定節點上文件的權限、屬主和屬組
[root@localhost ~]# ansible 192.168.118.14 -m file -a "dest=/tmp/bigfile mode=777 owner=root group=root"
192.168.118.14 | SUCCESS => {
......
}
script模塊
目的:通過在對方主機執行本機上腳本文件,並將信息返回到本機
[root@centos7 /app]# ansible test -m script -a "/app/f.sh"
192.168.10.101 | SUCCESS => {
......
}
cron模塊
目的:在指定節點上定義一個計劃任務,每三分鐘執行一次。
[root@localhost ~]# ansible all -m cron -a ‘name="Cron job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/bin/ntpdate tiger.sina.com.cn"‘
192.168.118.14 | SUCCESS => {
......
}
fetch模塊
目的 :從客戶端取文件至服務器端,copy相反,目錄可先tar
[root@centos7 /app]# ansible test -m fetch -a "src=/app/f.sh dest=/app/"
192.168.10.101 | SUCCESS => {
......
}
192.168.10.2 | SUCCESS => {
......
}
[root@centos7 /app]# ls
192.168.10.101 192.168.10.2
可以看出在本機dest下生成兩個目錄分別存放所拉取得文件
group模塊
目的:在遠程節點上創建一個組名為ansible,gid為2016的組
[root@localhost ~]# ansible 192.168.118.14 -m group -a "name=ansible gid=2016"
192.168.118.14 | SUCCESS => {
......
}
user模塊
目的:在指定節點上創建一個用戶名為ansible,組為ansible的用戶
[root@localhost ~]# ansible 192.168.118.14 -m user -a "name=ansible uid=2016 group=ansible state=present"
192.168.118.14 | SUCCESS => {
......
}
刪除遠端節點用戶,註意:刪除遠程用戶,但是不會刪除該用戶的家目錄
[root@localhost ~]# ansible 192.168.118.14 -m user -a "name=ansible state=absent"
192.168.118.14 | SUCCESS => {
......
}
yum 模塊
目的:在遠程節點安裝vsftpd
[root@localhost ~]# ansible 192.168.118.14 -m yum -a ‘name=vsftpd state=present‘
192.168.118.14 | SUCCESS => {
......
}
卸載寫法:
[root@localhost ~]# ansible 192.168.118.14 -m yum -a ‘name=vsftpd state=removed‘
192.168.118.14 | SUCCESS => {
......
}
service模塊
啟動
[root@localhost ~]# ansible 192.168.118.14 -m service -a ‘name=vsftpd state=started enabled=yes‘
192.168.118.14 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "vsftpd",
"state": "started"
}
停止
[root@localhost ~]# ansible 192.168.118.14 -m service -a ‘name=vsftpd state=stopped enabled=yes‘
192.168.118.14 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "vsftpd",
"state": "stopped"
}
ping模塊
[root@localhost ~]# ansible 192.168.118.14 -m ping
192.168.118.14 | SUCCESS => {
"changed": false,
"ping": "pong"
}
command模塊
[root@localhost ~]# ansible 192.168.118.14 [-m command] -a ‘w‘ # -m command可以省略就表示使用命名模塊
192.168.118.14 | SUCCESS | rc=0 >>
14:00:32 up 3:51, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.118.69 18:09 3:29 0.12s 0.12s -bash
root pts/1 192.168.118.13 14:00 0.00s 0.04s 0.00s /bin/sh -c LANG
註意: ansible srvs -m command -a ‘echo magedu |passwd --stdin wang’ 不成功
command模塊不支持 $VARNAME < > | ; & 等,用shell模塊實現
shell模塊
[root@centos7 ~]# ansible test -m shell -a "echo ‘$HOSTNAME‘"
192.168.10.101 | SUCCESS | rc=0 >>
centos7.5
192.168.10.2 | SUCCESS | rc=0 >>
centos7.5
[root@centos7 ~]# ansible test -m shell -a ‘echo "$HOSTNAME"‘
192.168.10.101 | SUCCESS | rc=0 >>
server01
192.168.10.2 | SUCCESS | rc=0 >>
centos6.magedu.com
註意:上面$HOSTNAME的單雙引號的位置,單引號為強應用,雙引號為弱引用。
raw模塊
主要的用途是在command中添加管道符號
[root@localhost ~]# ansible 192.168.118.14 -m raw -a ‘hostname | tee‘
192.168.118.14 | SUCCESS | rc=0 >>
localhost.localdomain
get_url模塊
目的:將http://192.168.118.14/1.png 下載到本地
[root@localhost ~]# ansible 192.168.118.14 -m get_url -a ‘url=http://192.168.118.14/1.png dest=/tmp‘
192.168.118.14 | SUCCESS => {
......
}
synchronize模塊
目的:將主空方目錄推送到指定節點/tmp目錄下
[root@localhost ~]# ansible 192.168.118.14 -m synchronize -a ‘src=/root/test dest=/tmp/ compress=yes‘
192.168.118.14 | SUCCESS => {
......
}
五、ansible playbooks
4.1 http安裝:
- hosts: eb
vars:
http_port: 80
max_clients: 256
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: ensure apache is running
service: name=httpd state=started
4.2 mysql安裝
- hosts: 192.168.118.14
vars:
remote_user: root
max_clients: 256
mysql_name: "mysql-server"
tasks:
- name: ensure install mysql
yum: name="{{mysql_name}}" state=present
- name: ensure apache is running
service: name=mysqld state=started
-
handlers
用於當關註的資源發生變化時采取一定的操作.
“notify”這個action可用於在每個play的最後被觸發,這樣可以避免多次有改變發生時每次都執行指定的操作,取而代之,僅在所有的變化發生完成後一次性地執行指定操作。在notify中列出的操作稱為handler,也即notify中調用handler中定義的操作。
1 - hosts: web 2 remote_user: root 3 tasks: 4 - name: install apache 5 yum: name=httpd 6 - name: install config 7 copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf 8 notify: 9 - restart httpd # 這觸發 restart httpd 動作 10 - name: start httpd 11 service: name=httpd state=started 12 handlers: 13 - name: restart httpd 14 service: name=httpd state=restarted
註意:測試使用ansible2.1版本,每執行一次如上腳本,- name: start httpd都會執行一次,因此可以不用使用handlers
-
調用setup模塊中的變量
1 - hosts: web 2 remote_user: root 3 tasks: 4 - name: copy file 5 copy: content="{{ansible_all_ipv4_addresses}}" dest=/tmp/a.txt
-
when 條件判斷
1 - hosts: all 2 remote_user: root 3 vars: 4 - username: test 5 tasks: 6 - name: create {{ username }} user. 7 user: name={{ username }} 8 when: ansible_fqdn == "localhost.localdomain" # 當條件匹配到,才會創建test用戶
-
使用with_items進行叠代
1 - hosts: web 2 remote_user: root 3 tasks: 4 - name: yum install packages 5 yum: name={{ item.name }} state=present 6 with_items: 7 - { name: ‘mysql-server‘ } 8 - { name: ‘vsftpd‘ }
-
template 使用
使用場景: 當多個服務修改的參數不一致時。
拷貝/etc/httpd/conf/httpd.conf到指定目錄,修改Listen使用變量
Listen {{ http_port }}
在ansible hosts中定義變量
14 [web]
15 192.168.2.12 http_port=8000
劇本寫法:
8 - name: install config
9 template: src=/root/temp/{{http_name}}.j2 dest=/etc/httpd/conf/httpd.conf # 使用template模塊
[root@ansible ~]# cat httpd.yml
- hosts: all
remote_user: root
tasks:
- name: install http
yum: name=httpd state=present
- name: copy file
template: src=/root/httpd.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: restart httpd
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[web]
192.168.118.14 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22 http_port=8888 maxClients=50
[myhost]
192.168.118.49 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22 http_port=9999 maxClients=100
-
tag的使用
使用場景:當一個playbook只需要執行某一個步驟的時候定義
劇本寫法
9 template: src=/root/temp/{{http_name}}.j2 dest=/etc/httpd/conf/httpd.conf
10 tags:
11 - conf
-
roles的用法:
mkdir -pv ansible_playbooks/roles/web/{templates,files,vars,tasks,meta,handlers} cp -a /etc/httpd/conf/httpd.conf files/ vim tasks/main.yml 1 - name: install httpd 2 yum: name=httpd 3 - name: install configuration file 4 copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf 5 tags: 6 - conf 7 notify: 8 - restart httpd 9 - name: start httpd 10 service: name=httpd state=started vim handlers/main.yml 1 - name: restart httpd 2 service: name=httpd state=restarted
[root@server1 ansible_playbooks]# ls
roles site.yml
[root@server1 ansible_playbooks]# vim site.yml
1 - hosts: web
2 remote_user: root
3 roles:
4 - web
[root@server1 ansible_playbooks]ansible-playbook site.yml
運維自動化工具ansible