優(yōu)點(diǎn)
一款自動(dòng)運(yùn)維工具,實(shí)現(xiàn)批量系統(tǒng)配置,批量程序部署,批量運(yùn)行命令。且備操作的機(jī)器無(wú)需安裝客戶端。
重點(diǎn)
- 安裝基本概念
-
playbook(劇本),批量執(zhí)行大量人物
安裝
部署客戶機(jī)和控制端的本地解析
vim /etc/hosts
#編輯完成后將文件傳給所有的機(jī)器
scp /etc/hosts ipaddress:/etc
yum 安裝ansible管理端
配置
- 將
/etc/hosts解析過(guò)的名字放入到ansible的hosts中/etc/ansible/hosts - 測(cè)試連通性
ansible localhost -m ping - 簡(jiǎn)潔輸出參數(shù)
-o使輸出的信息放在一行顯示 - 測(cè)試主機(jī)(ansible的本質(zhì)是檢測(cè)ssh能否連接,并不是真的在ping目標(biāo)機(jī)器)
組和密碼
組即是在/etc/ansible/hosts/中,想要放入一個(gè)組的幾個(gè)客戶機(jī)名字前寫入一行[],在中括號(hào)內(nèi)鍵入組名。
組中可以省略寫入的行數(shù),如果客戶端的機(jī)器是有一部分相同(以host為例)且后面的內(nèi)容按照數(shù)字順序排列的,則可以使用host[1:n]來(lái)表示.
如果這一批的賬號(hào)密碼相同的話,以這種方式省略顯示時(shí),可以在一段的后面進(jìn)行批量賬密設(shè)置:
ansible_ssh_user='*'
ansible_ssh_pass='*'
ansible_ssh_port='*'
也可以在組的后面使用一下書寫方式同意給予組的變量:
[group:vars]
ansible_ssh_user='*'
ansible_ssh_pass='*'
ansible_ssh_port='*'
組嵌套,子組和父組的設(shè)置:
[web1]
[web2]
[webserver:children]
web1
web2
Ad-Hoc點(diǎn)對(duì)點(diǎn)模式 模塊化使用
信息收集模塊 setup
ansible host3 -m setup -a 'filter = ***'
#不使用filter時(shí)會(huì)顯示全部的信息
文件模塊 file
ansible host3 -m file -a 'path=/tmp/text state=touch mode=777'
#創(chuàng)建文件
ansible host3 -m file -a 'path=/tmp/dir state=directory mode=777'
#創(chuàng)建目錄
軟件包管理 yum
ansible host3 -m yum -a 'name="httpd" state=latest'
服務(wù)模式 service
ansible host3 -m service -a 'name="httpd" state=started enble=yes'
#state中使用過(guò)去式 enble確認(rèn)開機(jī)啟動(dòng)狀態(tài)
用戶模式user
ansible host3 -m user -a 'name=* state=present'
#添加用戶
ansible host3 -m user -a 'name=* state=absent'
#刪除用戶
echo 1 | openssl passwd -1 stdin
ansible -m user -a 'name=* password=* '
#添加密碼時(shí)要先使用加密之后才能使用,此處使用1作為密碼
ansible webserver -m user -a 'name=* shell=/sbin/noglogin append=yes'
#控制登錄的shell
復(fù)制模塊copy
ansible -m copy -a 'src=* dest=* owner=* group=* mode=* backup=*'
#src源文件,dest目標(biāo)文件,backup為是否備份,如果等于yes,如果兩個(gè)文件完全相同不會(huì)有操作,但是略有不同則會(huì)創(chuàng)建一個(gè)新文件
Shell模式
ansible -m shell -a ' '
#參數(shù)中可以輸入任何的shell命令,但是打開如vim等軟件的操作會(huì)不好使
Role-角色扮演
YAML 語(yǔ)言使用
在ansible中,playbook使用yaml語(yǔ)言進(jìn)行編寫,實(shí)現(xiàn)對(duì)集群的大批量更改和大量命令一起順序執(zhí)行,其書寫的yaml語(yǔ)言格式如下
-hosts: webservers #1
tasts: #2
- name: install apache #3
yum: name=httpd state=present
- name: transport conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start apache
service: name=httpd state=started
#1 第一行是一對(duì)特殊的k-v對(duì),用來(lái)確定備操作的機(jī)器
#2 tasts,命令。執(zhí)行的命令寫于此字典內(nèi)。前輸入兩個(gè)空格
#3 -引導(dǎo)k-v對(duì),每一個(gè)冒號(hào)后都要有一個(gè)空格的存在
此實(shí)驗(yàn)是使用apache作為典型,在執(zhí)行playbook之前,先對(duì)apache的配置文件在當(dāng)前目錄下進(jìn)行了想要的修改,然后再通過(guò)ansible來(lái)將其傳遞給想要操作的集群中的每一臺(tái)機(jī)器。
觸發(fā)器handlers
識(shí)別判斷條件,在判斷條件觸發(fā)時(shí),執(zhí)行handlers內(nèi)的操作
-hosts: webservers
tasts:
- name: install apache
yum: name=httpd state=present
- name: transport
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart #1
- name: start apache
service: name=httpd state=started
handlers:
- name: restart #2
service: name=httpd state=restarted
#1此處為handlers的判斷條件,關(guān)鍵詞為notify,其后接的名字要和handlers內(nèi)的#2處的name相同
執(zhí)行playbook
ansible-playbook apache.yaml
#其后可接參數(shù)實(shí)現(xiàn)不同的操作
ansible-playbook apache.yaml --syntax-check #語(yǔ)法校驗(yàn)
ansible-playbook apache.yaml --list-hosts #列出備操作的主機(jī)
ansible-playbook apache.yaml --list-tasks #查看執(zhí)行的任務(wù)
Role模塊化
- 使用Role模塊化可以使代碼組織結(jié)構(gòu)可讀,代碼復(fù)用,層次清晰.以下實(shí)驗(yàn)使用nginx作為模型,來(lái)進(jìn)行操作。
創(chuàng)建目錄結(jié)構(gòu)
tree roles
roles
├── nginx
│ ├── files
│ │ └── index.html
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
│ └── main.yaml
└── site.yaml
使用Roles的模塊化必須擁有如上的目錄結(jié)構(gòu),可以通過(guò)如下命令創(chuàng)建
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/nginx/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 'balabalabala' > roles/nginx/files/index.html
#在本機(jī)有nginx服務(wù)的前提下
cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
編寫
在tasks/main.yaml中寫入自己想要執(zhí)行的命令的全部,如下
--- #1
- name: install nginx
yum: name={{ item }} state=latest #2
with_items:
- epel-release
- nginx
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf #3
notify: restart nginx
- name: make nginx run
service: name=nginx state=start enabled=yes
#1 在role的模塊化中,使用---作為開始。
#2 在ansible中調(diào)用變量要使用{{ }}將變量引用起來(lái)。
#3 在ansible中,若配置文件存在變量,在不同機(jī)器上運(yùn)行時(shí)想得到不同的結(jié)果,則可用template來(lái)進(jìn)行類復(fù)制操作。
對(duì)template(模板) roles/nginx/templates/nginx.conf.j2中的兩個(gè)參數(shù)添加變量:
worker_process {{ ansible_processor_cores }} #調(diào)用內(nèi)部變量
workdr_connections {{ worker_connections }} #調(diào)用自定變量
將自定義的變量寫入 roles/nginx/vars/main.yaml
worker_connection: 10240 #鍵值對(duì)形式存在
將觸發(fā)器寫入到roles/nginx/handlers/main.yaml中
---
- name: restart nginx
service: name=nginx state=restarted
將需要操作的機(jī)器和roles名寫入roles/site.yaml中
- hosts: host1
roles:
- nginx
執(zhí)行的時(shí)候是將上述的文件進(jìn)行操作
ansible-playbook site.yaml --syntax-check #語(yǔ)法測(cè)試
ansible_playbook site.yaml