1.ansible批量管理服務部署
1.管理端服務器
a.安裝軟件
yum install -y ansible(要epel源)
01. /etc/ansible/ansible.cfg --- ansible服務配置文件
02. /etc/ansible/hosts --- 主機清單文件
03. /etc/ansible/roles --- 角色目錄
b.編寫主機清單文件
vim /etc/ansible/hosts
c.測試是否可以管理多個主機
ansible all -a "hostname"
d.ansible服務架構信息
1.主機清單配置
2.軟件模塊信息
3.基于秘鑰連接主機
4.主機需要關閉selinux
5.軟件劇本功能
2.ansible軟件模塊應用
ansible的官方文檔doc.ansible.com
ansible學習模塊命令:
a.列出全部模塊 : ansible-doc -l
b.指定一個模塊詳細說明: ansible-doc -s fetch
c.查詢模塊在劇本中的方法: ansible-doc fetch
ansible模塊的語法:
ansible 主機名稱/主機組名稱/主機地址信息/all -m(指定模塊) 模塊名稱 -a(指定動作) "執(zhí)行的動作"
3.第一個模塊: command(默認的模塊可不寫)
1.定義:command - Executes a command on a remote node 在一個遠程主機上執(zhí)行命令
2.基本用法: ansible all -m command -a "cat /etc/hosts"
3.擴展用法:
a. chdir: Change into this directory before running the command. ---切換目錄,默認是在/root下
命令: ansbible all -m command -a "chdir=/test touch lin.txt" 切換到/test目錄下旷余,創(chuàng)建文件lin.txt
b. creates: If it already exists, this step won't be run. --- 如果creates=/dir/file 存在,則不執(zhí)行后面動作
命令: ansible all -m command -a "chdir=/test creates=/test/lin.txt touch wen.txt" 因lin.txt存在惶看,不能touch,如果不存在則可以touch
c. removes: If it already exists, this step will be run. 與creates相反
d. free_form(required) The command module takes a free form command to run.
There is no actual parameter named 'free form'. ;即 用command模塊時,-a參數(shù)后要跟上合法的linux命令
f.PS: variables like 6
ShwRLP5IXGnTa3xcQ/4N2sj4pym3fccJBykjmlayizu97knMWnsXYmywEgNESideMnIHjhvRhYW0534PFjVvB0"
}
2.mkpasswd --method=sha512 --失敗
3. python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password:
m3BJ5CMGFWpLkcs.
6
iJpLuJvRBB.J3CzNbSXNJyFZZyvzzvcfWoetegViTWyTU546g0M5KzN.X38Tc//Hoq2d1KRTVMTS3HyQH6w10/'"
PS:密文一定要加上''號缩举,因為有特殊符號在里面$
2.playbook劇本編寫
01.劇本編寫規(guī)范 yaml格式
a.縮進空格規(guī)范: 要用相同的空格數(shù)進行縮進
b.冒號規(guī)范: :后面要跟空格 (:結尾或在注釋說明中不要加空格)
c.短橫線應用- : 表示列表功能
02.劇本的執(zhí)行:
a.檢查劇本的語法格式:
ansible-playbook --syntax-check rsync.yaml格式
b.模擬執(zhí)行劇本:
ansible-playbook -C rsync.ymal
c.執(zhí)行劇本:
ansible-playbook rsync.ymal
03.rsync服務劇本編寫: vim /etc/ansible/ansible_playbook/rsync.yaml格式
a.先按安裝部署rsync流程來編寫playbook:
- hosts: 192.168.3.18
tasks:
- name: 01-install rsyncd
yum: name=rsync state=installed
- name: 02-copy configer file
copy: src=/etc/rsyncd.conf dest=/etc/
- name: 03-create rsync user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04-create backup dir
file: dest=/backup state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:lin19831214 dest=/etc/rsync.password mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
- hosts: 192.168.3.20
tasks:- name: 01-install rsync
yum: name=rsync state=installed - name: 02-create password file
copy: content=lin19831214 dest=/etc/rsync.password mode=600 - name: 03-create test file
file: dest=/tmp/test.txt state=touch - name: 04-check test
shell: rsync -avz /tmp/test.txt rsync_backup@192.168.3.18::ftp --password-file=/etc/rsync.password
b.劇本編寫常見錯誤:
01.劇本語法規(guī)范是否符合(空格 冒號 短橫線)
02.劇本模塊使用是否正常
03.劇本中一個name標識下面只能寫一個模塊任務信息
04.劇本中盡量不要大量使用shell模塊,有時會報錯匹颤,類似用shell代替user時蚁孔,當user有時,用shell會報錯
3.主機清單配置:
1.分組配置主機信息
[nginx]
192.168.3.17
192.168.3.18
192.168.3.19
[data]
192.168.3.20
192.168.3.21
2.主機名符號匹配配置
a. [nginx]
192.168.3.[17:19]
b. [nginx]
nginx[01:03]
3.非標準遠程端口配置(不是22端口時)
[nginx]
192.168.3.17:port號(比如522)
4.使用特殊的變量 ssh_port -- 商品 ssh_user --用戶 ssh_pass --密碼 ssh_host 主機名 不用/etc/hosts對照
[nginx]
192.168.3.18 ansible_ssh_port=522 ansible_ssh_user=root ansible_ssh_pass=lin123
[nginx]
nginx01 ansible_ssh_host=192.168.3.18 ansible_ssh_port=522 ansible_ssh_user=root ansible_ssh_pass=lin123
5.主機組名嵌入配置
a.[rsync:children] --- 嵌入子組信息
rsync_server
rsync_client
[rsync_server]
192.168.3.18
[rsync_client]
192.168.3.20
b.[rsync:vars]
ansible_ssh_host=192.168.3.18
ansible_ssh_port=522
ansible_ssh_user=root
ansible_ssh_pass=lin123
[rsync]
rsync01
6.ansible變量用法:
1.直接在劇本文件編寫 (引用變量時要用{{ 變量 }} PS:配置文件中要修改惋嚎,不然不能成功
- name: 01-install rsync
- hosts: 192.168.3.18
vars:
backupdir: /backup
passfile: /rsync.password
tasks:- name: 01-install rsyncd
yum: name=rsync state=installed - name: 02-copy configer file
copy: src=/etc/rsyncd.conf dest=/etc/ - name: 03-create rsync user
user: name=rsync create_home=no shell=/sbin/nologin - name: 04-create backup dir
file: dest={{ backupdir }} state=directory owner=rsync group=rsync - name: 05-create password file
copy: content=rsync_backup:lin19831214 dest=/etc/{{ passfile }} state=touch mode=600 - name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
- name: 01-install rsyncd
- hosts: 192.168.3.20
vars:
passfile: rsync.password
tasks:- name: 01-install rsync
yum: name=rsync state=installed - name: 02-create password file
copy: content=lin19831214 dest=/etc/{{ passfile }} mode=600 - name: 03-create test file
file: dest=/tmp/test.txt state=touch - name: 04-check test
shell: rsync -avz /tmp/test.txt rsync_backup@192.168.3.18::ftp --password-file=/etc/{{ passfile }}
2.在命令行中進行指定
ansible-playbook -e(--extra-vars) backupdir=/backup -e passfile=rsync.password rsync_var.yaml
3.在主機清單文件中編寫
[rsync_server:vars]
backupdir=/backup
passfile=rsync.password
[rsync_client]
192.168.3.20
[rsync_client:vars]
passfile=rsync.password
4.三個變量設置的優(yōu)先級: 命令行>劇本變量>主機清單變量
- 劇本設置注冊信息 ---可顯示輸出命令結果信息 register --
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes - name: 07-check server port
shell: netstat -tlnup|grep 874 ---端口信息
register: get_server_port ---注冊端口信息 - name: display port info
debug: msg={{ get_server_port.stdout_lines }} <--注冊的變量在里面
-
在劇本中設置判斷信息 ----用when: 來設置
1.基本用法: - name: 03-create test file
file: dest=/tmp/nfs.txt state=touch
when: (ansible_hostname == "nfs")
- name: 03-create test file
file: dest=/tmp/mysql.txt state=touch
when: (ansible_hostname == "mysql")
- name: 04-check test
shell: rsync -avz /tmp/nfs.txt rsync_backup@192.168.3.18::nfs --password-file=/etc/rsync.password
when: (ansible_hostname == "nfs")
- name: 04-check test
shell: rsync -avz /tmp/mysql.txt rsync_backup@192.168.3.18::mysql --password-file=/etc/rsync.password
when: (ansible_hostname == "mysql")
2.setup模塊可以查看被管理主機系統(tǒng)的詳細信息 ansible 主機 -m setup
3.獲取內置變量方法: ansible 主機 -m setup -a "filter=ansible_hostname"
4.常見主機信息:
"ansible_all_ipv4_addresses": ---僅顯示ipv4信息
"ansible_devices" ---顯示磁盤設備信息
"ansible_distribution": "CentOS" ---顯示系統(tǒng)信息
"ansible_distribution_major_version": "7", ---顯示系統(tǒng)主版本
"ansible_distribution_version": "7.6", ---顯示系統(tǒng)版本
"ansible_machine": "x86_64", ---顯示系統(tǒng)類型 32位或64位
"ansible_ens33": ---顯示ens33信息
"ansible_hostname": ---顯示主機名
"ansible_kernel": "3.10.0-957.el7.x86_64", ---顯示內核版本
"ansible_lvm": ---LVM相關信息
"ansible_memtotal_mb": 972 ---系統(tǒng)總內存
"ansible_memfree_mb": 69, ---可用系統(tǒng)內存
"ansible_memory_mb": ---詳細顯示內存情況
"ansible_swapfree_mb": 2047, ---總的swap內存
"ansible_swaptotal_mb": 2047 ---swap內存的可用內存
"ansible_mounts" ---磁盤掛載情況
"ansible_processor" ---顯示cpu個數(shù) (具體顯示每個cpu的型號)
"ansible_processor_vcpus": 1, ---顯示cpu個數(shù) (只顯示總個數(shù))5.獲取子信息方法: (在劇本中可用,命令行有時沒效果)
ansible 192.168.3.18 -m setup -a "filter=ansible_ens33[ipv4]"
9.在劇本中設置循環(huán)信息:
1.循環(huán)主要是為了合并劇本的重復模塊站刑,比如把兩個copy的模塊合并
2.兩個模塊的命令要特別的相似才行
3.用法:#copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/ mode=644
#copy: src=/etc/ansible/server_file/rsync_server/rsync.password dest=/etc/ mode=600
copy: src=/etc/ansible/server_file/rsync_server/{{ item.src }} dest={{ item.dest }} mode={{ item.mode }}
with_items:
- { src: 'rsyncd.conf', dest: '/etc/', mode: '644' }
- { src: 'rsync.password', dest: '/etc/', mode: '600' }
4.用法2:yum 安裝多軟件時:
- name: installed pkg
yum:
name=['rsync', 'tree', 'wget', 'lrzsz']
state=installed
10.劇本執(zhí)行出現(xiàn)錯誤排查
a.找到書本出現(xiàn)問題關鍵點
b.將劇本中的操作轉換成模塊進行操作
c.將模塊的功能操作轉換成linux命令:
01.本地管理主機上執(zhí)行命令測試
02.遠程被管理主機上執(zhí)行命令測試
d.劇本執(zhí)行卡死 ---ansible-playbook XX.ymal -vvvv(最多四個V)
11.劇本中設置忽略錯誤信息
1.通過在playbook劇本在另伍,在報錯的那行下面寫上- name: 02-copy configer file
copy: rc=/etc/rsyncd.conf dest=/etc/
ignore_errors: yes
2.可以通過設置ignore_errors: yes通過忽略報錯那行,可以執(zhí)行下绞旅,是否下面有報錯有影響
12.劇本中設置標簽功能:
1.原因:主要是為了跳過正常的步驟摆尝,不會再執(zhí)行,浪費時間因悲,直接給報錯那行打上tags
2.配置:- name: 02-copy configer file
copy: rc=/etc/rsyncd.conf dest=/etc/
tags: copy configer --在要執(zhí)行的行下面打tags不能用純數(shù)字
3.命令:ansible-playbook --tags='copy configer' rsync_test.yaml
13.劇本中設置觸發(fā)功能:
1.原因:當nofity配置或文件改變時堕汞,通過handlers:來改變一些模塊
2.配置: - name: 02-copy configer file & password file
copy: src=/etc/ansible/server_file/rsync_server/{{ item.src }} dest={{ item.dest }} mode={{ item.mode }}
with_items:
- { src: 'rsyncd.conf', dest: '/etc/', mode: '644' }
- { src: 'rsync.password', dest: '/etc/', mode: '600' }
notify: restart rsync server
service: name=rsyncd state=started enabled=yes
handlers: ---與tasks對齊 當rsyncd.conf配置文件修改時,才執(zhí)行這下面的任務晃琳,restart rsyncd
- name: restart rsync server
service: name=rsyncd state=restarted
14.ansible性能調優(yōu):
1.原因:在執(zhí)行playbook時讯检,系統(tǒng)有個自動的 TASK [Gathering Facts]琐鲁,這個主要是收集系統(tǒng)的信息,架構人灼,主機類的,影響性能有時能關閉掉
2.方法: gather_facts: no
15.ansible的playbook劇本整合
1.原因:把多個劇本整合在一起成為一個劇本進行執(zhí)行
2.方法: [root@nginx ansible_playbook]# vim site.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
3.命令: ansible-playbook site.yaml
16.ansible程序roles ---規(guī)范
- name: 02-copy configer file
ansible劇本編寫完問題:
01.目錄結構不夠規(guī)范
02.編寫好的任務如何重復調用
03.服務端配置文件改動围段,客戶端參數(shù)信息也自動變化
04.匯總劇本中沒有顯示主機角色信息
05.一個劇本內容信息過多,不容易進行閱讀投放,如何進行拆分
06.這個就是引出了role的使用奈泪,也可以不用
2.規(guī)范目錄結構
01.cd /etc/ansible/roles
02.mkdir {rsync,nfs} ---創(chuàng)建相應角色目錄,可自起名字
03.mkdir {rsync,nfs}/{vars,tasks,templates,handlers,files} ---創(chuàng)建子目錄灸芳,要和官方文檔上一樣
04.說明: ├── nfs
│ ├── files ---保存需要分發(fā)文件目錄
│ ├── handlers ---保存觸發(fā)器配置文件
│ ├── tasks ---保存要執(zhí)行的動作信息文件
│ ├── templates ---保存需要分發(fā)模板文件(與files不同的是涝桅,能支持變量)
│ └── vars ---保存變量信息文件
└── rsync
├── files ---
├── handlers ---
├── tasks ---
├── templates ---
└── vars ---
05.每個子目錄下的編輯文件都是main.yaml除了files目錄
[root@nginx nfs]# tree
.
├── files
│ └── exports
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
└── vars
└── main.yaml
a.tasks/main.yaml: - name: 01-copy conf file
copy: src=exports dest=/etc
notify: restart nfs server
- name: 02-create data dir
file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody
- name: 03-boot server
service: name={{ item }} state=started enabled=yes
with_items:
- rpcbind
- nfs
b.vars/main.yaml: Data_dir: /data
c.files/exports:
/data 192.168.3.0/24(rw,sync)
d.handlers/main.yaml:
- name: restart nfs server
service: name=nfs state=restarted
e.在roles/ vim main.yaml
- hosts: nfs_server
roles:
- nfs_server
f.可以把各個動作分成一個yaml
copy_conf.yaml : - name: 01-copy conf file
copy: src=exports dest=/etc
notify: restart nfs server
create_dir.yaml - name: 02-create data dir
file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody
boot-server.yaml - name: 03-boot server
service: name={{ item }} state=started enabled=yes
with_items:
- rpcbind
- nfs
合并:vim site.yaml - import_playbook: copy_conf.yaml
- import_playbook: create_dir.yaml
- import_playbook: boot-server.yaml
g.tasks/main.yaml --> rsync_server的
- name: 01-install rsync
- name: 01-install rsyncd
yum: name=rsync state=installed - name: 02-copy configer file
template: src=rsyncd.conf dest=/etc/ --copy變成了template 主要是加了配置文件的變量
notify: restart rsync server - name: 03-create rsync user
user: name=rsync create_home=no shell=/sbin/nologin - name: 04-create backup dir
file: dest={{ Data_dir }} state=directory owner=rsync group=rsync - name: 05-create password file
copy: content=rsync_backup:lin19831214 dest=/etc/rsync.password mode=600 - name: 06-start rsync server
service: name=rsyncd state=started enabled=yes