ansible優化與目錄結構與ansible-shell使用

1.開啓ssh長鏈接ansible.cfg
ssh_args = -C -o ControlMaster=auto -o ControlPersist=7d
ControlPersist=7d整個長鏈接保持時間;經過ssh鏈接過的設備都會在當前ansible/cp/目錄下生成一個socket文件,經過netstat命令查看會發現一個ESTABLISHED狀態的連接一直與遠端設備進行TCP連接(SSH版本5.6或之上)
2.開啓pipeliningansible.cfg
將本地生成好的python腳本put到遠端服務器開啓pipelining大大提升整個執行效率;開啓pipelining須要被控制機/etc/sudoers 文件編輯當前ansible ssh用戶配置爲requiretty
pipelining = True
3.開啓accelerate模式
依賴ansible中控制機和遠端機器有一個長連接,accelerate使用python程序在遠端機器上運行一個守護進程;開啓accelerate,只要在playbook中配置accelerate:true便可;若是開啓這個模式,則須要在控制機和遠端機器上安裝python-keyzar軟件包。ansible.cfg中定義一些accelerate參數,例如遠端機器監聽端口以及一些timeout設置
[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 5.0
4.設置facts緩存
在執行playbook的時候,默認第一個task都是gather_facts,這個過程就是手機每臺主機的facts信息,若是不須要這些信息則設置爲False提升playbook效率。若是既想每次執行playbook的時候都能收集facts,又想加速收集過程,那麼就須要配置facts緩存了。
gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile
fact_caching_connection = /dev/shm/ansible_fact_cache
能夠經過redis存儲facts信息
gathering = smart
fact_caching_timeout = 86400
fact_caching = redis
支持memcached存儲facts信息python

ansible統一目錄結構:
production #production環境的inventory文件
stage #stage環境的inventory文件
group_vars/
group #定義變量的文件
group2
host_vars/
hostname1 #定義hostname1的變量文件
hostname2 #定義hostname2的變量文件
library/ #自定義模塊存放目錄
filter_plugins #自定義filter 插件存放目錄
site.yml #playbook統一入口文件
webservers.yaml #特殊任務playbook文件
roles #role存放目錄
common/ #common角色目錄
tasks/
main.yml #common角色task入口文件
handlers/ #common 角色 handlers入口文件
main.yml
templates/
ntp.conf.j2
file/
bar.txt #file資源文件
foo.sh #files資源文件
vars/
main.yml #common 角色變量定義文件
defaults/
main.yml #common 角色變量定義文件(優先級低)
meta/
main.yml #common 角色依賴文件
webtier/ #webtier角色目錄
monitoring/
fooapp/git

目錄維護方便與管理方便,其中幾個主要目錄定義好github

灰度發佈與檢測
1.語法檢測:
ansible-playbook命令的--syntax-check
2.灰度發佈
意思是先挑選一臺機器進行測試,這樣才知道是不是咱們須要的結果
進行預運行是須要把一個或多個task使用delegate_to參數指定到一臺設備上進行測試,經過後再進行接下來的工做
3.檢查結構是否達到預想
運行playbook的時候使用--check和--diff參數對比生成後的文件是否爲咱們所需的文件web

統一管理
使用gitlab或者GitHub私有倉庫進行管理正則表達式

使用ansible-shell交互命令行
ansible-shell 是GitHub上的一個開源項目,經過交互式的模式把全部ansible的命令Ad-Hoc命令都引入了,支持tab鍵補齊方式;目前ansible-shell 只支持Ad-Hoc命令不支持playbook.
安裝:
git clone https://github.com/dominis/ansible-shell.git
cd ansible-shell/
python setup.py install
安裝後/usr/bin/生成一個ansible-shell命令,能夠經過運行ansible-shell命令進入ansible-shell ,默認不跟任何參數, 但ansible-shell會引入ansible.cfg裏面的參數,固然也能夠指定參數,好比指定inventory、ansible-shell -i hosts、sudo模式ansible-shell -s ,還能夠指定用戶信息等等。還能夠在ansible.cfg文件中添加對ansible-shell參數定義
[ansible-shell]
cwd = 172.19.95.175
forks = 4
默認ansible-shell有幾個內置命令
cd 切換inventory對象,支持正則表達式
list 顯示當前目錄下的主機和主機組列表
forks 臨時設置併發數
become 設置become模式,例如su或sudo
! 強制調用shell模塊redis

hellp shellshell

相關文章
相關標籤/搜索