微信公衆号:運維開發故事,作者:姜總
- 一、Ansible安裝
- 1.1 yum使用EPEL源安裝
- 1.2 編譯安裝
- 1.3 Git 源碼安裝
- 1.4 pip安裝
- 二、Ansible相關檔案說明
- 2.1 配置檔案
- 2.2 主配置檔案介紹
- 2.3 Inventory主機清單檔案
- 三、實戰演練
- 3.1 環境介紹
- 3.2 Ansible相關指令工具
一、Ansible安裝
Ansible的安裝方法主要有以下三種:
1.1 yum使用EPEL源安裝
$ mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
$ mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
$ wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
$ yum clean all && yum makecache
# 可以使用以下三種方式檢視ansible包的資訊
$ yum info ansible
$ yum list ansible
$ yum list | grep ansible
ansible.noarch 2.9.27-1.el7 epel
ansible-collection-microsoft-sql.noarch 1.1.0-1.el8 AppStream
ansible-collection-redhat-rhel_mgmt.noarch 1.0.0-2.el8 AppStream
ansible-doc.noarch 2.9.27-1.el7 epel
ansible-freeipa.noarch 0.3.8-1.el8 AppStream
ansible-freeipa-tests.noarch 0.3.8-1.el8 AppStream
ansible-inventory-grapher.noarch 2.4.4-1.el7 epel
ansible-lint.noarch 3.5.1-1.el7 epel
ansible-openstack-modules.noarch 0-20140902git79d751a.el7 epel
ansible-pcp.noarch 2.2.1-1.el8 AppStream
ansible-python3.noarch 2.9.27-1.el7 epel
ansible-review.noarch 0.13.4-1.el7 epel
ansible-test.noarch 2.9.27-1.el7 epel
centos-release-ansible-29.noarch 1-2.el8 extras
kubernetes-ansible.noarch 0.6.0-0.1.gitd65ebd5.el7 epel
python2-ansible-runner.noarch 1.0.1-1.el7 epel
python2-ansible-tower-cli.noarch 3.3.9-1.el7 epel
vim-ansible.noarch 3.2-1.el7
# 安裝ansible
$ yum -y install ansible
$ ansible --version
1.2 編譯安裝
$ yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
# 需要提前安裝Python環境(略)
$ wget https://releases.ansible.com/ansible/ansible-2.9.27.tar.gz
$ tar xf ansible-2.9.27.tar.gz
$ cd ansible-2.9.27
$ ls -al
total 160
drwxr-xr-x 2 root root 4096 May 25 05:18 bin
drwxr-xr-x 2 root root 4096 May 25 05:18 changelogs
drwxr-xr-x 3 root root 4096 May 25 05:18 contrib
-rw-r--r-- 1 root root 35148 May 25 05:18 COPYING
drwxr-xr-x 6 root root 4096 May 25 05:18 docs
drwxr-xr-x 3 root root 4096 May 25 05:18 examples
drwxr-xr-x 3 root root 4096 May 25 05:18 hacking
drwxr-xr-x 3 root root 4096 May 25 05:18 lib
drwxr-xr-x 2 root root 4096 May 25 05:18 licenses
-rw-r--r-- 1 root root 13840 May 25 05:18 Makefile
-rw-r--r-- 1 root root 1731 May 25 05:18 MANIFEST.in
drwxr-xr-x 10 root root 4096 May 25 05:18 packaging
-rw-r--r-- 1 root root 7724 May 25 05:18 PKG-INFO
-rw-r--r-- 1 root root 5175 May 25 05:18 README.rst
-rw-r--r-- 1 root root 351 May 25 05:18 requirements.txt
-rw-r--r-- 1 root root 12949 May 25 05:18 setup.py
-rw-r--r-- 1 root root 28352 May 25 05:18 SYMLINK_CACHE.json
drwxr-xr-x 7 root root 4096 May 25 05:18 test
$ python setup.py build
$ python setup.py install
$ mkdir /etc/ansible
$ cp -r examples/* /etc/ansible
1.3 Git 源碼安裝
$ git clone https://github.com/ansible/ansible.git
$ cd ansible
$ git checkout stable-2.9
Updating files: 100% (19674/19674), done.
Branch 'stable-2.9' set up to track remote branch 'stable-2.9' from 'origin'.
Switched to a new branch 'stable-2.9'
$ source ./hacking/env-setup
1.4 pip安裝
$ yum install python-pip python-devel
$ yum install gcc glibc-devel zibl-devel rpm-bulid openss1-devel
$ pip install -U pip
$ pip install ansible --upgrade
二、Ansible相關檔案說明
2.1 配置檔案
-
:主配置檔案,配置ansible工作特性;/etc/ansible/ansible.cfg
-
:主機清單檔案,管理的目标主機位址清單;/etc/ansible/hosts
-
:存放角色的目錄。/etc/ansible/roles/
2.2 主配置檔案介紹
[defaults]
#inventory = /etc/ansible/hosts # 主機清單配置檔案
#library =/usr/share/my_modules/ # 庫檔案存放目錄
#remote_tmp = $HOME/.ansible/tmp # 臨時py指令檔案存放在遠端主機目錄
#local_tmp = $HOME/.ansible/tmp # 本機的臨時指令執行目錄
#forks = 5 # 預設并發數
#sudo_user = root # 預設sudo使用者
#ask_sudo_pass = True # 每次執行ansible指令是否詢間ssh密碼
#ask_pass = True # 是否詢問密碼
#remote_port = 22 # 預設的遠端登入端口
host_key_checking = False # 檢查對應伺服器的host_key,建議取消注釋
log_path=/var/log/ansible.log # 日志檔案,建議啟用
#module_name = command # 預設子產品,可以修改為shell子產品
以上隻是一小部分關于default的相關配置,還有其他更多的配置這裡暫時不一一列舉。因為ansible的大部分參數都可以保持預設,無需更改。但是當你的被管理機器數量增加以後,建議将forks數量适當的調整。
**溫馨提示:**由于Ansible不是一個服務,是以更改完配置後無無需進行重新開機操作(也沒提供重新開機的操作方法),改完配置立即生效。
2.3 Inventory主機清單檔案
主機清單檔案,用大白話講其實就是被整合在一個檔案中的一組或者多組被管控的節點。預設情況下,該清單檔案為:
/etc/ansible/hosts
,通常我們可能會根據項目需求,起一個見名知意的其他名稱,或者也會直接在每一套我們需要組織的項目根目錄下存放一個hosts的清單檔案。
Inventory主機清單檔案可以有多個,在使用plyabook時可以手動通過
-i
參數指定你需要使用的清單檔案名稱。
Inventory檔案遵循了
ini
配置檔案的風格,一個section就表示一組隸屬于同一個分組的被管理機器。目标被管理節點預設均被視為使用SSH的22端口,但是當部分節點使用非22端口号時,也可以在清單檔案中進行指定。
被管理節點可以是以IP的形式寫在清單檔案中,也可以使用域名的形式。
[sites]
172.16.0.10
172.16.0.11
[appsrvs]
192.168.66.[1:30]
[nginx]
nginx1.ayunw.cn
nginx2.ayunw.cn
[apache]
apache1.ayunw.cn:2333
apache2.ayunw.cn
[nginxsrvs]
nginx[10:20].ayunw.cn
[dbservers]
redis1.ayunw.cn
redis2.ayunw.cn
[dbsrvs]
mysql-[a:z].ayunw.cn
三、實戰演練
3.1 環境介紹
我這裡包括ansible主要機和另外兩個被管理機器。
[[email protected] ~]# cat /etc/ansible/hosts
[webservers]
192.168.66.[151:153]
[dbservers]
192.168.66.151
[appservers]
192.168.66.[152:153]
3.2 Ansible相關指令工具
-
:主提序,臨時指令執行工具/usr/bin/ansible
-
:檢視配置文檔,子產品功能檢視工具/usr/bin/ansible-doc
-
:下載下傳/上傳優秀代碼或Roles子產品的自網平台/us/bin/ansible-galaxy
-
:定制自動化任務,編排劇本工具/usr/bin/ansible-playbook
-
:遠端執行指令的工具/usr/bin/ansible-pull
-
:檔案加密工具/usr/bin/ansible-vault
-
:基于Console界面與使用者互動的執行工具/usr/bin/ansible-console
以上指令,最常用的兩個:
ansible
和
ansible-playbook
,這兩個指令就是ansible用來實作批量管裡的關鍵。
- ansible指令通常用于一次性或者臨時的任務,一般一條指令即可實作任務。通常我們稱這種方式為:ad-hoc;
- ansible-playbook主要是應用于針對大型項目的部署,需要通過多個yaml格式的檔案(以yaml或者yml結尾)組合使用,是以一般需要進行提前規劃目錄,整合yaml檔案。
接下來介紹幾個比較常用的指令。
3.2.1 ansible-doc
該指令主要用于顯示針對某個子產品的使用方法的幫助資訊。如果忘記了子產品或者子產品的用法,可以通過該指令快速檢視。
ansible-doc [options][module...]
# 列出可用子產品
-l,--list
# 顯示指定子產品的playbook片段
-s,--snippet
如:
# 列出所有子產品
ansible-doc -l
# 檢視指定子產品幫助用法
ansible-doc copy
# 檢視指定子產品幫助用法[簡化版的幫助]
ansible-doc -s copy
注意: 在使用ansible批量管理操作之前,需要先對所有主機做一個免密認證,以確定每個被管理節點都能在ansible管理節點上通過ssh協定免密登入到被管理節點。
免密的三種實作方式:
- 基于ssh-keygen + shell + sshpass`方法實作
- 基于
實作;expect
- 基于
實作。ansible-playbook
ad-hoc 方式常用參數說明:
-
:顯示版本--version
-
:指定子產品,預設為command-m module
-
:詳細過程-vv -vvv更詳細-V
-
:顯示主機清單,可簡寫–list--list-hosts
-
:提示輸入ssh連接配接密碼,預設key驗證-k,--ask-pass
-
:檢查,并不執行-C,--check
-
:執行指令的逾時時間,預設10s-T,--timeout=TIMEOUT
-
:執行遠端執行的使用者-u,--user=REMOTE_USER
-
:代替舊版的sudo切換-b,--become
-
:指定sudo的runas使用者,預設為root--become-user=USERNAME
-
:提示輸入sudo時的密碼-K,--ask-become-pass
通配符
ansible "*"-m ping
ansible 192.168.1.* -m ping
ansible "srvs" -m ping
[[email protected] ~]# ansible "*" --list-hosts
hosts (3):
192.168.66.151
192.168.66.152
192.168.66.153
邏輯或
ansible "websrvs:appsrvs" -m ping
ansible "192.168.66.151:192.168.66.152" -m ping
邏輯與
# 在websrvs組并且在dbsrvs組中的主機
ansible "webservers:&dbservers" -m ping
192.168.66.151 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
邏輯非
# 在websrvs組,但不在dbsrvs組中的主機
# 注意:此處為單引号
ansible 'webservers:!dbservers' -m ping
正則
ansible "webservers:&dbservers" -m ping
ansible "~(web|db)servers" -m ping
3.2.2 ansible執行指令過程
- 加載自己的配置檔案預設
,如果指定了你自定義的清單檔案,則從自己的清單檔案中查找被管理主機/etc/ansible/ansible.cfg
- 加載自己對應的子產品檔案,如:
command
- 通過ansible将子產品或指令生成對應的臨時py檔案,并将該檔案傳輸至遠端伺服器的對應執行使用者
檔案$HOME/.ansible/tmp/ansible-tmp-數字/XXX.PY
- 給檔案+x執行
- 執行并傳回結果
- 删除臨時py檔案,退出
可以通過加參數
-v
或者
-vvv
列出詳細的執行過程(可以多加幾個v參數)。
[[email protected] ~]# ansible "~(web|db)servers" -vvv -m ping > ansible.log
[[email protected] ~]# grep "chmod" ansible.log
3.2.3 ansible執行後顔色描述
預設情況下是以下三種顔色:
- 綠色:表示成功
- 黃色:修改了遠端檔案後并執行成功
- 紅色:表示執行失敗
但是在ansible配置檔案中可以定義顔色,如下:
[[email protected] ~]# vim /etc/ansible/ansible.cfg
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan
3.2.4 Ansible-playbook示例
[[email protected] ~]# cat echo-demo.yml
---
- hosts: all
remote_user: root
tasks:
- name: echo demo
command: echo "第一個 ansible-playbook 示例"
[[email protected] ~]# ansible-playbook echo-demo.yml
3.2.5 ansible-vault
該工具用于對yaml檔案進行加解密,格式如下:
ansible-vault [create|decrypt|edit|encrypt|rekey|view]
如:
ansible-vault encrypt echo-demo.yml # 加密
ansible-vault view echo-demo.yml # 檢視
ansible-vault decrypt echo-demo.yml # 解密
ansible-vault edit echo-demo.yml # 編輯加密檔案
ansible-vault create echo-demo.yml # 建立新檔案
ansible是一個簡單高效且很強大的工具,它的功能遠不止于此。而那些不常用的指令工具,本文也暫不介紹,學習了不用也是在浪費自己的時間。如果有興趣的話可以去官網或者其他站點查閱資料了解更多适合自己的資訊。
溫馨提示
一名常年穿梭于Google、阿裡、百度、騰訊的一線運維從業者。是<<運維開發故事>>公衆号的成員之一。不定期分享技術幹貨和對技術的了解與感悟。