Ansible 組件介紹
本章主要通過對 Ansible 經常使用的組件進行講解,使對 Ansible 有一個更全面的了解,主要包含以下內容:
- Ansible Inventory
- Ansible Ad-Hoc 命令
- Ansible playbook
- Ansible facts
- Ansible role
- Ansible Galaxy
Ansible Inventory
Inventory 組件主要存盤在配置管理作業中需要管理的不同業務的不同機器的資訊,默認 Ansible 的 Inventory 是靜態的 INI 格式的檔案/etc/ansible/hosts,可以通過 ANSIBLE_HOSTS 環境變數指定或者運行 ansible 和 ansible-playbook 的時候用 -i 引數臨時設定,
定義主機和主機組
首先看下默認 Inventory 檔案是如何定義主機和主機組的,默認的 Inventory 檔案如下:
# - 主機組由[header]元素分隔
# - 您可以輸入主機名或IP地址
# - hostname/ip 可以是多個組的成員
# 未組合的主機,在任何主機組之前指定,
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# 屬于'webservers'組的主機集合
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# 如果有多個主機遵循模式
## www[001:006].example.com
# 'dbservers'組中的資料庫服務器集合
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
## db-[99:101]-node.example.com
多個 Inventory 串列
Ansible 支持多個 Inventory 檔案,方便管理維護不同業務或環境中的機器,下面介紹如何使用多個 Inventory 檔案,
首先新建一個檔案夾用來存放 Inventory 檔案
mkdir inventory
并在檔案夾內新建檔案,webservers 和 hosts,
hosts 檔案如下:
10.1.90.59
10.1.90.69
webservers 檔案如下:
[webservers]
10.1.90.59
10.1.90.69
[ansible:children]
webservers
然后修改 ansible.cfg 檔案中的 inventory 的默認路徑
inventory = /root/ansible/inventory/
這樣就可以使用 ansible 的list-hosts 引數來進行驗證
ansible 10.1.90.59:10.1.90.69 --list-hosts
回傳:
hosts (2):
10.1.90.59
10.1.90.69
動態 Inventory
動態 Inventory 其實可以通過把 ansible.cfg 檔案中的 inventory 默認路徑改為一個腳本,
腳本需要支持兩個引數
- list或者-l ,這個引數顯示所有主機以及主機組的資訊(json格式)
- host或者-H ,引數后面指定一個host,會顯示這臺主機的所有資訊(json格式)
下面是 hosts.py 腳本:
import argparse
import sys
import json
def list():
r={}
h=['10.1.90.'+ str(i) for i in (59,69)]
hosts={'host':h}
r['webservers']=hosts
return json.dumps(r,indent=4)
def hosts(name):
r={'ansible_ssh_pass':'123'}
cpis=dict(r.items())
return json.dumps(cpis)
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('-l','--list',help='host list',action='store_true')
parser.add_argument('-H','--host',help='hosts vars')
args=vars(parser.parse_args())
if args['list']:
print list()
elif args['host']:
print hosts(args['host'])
else:
parser.print_help()
執行腳本函式 python hosts.py -l,回傳如下:
{
"webservers": {
"host": [
"10.1.90.59",
"10.1.90.69"
]
}
}
執行腳本函式 python hosts.py -H 10.1.90.59,回傳如下:{"ansible_ssh_pass": "123"}
執行臨時指定 hosts.py 腳本,
ansible -i hosts.py 10.1.90.59 -m ping -o,回傳結果:
10.1.90.59 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
如果報錯: [WARNING]: * Failed to parse /root/ansible/inventory/hosts.py with script plugin: problem running /root/ansible/inventory/hosts.py
--list ([Errno 13] Permission denied)
需要給予執行權限chmod +x hosts.py
如果報錯:"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
需要安裝 sshpass
yum -y install sshpass
Inventory 內置引數
| 引數 | 解釋 | 例子 |
|---|---|---|
| ansible_ssh_host | 定義 host ssh 地址 | ansible_ssh_host=10.1.90.59 |
| ansible_ssh_port | 定義 hosts ssh 埠 | ansible_ssh_port=22 |
| ansible_ssh_user | 定義 hosts ssh 認證用戶 | ansible_ssh_user=wupx |
| ansible_ssh_pass | 定義 hosts ssh 認證密碼 | ansible_ssh_pass=123 |
| ansible_sudo | 定義 hosts sudo的用戶 | ansible_sudo=wupx |
| ansible_sudo_pass | 定義 hosts sudo密碼 | ansible_sudo_pass=123 |
| ansible_sudo_exe | 定義 hosts sudo 路徑 | ansible_sudo_exe=/usr/bin/sudo |
| ansible_ssh_private_key_file | 定義 hosts 私鑰 | ansible_ssh_private_key_file=/root/key |
| ansible_shell_type | 定義 hosts shell 型別 | ansible_shell_type=bash |
| ansible_python_interpreter | 定義 hosts 任務執行 python 的路徑 | ansible_python_interpreter=/usr/bin/python2.6 |
| ansible_*_interpreter | 定義 hosts 其他語言決議器路徑 | ansible_ruby_interpreter=/usr/bin/ruby |
Ansible Ad-Hoc 命令
Ad-Hoc 其實就是臨時命令,Ad-Hoc 是相對于 Ansible-playbook 而言的,Ansible 提供兩種完成任務方式:一種是 Ad-Hoc 命令集,即ansible,另一種就是 Ansible-playbook,即命令 Ansible-playbook,前者更注重于解決一些簡單的或者平時作業中臨時遇到的任務,相當于Linux系統命令列下的Shell命令,后者更適合與解決復雜或需固化下來的任務,相當于Linux系統的Shell Scripts,
執行命令
Ansible 命令都是并發執行的,默認的并發數由 ansible.cfg 中的 forks 值來確定,也可以在執行命令時通過 -f 指定并發數,
使用命令回傳 webservers 組所有主機的 hostname,并指定并發數為 5:ansible webservers -m shell -a 'hostname' -f 5 -o
執行結果:
192.168.46.129 | CHANGED | rc=0 | (stdout) web2
192.168.46.128 | CHANGED | rc=0 | (stdout) web1
使用異步執行,-P 0 的情況下會直接回傳 job_id,然后針對主機根據 job_id 查詢執行結果:ansible webservers -B 120 -P 0 -m shell -a 'sleep 10;hostname' -f 5 -o
執行結果:
192.168.46.128 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "ansible_job_id": "899260515938.13222", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/899260515938.13222", "started": 1}
192.168.46.129 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "ansible_job_id": "245835357736.13147", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/245835357736.13147", "started": 1}
可以根據 job_id 通過 async_status 模塊查看異步任務的狀態和結果:ansible 192.168.46.128 -m async_status -a 'jid=899260515938.13222'
執行結果:
192.168.46.128 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"ansible_job_id": "899260515938.13222",
"changed": true,
"cmd": "sleep 10;hostname",
"delta": "0:00:10.024378",
"end": "2019-09-22 08:54:35.364400",
"finished": 1,
"rc": 0,
"start": "2019-09-22 08:54:25.340022",
"stderr": "",
"stderr_lines": [],
"stdout": "web1",
"stdout_lines": [
"web1"
]
}
當-P 引數大于 0 時,Ansible 會自動根據 job_id 輪詢查詢執行結果:ansible webservers -B 120 -P 1 -m shell -a 'sleep 10;hostname' -f 5 -o
執行結果:
192.168.46.128 | CHANGED => {"ansible_job_id": "892179643372.13778", "changed": true, "cmd": "sleep 10;hostname", "delta": "0:00:10.023149", "end": "2019-09-22 09:01:57.625917", "finished": 1, "rc": 0, "start": "2019-09-22 09:01:47.602768", "stderr": "", "stderr_lines": [], "stdout": "web1", "stdout_lines": ["web1"]}
192.168.46.129 | CHANGED => {"ansible_job_id": "506204522875.13683", "changed": true, "cmd": "sleep 10;hostname", "delta": "0:00:10.029853", "end": "2019-09-22 09:01:59.039427", "finished": 1, "rc": 0, "start": "2019-09-22 09:01:49.009574", "stderr": "", "stderr_lines": [], "stdout": "web2", "stdout_lines": ["web2"]}
復制檔案
可以使用 copy 模塊來批量下發檔案,檔案的變化是通過 MD5 值來判斷的:ansible webservers -m copy -a 'src=https://www.cnblogs.com/wupeixuan/p/hosts dest=/root/hosts owner=root group=root mode=644 backup=yes' -o
回傳結果:
192.168.46.129 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "83e11aa2eb53ae6ea2476c05a8448696018401db", "dest": "/root/hosts", "gid": 0, "group": "root", "md5sum": "a154d2b2131e6420e51e293c540e4cb5", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 1021, "src": "/root/.ansible/tmp/ansible-tmp-1569169777.73-89235977935/source", "state": "file", "uid": 0}
192.168.46.128 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "checksum": "83e11aa2eb53ae6ea2476c05a8448696018401db", "dest": "/root/hosts", "gid": 0, "group": "root", "md5sum": "a154d2b2131e6420e51e293c540e4cb5", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 1021, "src": "/root/.ansible/tmp/ansible-tmp-1569169777.73-148086225859296/source", "state": "file", "uid": 0}
驗證檔案下發功能:ansible webservers -m shell -a 'md5sum /root/hosts' -t 5 -o
回傳結果:
192.168.46.128 | CHANGED | rc=0 | (stdout) a154d2b2131e6420e51e293c540e4cb5 /root/hosts
192.168.46.129 | CHANGED | rc=0 | (stdout) a154d2b2131e6420e51e293c540e4cb5 /root/hosts
包和服務管理
可以直接使用 Ad-Hoc 命令來管理包和服務:
ansible webservers -m yum -a 'name=httpd state=latest' -f 5 -o
ansible webservers -m service -a 'name=httpd state=started' -f 5 -o
ansible webservers -m shell -a 'rpm -qa httpd' -f 5 -o
驗證服務運行情況:ansible webservers -m shell -a 'netstat -tpln|grep httpd' -f 5
回傳結果:
192.168.46.128 | CHANGED | rc=0 >>
tcp6 0 0 :::80 :::* LISTEN 16853/httpd
192.168.46.129 | CHANGED | rc=0 >>
tcp6 0 0 :::80 :::* LISTEN 43184/httpd
用戶管理
首先通過 openssl 命令生成密碼(因為ansible user 的 password 引數需要接受加密后的值):echo ansible | openssl passwd -1 -stdin
回傳結果:$1$RBXBgM3M$WE3mYCc2gIlFIircO3unx.
使用 user 模塊批量新建用戶:ansible webservers -m user -a 'name=test password="$1$RBXBgM3M$WE3mYCc2gIlFIircO3unx."' -f 5 -o
回傳結果:
192.168.46.128 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/test", "name": "test", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001}
192.168.46.129 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/test", "name": "test", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001}
通過 SSH 登錄驗證新建用戶是否成功(密碼為 ansible):ssh 192.168.46.128 -l test
Ansible playbook
playbook 是 Ansible 進行配置管理的組件,是來彌補 Ad-Hoc 命令無法支撐復雜環境的配置管理作業的,playbook 是 Ansible 的重要組件之一,因此放在下一篇來對 Ansible 的 playbook 進行詳細講解,
Ansible facts
facts 組件是 Ansible 用于采集被管機器設備資訊的功能,可使用 setup 模塊查看機器所有 facts 資訊,或使用 filter 來查看指定資訊(回傳的結果是 JSON 格式),
查看機器的所有 facts 資訊:ansible 192.168.46.128 -m setup
查看機器的 ipv4 資訊:ansible 192.168.46.128 -m setup -a 'filter=ansible_all_ipv4_addresses'
回傳結果:
192.168.46.128 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.46.128"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
使用 facter 擴展 facts 資訊
Ansible 的 facts 組件會判斷被控機器上是否安裝 facter 和 ruby-json 包,若存在,Ansible 的 facts 會采集 facter 資訊,
查看是否安裝 facter 和 ruby-json:ansible 192.168.46.128 -m shell -a 'rpm -qa ruby-json facter'
運行 facter 模塊查看 facter 資訊:ansible 192.168.46.128 -m facter
使用 ohai 擴展 facts 資訊
Ansible 的 facts 組件會判斷被控機器上是否安裝 ohai 包,若存在,Ansible 的 facts 會采集 ohai 資訊,
查看是否安裝 ohai:ansible 192.168.46.128 -m shell -a 'gem list|grep ohai'
運行 ohai 模塊查看 ohai 資訊:ansible 192.168.46.128 -m ohai
直接運行 setup 模塊也會采集 facter 和 ohai 資訊,
Ansible role
role 只是對我們使用的 playbook 的目錄結構進行一些規范,
這是一個 role 的目錄結構:
├── roles
├── dbsrvs -------------role1名稱
│ ├── files -------------ansible中unarchive、copy等模塊會自動來這里找檔案,從而我們不必寫絕對路徑,只需寫檔案名
│ │ ├── mysql.tar.gz
│ │ └── nginx.tar.gz
│ ├── handlers -----------存放tasks中的notify指定的內容
│ │ └── main.yml
│ ├── meta
│ ├── tasks --------------存放playbook的目錄,其中main.yml是主入口檔案,在main.yml中匯入其他yml檔案,要采用import_tasks關鍵字,include要棄用了
│ │ ├── install.yml
│ │ └── main.yml -------主入口檔案
│ ├── templates ----------存放模板檔案,template模塊會將模板檔案中的變數替換為實際值,然后覆寫到客戶機指定路徑上
│ │ └── nginx.conf.j2
│ └── vars ----------存放變數檔案
└── websrvs -------------role2名稱
│ ├── files
│ │ ├── mysql.tar.gz
│ │ └── nginx.tar.gz
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ ├── install.yml
│ │ └── main.yml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
└── site.yml -------------role參考的入口檔案
執行 role:ansible-playbook -i /etc/ansible/hosts site.yml
Ansible Galaxy
Galaxy 是 Ansible 官方分享 role 的功能平臺,可將自己撰寫的 role 通過 ansible-galaxy 上傳到 Galaxy 網站,也可通過 ansible-galaxy 命令實作 role 的分享和安裝,使用ansible-galaxy install就可以安裝 role,默認安裝路徑為/etc/ansible/roles/,
總結
本章主要介紹一些 Ansible 常用的組件,主要包含 Ansible Inventory、playbook、facts、role、Galaxy 等,下一篇將對 Ansible 中的 playbook 進行詳細講解,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/6656.html
標籤:其他
上一篇:CSAPP 3 程式的機器級表示
下一篇:一、計算機網路概論
