《Ansible自動化運維:技術與最佳實踐》第三章讀書筆記
- 2019 年 10 月 6 日
- 筆記
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=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 進行詳細講解。