ansbile簡單應用

ansbile簡單應用
一、簡介

Ansible is a radically simple configuration-management, application deployment, task-execution, and multinode orchestration engine.
Design Principles
Have a dead simple setup process and a minimal learning curve
Be super fast & parallel by default
Require no server or client daemons; use existing SSHd
Use a language that is both machine and human friendly
Focus on security and easy auditability/review/rewriting of content
Manage remote machines instantly, without bootstrapping
Allow module development in any dynamic language, not just Python
Be usable as non-root
Be the easiest IT automation system to use, ever.

二、安裝
ansible依賴於Python 2.6或更高的版本、paramiko、PyYAML及Jinja2。
2.1 編譯安裝
解決依賴關係

# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
# tar xf ansible-1.5.4.tar.gz
# cd ansible-1.5.4
# python setup.py build
# python setup.py install
# mkdir /etc/ansible
# cp -r examples/* /etc/ansible
2.2 rpm包安裝
# yum install ansible

注意:不同版本的ansible的功能差異可能較大。

三、簡單應用
ansible通過ssh實現配置管理、應用部署、任務執行等功能,因此,需要事先配置ansible端能基於密鑰認證的方式聯繫各被管理節點。

ansible [-f forks] [-m module_name] [-a args]
-m module:默認爲command

ansible-doc: Show Ansible module documentation
-l, –list List available modules
-s, –snippet Show playbook snippet for specified module(s)
實例應用:

ssh-keygen -t rsa -P ''    
ssh-copy-id -i .ssh/id_rsa.pub root@103.242.135.25   

一、定義host
這裏寫圖片描述

注:一個主機可以定義於多個不同組 解決通信問題:1.直接可以編輯文件加入密碼 2.可以基於ssh祕鑰認證方式進行

ping模塊使用,也可以使用單個主機或者已定義好的組名,這裏使用all,代表所有主機

這裏寫圖片描述

command模塊使用,創建目錄!可以使用linux系統命令

這裏寫圖片描述

shell模塊使用,命令中執行管道,重定向之類的需使用shell模塊

這裏寫圖片描述

shell模塊和command模塊的區別

這裏寫圖片描述

cpoy模塊使用,cp本地的1.html到遠端主機的/root目錄下,必須使用絕對路徑

這裏寫圖片描述

cron模塊,批量執行定時任務

這裏寫圖片描述
state=absent 刪除指定name對應的任務計劃

fetch拉取模塊,將遠端的文件拉到本地,和copy模塊相反

這裏寫圖片描述
file模塊使用,創建目錄,並賦予權限

使用yum模塊,批量安轉tree工具包

ansible all -m yum -a “name=tree state=installed”
name:指定安裝包名
state:指定方式,安裝或卸載
安裝: install (present' orinstalled’, latest'),
卸載:remove (
absent’ or `removed’) a package
ansible all -m yum -a “name=httpd* state=latest” #安裝httpd服務

service模塊使用,關閉服務:stopped 開啓服務:started

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章