目錄html
Ansible是一個簡單的自動化引擎,可完成配置管理,應用部署,服務編排以及其餘各類IT需求。Ansible也是一款使用Python語言開發實現的開源軟件,其依賴Jinja2,Paramiko和PyYAML這幾個庫linux
Ansbile的優勢:web
Ansible做爲自動化系統運維的一大利器,在構建整個體系過程當中有着舉足輕重的地位。其簡單易用,易於安裝,功能強大,便於分享,內含大量模版等都是它的魅力所在,再加上易封裝,接口調用方便,Ansible正在被愈來愈多的大公司採用。shell
Ansilbe
管理員節點和遠程主機節點經過ssh協議進行通訊。apache
Ansible配置的時候只須要與被控端作免密便可。json
# Redhat/CentOS Linux上,Ansible目前放在的epel源中 # Fedora默認源中包含ansible,直接安裝包既可 [root@master ~]# yum install epel-release [root@master ~]# yum install ansible -y
配置Linux不一樣機器間免密 略...vim
什麼是Host Invenory(主機目錄,主機清單)?架構
Host Inventory是配置文件,用來告訴Ansible須要管理那些主機。而且把這些主機根據需分類。運維
默認的配置文件是:/etc/ansible/hosts
ssh
最簡單的host文件:
192.168.32.130
Ansible 提供了一個命令行工具
ansible命令的格式是:
ansible <host-pattern> [options]
檢查ansible安裝環境
[root@192.168.32.130 /etc/ansible]$ ansible all -m ping -u root 192.168.32.130 | SUCCESS => { "changed": false, "ping": "pong" }
執行命令
[root@192.168.32.130 /etc/ansible]$ ansible all -a "ls /etc/ansible" 192.168.32.130 | SUCCESS | rc=0 >> ansible.cfg hosts hosts.py roles
拷貝文件
[root@192.168.32.130 /tmp]$ ansible all -m copy -a "src=/etc/ansible/hosts dest=/tmp/" 192.168.32.130 | SUCCESS => { "changed": true, "checksum": "2e304266c75c95987fb111c5482443bb41408cd7", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "827317b4e0cd727bf245f2044319d31d", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 15, "src": "/root/.ansible/tmp/ansible-tmp-1572083478.93-155762973748871/source", "state": "file", "uid": 0 }
安裝包
[root@192.168.32.130 /tmp]$ ansible all -m shell -a "yum -y install nc" 192.168.32.130 | SUCCESS | rc=0 >> Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: ftp.sjtu.edu.cn * remi-safe: ftp.riken.jp * updates: mirrors.163.com Package nc-1.84-24.el6.x86_64 already installed and latest version Nothing to do
添加用戶
[root@192.168.32.130 /tmp]$ ansible all -m shell -a "useradd jack" 192.168.32.130 | SUCCESS | rc=0 >>
並行執行
# 開啓10個線程執行 [root@192.168.32.130 /tmp]$ ansible all -a "ip addr" -f 10 192.168.32.130 | SUCCESS | rc=0 >> 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b8:5c:ad brd ff:ff:ff:ff:ff:ff inet 192.168.32.130/24 brd 192.168.32.255 scope global eth0 inet6 fe80::20c:29ff:feb8:5cad/64 scope link valid_lft forever preferred_lft forever 3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN link/ether 16:86:d7:59:08:da brd ff:ff:ff:ff:ff:ff # 查看遠程主機的所有系統信息 [root@192.168.32.130 /tmp]$ ansible all -m setup
只有腳本才能夠重用,避免總敲重複的代碼
Ansible腳本的名字叫 Playbook,使用YAML的格式,文件以yml結尾
YAML 和 JSON 相似,是一種表示數據的格式
執行腳本的方法:[root@192.168.32.130 /tmp]$ ansible-playbook xxx.yml
yml文件的功能能夠寫一些部署、啓停服務邏輯,例如:安裝Apache,步驟以下:
一、 安裝Apache包
二、 拷貝配置文件httpd,並保證拷貝文件後,apache服務會被重啓
三、 拷貝默認的網頁index.html
四、 啓動Apache服務
yml文件包含如下幾個關鍵字:
示例:
- hosts: web vars: http_port: 80 max_clients: 200 remote_user: root tasks: - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: Write the configuration file template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: - restart apache - name: Write the default index.html file template: src=templates/index.html.j2 dest=/var/www/html/index.html - name: ensure apache is running service: name=httpd state=started handlers: - name: restart apache service: name=httpd state=restarted
不懂yml沒有關係,上面的yml格式能夠轉化爲json格式:
[ { "hosts": "web", "vars": { "http_port": 80, "max_clients": 200 }, "remote_user": "root", "tasks": [ { "name": "ensure apache is at the latest version", "yum": "pkg=httpd state=latest" }, { "name": "Write the configuration file", "template": "src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf", "notify": [ "restart apache" ] }, { "name": "Write the default index.html file", "template": "src=templates/index.html.j2 dest=/var/www/html/index.html" }, { "name": "ensure apache is running", "service": "name=httpd state=started" } ], "handlers": [ { "name": "restart apache", "service": "name=httpd state=restarted" } ] } ]
playbook 是指一個能夠被ansible執行的yml文件
module就是Ansible的「命令」,即執行任務的方式,常見的module有yum、copy、shell
例如使用mudule copy拷貝文件
主機目錄的文件,遠程機的臨時文件存儲位置,管理機的臨時文件存儲文件
[root@192.168.32.130 /etc/ansible]$ vim ansible.cfg
inventory = /etc/ansible/hosts library = /usr/share/my_modules/ remote_tmp = ~/.ansible/tmp local_tmp = ~/.ansible/tmp
鏈接端口號"accelerate_port",超時時間等。
accelerate_port = 5099 accelerate_timeout = 30 accelerate_connect_timeout = 5.0 accelerate_daemon_timeout = 30 accelerate_multi_key = yes
主機目錄管理,告訴ansible須要管理那些server,和server的分類和分組信息
# 默認文件 /etc/ansible/hosts # 修改主機目錄的配置文件 ... inventory = /etc/ansible/hosts ... # 命令行中傳遞主機目錄配置文件 $ ansible-playbook -i hosts site.yml 或者參數—inventory-file $ ansible-playbook --inventory-file hosts site.yml
遠程主機的分組([]內是組名):
[webservers] foo.example.com [databases] db-[a:f].example.com
指定Server的鏈接參數,包括鏈接方法、用戶等
執行playbook的語法
ansible-playbook deploy.yml
查看輸出的細節
ansblie-playbook playbook.yml --verbose
查看該腳本影響哪些hosts
ansible-playbook playbook.yml --list-hosts
並行執行腳本
ansible-playbook playbook.yml -f 10
最基本的playbook腳本分爲三個部分:
在什麼機器上以什麼身份執行