Python自動化運維之Ansible定義主機與組規(guī)則操作詳解
本文實例講述了Python自動化運維之Ansible定義主機與組規(guī)則操作。分享給大家供大家參考,具體如下:
一 點睛
Ansible通過定義好的主機與組規(guī)則(Inventory)對匹配的目標(biāo)主機進行遠程操作,配置規(guī)則文件默認(rèn)是/etc/ansible/hosts。
二 定義主機與組
所有定義的主機與組規(guī)則都在/etc/Ansible/hosts文件中,為ini文件格式,主機可以用域名、IP、別名進行標(biāo)識,其中webservers、dbservers 為組名,緊跟著的主機為其成員。格式如下:
mail.example.com 192.168.1.21:2135 [webservers] foo.example.com bar.example.com 192.168.1.22 [dbservers] one.example.com two.example.com three.example.com 192.168.1.23
其中,192.168.1.21:2135的意思是定義一個SSH服務(wù)端口為2135的主機。
當(dāng)然我們也可以使用別名來描述一臺主機。
jumper ansible_ssh_port=22 ansible_ssh_host=192.168.1.50
jumper為定義的一個別名,ansible_ssh_port為主機SSH服務(wù)端口, ansible_ssh_host為目標(biāo)主機。
更多變量說明如下:
ansible_ssh_host:連接目標(biāo)主機的地址。
ansible_ssh_port:連接目標(biāo)主機SSH端口,端口22無需指定。
ansible_ssh_user:連接目標(biāo)主機默認(rèn)用戶。
ansible_ssh_pass:連接目標(biāo)主機默認(rèn)用戶密碼。
ansible_connection:目標(biāo)主機連接類型,可以是local、ssh或 paramiko。
ansible_ssh_private_key_file:連接目標(biāo)主機的ssh私鑰。
ansible_*_interpreter:指定采用非Python的其他腳本語言,如 Ruby、Perl或其他類似ansible_python_interpreter解釋器。
組成員主機名稱支持正則描述,例如:
[webservers] www[01:50].example.com [databases] db-[a:f].example.com
三 定義主機變量
主機可以指定變量,以便后面供Playbooks配置使用,比如定義主機hosts1及hosts2上Apache參數(shù)http_port及maxRequestsPerChild,目的是讓兩臺主機產(chǎn)生Apache配置文件httpd.conf差異化,定義格式如下:
[atlanta] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
四 定義組變量
組變量的作用域是覆蓋組所有成員,通過定義一個新塊,塊名由 組名+“:vars”組成,定義格式如下:
[atlanta] host1 host2 [atlanta:vars] ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com
五 嵌套組
Ansible支持組嵌套組,通過定義一個新塊,塊名由組名+“: children”組成,舉例如下:
[atlanta] host1 host2 [raleigh] host2 host3 [southeast:children] atlanta raleigh [southeast:vars] some_server=foo.southeast.example.com halon_system_timeout=30 self_destruct_countdown=60 escape_pods=2 [usa:children] southeast northeast southwest southeast
六 分離主機與組特定數(shù)據(jù)
為了更好規(guī)范定義的主機與組變量,Ansible支持 將/etc/ansible/hosts定義的主機名與組變量單獨剝離出來存放到指定的文件中,將采用YAML格式存放,存放位置規(guī)定:“/etc/ansible/group_vars/+組名”和“/etc/ansible/host_vars/+主機名”分別存放指定組名或主機名定義的變量。
七 匹配目標(biāo)
目標(biāo) (Patterns)匹配,格式為:ansible<pattern_goes_here>-m<module_name>-a<arguments>。
舉例說明:重啟webservers組的所有Apache服務(wù)。
ansible webservers -m service -a "name=httpd state=restarted"
<pattern_goes_here>參數(shù)的使用方法,詳細(xì)規(guī)則及含義見下表:
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
簡單介紹Python中利用生成器實現(xiàn)的并發(fā)編程
這篇文章主要介紹了簡單介紹Python中利用生成器實現(xiàn)的并發(fā)編程,使用yield生成器函數(shù)進行多進程編程是Python學(xué)習(xí)進階當(dāng)中的重要知識,需要的朋友可以參考下2015-05-05Python+PyQt5實現(xiàn)開發(fā)Memcached客戶端
這篇文章主要介紹了如何使用Python和PyQt5來制作一個Memcached客戶端,以便我們可以輕松地與Memcached服務(wù)器進行交互,感興趣的小伙伴可以了解一下2023-06-06python實現(xiàn)循環(huán)語句1到100累和
這篇文章主要介紹了python循環(huán)語句1到100累和方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05python中py文件與pyc文件相互轉(zhuǎn)換的方法實例
pyc是一種二進制文件,是由py文件經(jīng)過編譯后,生成的文件,下面這篇文章主要給大家介紹了關(guān)于python中py文件與pyc文件相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2022-05-05