ansible是新出現的自動化運維工具,基於Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優勢,實現了批量系統配置、批量程序部署、批量運行命令等功能。linux
ansible是基於模塊工做的,自己沒有批量部署的能力。真正具備批量部署的是ansible所運行的模塊,ansible只是提供一種框架。shell
模塊化,調用特定的模塊,完成特定的任務,基於Python語言實現,易於擴展;編程
部署簡單,agentless,無需客戶端安裝軟件,服務端經過鏈接插件connection plugins和被監控端實現通訊;bash
playbook:可讓節點調用角色模板一次性運行多個任務快速部署基礎環境。架構
ansible所執行的操做主要是定義管理者所指望的對象狀態,屢次執行命令的結果具備冪等性框架
冪等性本來是數學上的概念,即便公式:f(x)=f(f(x)) 可以成立的數學性質。用在編程領域,則意爲
對同一個系統,使用一樣的條件,一次請求和重複的屢次請求對系統資源的影響是一致的
。less
ansible-doc 命令用於獲取ansible相關幫助頁面,-l是查看可用模塊,-s可顯示指定模塊說明。運維
ansible的命令行執行格式以下:ssh
ansible <host-pattern> [-f forks] (host分批) -m module_name -a args 模塊化
command:執行命令無須爲key=value格式,而是直接給出要執行的命令便可。
注意,command爲默認調用模塊能夠不使用-m指定。
示例1:
[root@ansible-manager ~]# ansible directors -a "ifconfig"192.168.7.151 | SUCCESS | rc=0 >>ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.7.151 netmask 255.255.255.0 broadcast 192.168.7.255 .........
示例2:chdir選項可指定命令運行的目錄
[root@ansible-manager ~]# ansible directors -a "chdir=/var ls"192.168.7.151 | SUCCESS | rc=0 >>
..........
示例3:command模塊並不能理解執行 花括號命令行展開,管道符傳遞等特殊命令形式
[root@ansible-manager ~]# ansible directors -a "ifconfig |grep ens33"192.168.7.152 | FAILED | rc=255 >>SIOCSIFADDR: No such device
..........
示例4: 上例的問題可用shell模塊解決
[root@ansible-manager ~]# ansible directors -m shell -a "ifconfig |grep ens33" 192.168.7.151 | SUCCESS | rc=0 >>ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
.........
user -a 'name= state={present|absent} system= uid='
示例:
[root@ansible-manager ~]# ansible directors -m user -a "name=wind state=present password=abc321"192.168.7.152 | SUCCESS => { "changed": true, "comment": "", #用戶的描述信息 "createhome": true, "group": 1000, "home": "/home/wind", "name": "wind", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, #非系統用戶 "uid": 1000}
到客戶機上驗證結果:
[root@client2 ~]# id winduid=1000(wind) gid=1000(wind) groups=1000(wind)
cron -a 'name= minute= hour= day= month= weekday= job= user= state='
示例:
[root@ansible-manager ~]# ansible all -m cron -a "name=ntptime minute=*/3 job='/usr/sbin/ntpdate 192.168.7.138 &>/dev/null '"192.168.7.152 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "ntptime" ]}
注意:
默認的state就是present,刪除的時候能夠僅指定name和state=absent.
job='character string' job的內容必須加單引號,不然報錯.
任務計劃可否成功建立並不受客戶機是否有相關命令影響,沒有安裝ntpdate命令也能夠成功建立.
copy -a 'dest= src= mode= owner= group='
示例: src參數不帶絕對路徑的話默認是複製當前目錄下的指定文件,支持相對路徑。
[root@ansible-manager ~]# ansible directors -m copy -a "src=repo.tar dest=/root"
192.168.7.151 | SUCCESS => {
[root@ansible-manager ~]# ansible directors -m copy -a "src=../home/wind/1.txt dest=/root/2.txt"192.168.7.151 | SUCCESS => {
注意:若是src以「/」結尾則僅複製目錄中的內容,不以此結尾就是複製目錄及其中內容src不是必選項由於能夠用content直接生成目標文件的內容(可生成空文件)。
yum -a 'name= state={present|latest|absent}'
示例:
ansible directors -m yum -a "name=ntpdate"
192.168.7.151 | SUCCESS => {
"changed": true,
fetch 從遠程主機取回文件到本地,
示例:
[root@ansible-manager ~]# ansible 192.168.7.151 -m fetch -a "src=/root/2.txt dest=remotefile"
命令運行後在本地直接生成了remotefile目錄,並且將遠程主機上的絕對路徑也複製到了本地指定目錄的遠程主機名目錄之下。即./remotefile/REMOTE_HOST/REMOTE_PATH
若是不須要此種多級目錄結構,可以使用flat選項,其幫助文件說明以下:
flat: # If dest ends with '/', it will use the basename of the source file, similar to the copy module. Obviously this is only handy if the filenames are unique.
ansible 192.168.7.152 -m fetch -a "src=/root/2.txt src=/root/3.txt flat=yes dest=test/"
[root@ansible-manager ~]# ls test/2.txt 3.txt
使用此選項並不會生成多級目錄,可是顯然這個選項不適合從多主機上拉取文件的狀況。
會形成多主機重複寫入同一文件。
若是在多個遠程主機的拉取文件時使用了flat選項,執行命令後,則會輪流覆蓋寫入拉回本地的文件,執行結果多個主機依次變爲黃色changed。
緣由是,依照hosts文件中定義的主機上下順序,依次對比遠程主機文件與ansiblemanager本地文件的校驗值,若是發生改變則覆蓋,執行結果黃色;校驗值同的主機顯示爲綠色狀態未變動。
此種執行方式僅爲興趣探究,實際中並沒有意義,從多個主機取回文件必然是不但願互相覆蓋的。
注意:
src必須指向遠程主機上的文件,不能夠是目錄。能夠指定多個src
使用flat=yes選項時,適合拉取單臺主機文件的狀況,下降目錄複雜性。
file -a 'path= mode= owner= group= state={directory|link|present|absent} src='
示例:
[root@ansible-manager ~]# ansible directors -m file -a "path=/root/a.txt src=/root/2.txt state=link"192.168.7.152 | SUCCESS => { "changed": true, "dest": "/root/a.txt", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 11, "src": "/root/2.txt", "state": "link", "uid": 0}
state選項說明:
If 'directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions.
If 'file', the file will NOT be created if it does not exist, see the [copy] or [template] module if you want that behavior.
If 'link', the symbolic link will be created or changed. Use 'hard' for hardlinks.
If 'absent', directories will be recursively deleted, and files or symlinks will be unlinked. Note that 'absent' will not cause 'file' to fail if the 'path' does not exist as the state did not change.
If 'touch' (new in 1.4), an empty file will be created if the 'path' does not exist, while an existing file or directory will receive updated file access and modification times (similar to the way 'touch' works from the command line).
若是是directionary,1.7版本後, path指定的目錄都將依據提供的權限設置遞歸建立。
若是是file,path指定的文件若是原來不存在, 那麼也不會被建立。若是須要新建一個文件請使用copy或template模塊,或者使用touch參數(建立空文件)。
若是是link,符號連接將被建立或更改。使用hard選項建立 hardlinks(也就是說默認軟鏈接)。
若是是absent, 目錄將被遞歸地刪除, 文件或符號連接將被取消連接。請注意, path不存在時不會致使file執行失敗,由於命令會返回狀態未改變的結果。
若是是touch, (1.4新增參數), 若是path不存在, 則會建立一個空文件, 而若是path是現有或目錄時將更新文件access和modifycation時間 (相似命令行下執行touch命令)。
ping 沒有參數,僅測試是否能夠ping通
示例:
[root@ansible-manager ~]# ansible directors -m ping 192.168.7.151 | SUCCESS => { "changed": false, "ping": "pong"}192.168.7.152 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.7.152 port 22: Connection timed out\r\n", "unreachable": true}
service -a 'name= state={started|stopped|restarted} enabled='
示例:
[root@ansible-manager ~]# ansible directors -m service -a "name=httpd state=restarted"192.168.7.151 | SUCCESS => {
.......會顯示很長的詳細狀態信息
Playbooks 是 Ansible的配置,部署,編排語言。是一種但願遠程主機執行所需命令的方案,或者一組IT程序運行的命令集合.
tasks: 任務
任務列表
variables: 變量
ansible有內置變量與自定義變量,變量定義能夠在命令行,主機清單,角色目錄下的變量目錄等多處進行。
templates: 模板
主要用於服務的配置文件的參數傳遞至被控主機
handlers: 觸發調用任務
適用於如配置文件變動時觸發重啓/重載等任務。須要tasks任務列表中使用notify調用。
roles: 角色
經過對以上元素的整合,達成一整套的服務自動配置的目的。有固定的目錄組織形式,各元素分別定義。在playbook中經過對host安排角色進行調用。
具體的每一個元素再也不詳述,能夠參考ansible中文手冊,下面以實例來演示。
建立目錄
[root@ansible-manager ansible]# mkdir -p roles/keepalived/{tasks,handlers,meta,templates,vars,files}
[root@ansible-manager ansible]# tree roles/
roles/
└── keepalived
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
7 directories, 0 files
tasks⽬錄:存放task列表。若role要⽣效,此⽬錄必需要有⼀個主task⽂件main.yml,在main.yml中可使⽤include包含同⽬錄(即tasks)中的其餘⽂件。handlers⽬錄:存放handlers的⽬錄,若要⽣效,則⽂件必須名爲main.yml⽂件。files⽬錄:在task中執⾏copy或script模塊時,若是使⽤的是相對路徑,則會到此⽬錄中尋找對應的⽂件。templates⽬錄:在task中執⾏template模塊時,若是使⽤的是相對路徑,則會到此⽬錄中尋找對應的模塊⽂件。vars⽬錄:定義專屬於該role的變量,若是要有var⽂件,則必須爲main.yml⽂件。defaults⽬錄:定義⾓⾊默認變量,⾓⾊默認變量的優先級最低,會被任意其餘層次的同名變量覆蓋。若是要有var⽂件,則必須爲main.yml⽂件。meta⽬錄:⽤於定義⾓⾊依賴(dependencies),若是要有⾓⾊依賴關係,則⽂件必須爲main.yml。
以上引自博客園——駿馬金龍的ansible教程。
各目錄具體配置
tasks
main.yml
- name: yum install keepalived
yum: name=keepalived state=present
when: ansible_distribution_major_version == '7' #無實際意義的條件判斷僅測試下when語句
- name: copy script
copy: src=notify.sh dest=/etc/keepalived
- name: copy conf
template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
notify: restart keepalived
- name: start keepalived
systemd: name=keepalived state=started
- name: show vip
shell: /usr/sbin/ip addr show |grep {{ vip }}
handlers
main.yml
- name: restart keepalived
systemd: name=keepalived state=restarted
templates
keepalived.conf.j2
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id {{ ansible_hostname }} #引用內置變量
vrrp_mcast_group1 224.0.0.33
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance vip1 {
state {{ keepalived_state }} #主機清單定義變量
interface {{ ansible_default_ipv4.interface }} #引用內置變量的子參數需以VAR.XX的形式
virtual_router_id 51
priority {{ pri }} #主機清單定義變量
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
{{ vip }} #自定義變量
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
files
notify.sh
vars
main.yml
vip: 192.168.7.120 #必須是字典形式
roles所在目錄定義hosts以及playbook
hosts
[directors]
192.168.7.151 keepalived_state=MASTER pri=99
192.168.7.152 keepalived_state=SLAVE pri=90
keepalived.yml
- hosts: directors
roles:
- keepalived