Ansible Playbooks的使用

playbooks概念:
Tasks:
任務:由各模塊所支持執行的特定操做任務:
例子:-m user -a ‘name= password=’
Variables:
變量:
Templates:
模板:
在定義模板以後能夠實現各節點對應的變量來取代,表達式自身會根據當前節點
所賦值作運算,以後生成的值則賦予這個參數,用於生成不一樣配置的配置文件,
因此模板主要實現配置不一樣場景文本文件。並且這樣使用模板語言來定義,模板
語言中能夠根據定義替換成特定主機的某些值。
Handlers:
處理器:
若是某一次操做配置文件發生改變的話,遠程和本地配置文件不一致,那麼確定
是將其配置文件須要進行覆蓋,這樣須要在配置文件發送改變的時候須要觸動某
些機制進行操做。其實就是事先定義好的能夠在某些條件下被觸發的操做:
Roles:
角色:
層次型組織playbook及其所依賴的各類資源的一種機制;角色可被單獨調用;
彼此之間的server服務器的配置確定是徹底不同的,若是對於這些節點同時
配置並提供一樣的playbooks是不可能的,因此將其每一個server分紅組
之間有些角色或者功能是相同的,那麼好比web server之間可能有時間同步等,
某些服務是通用的,所以有了這些機制以後可以實現按須要將某個類特定服務器
相同操做定義基礎配置,將特定功能定義成一類操做
playbooks是基於YAML語言所定義的聲明式配置文件
laybooks
contain one or more plays
Written in YAML #使用YAML編寫的文檔
-declarative config 配置文件聲明
-not code #沒有代碼
Executed in the order it is written(aka Imperative)nginx

數據結構包括:
每一個橫線所表示引導一個列表中的一貫,經過縮進所表示具備相同縮進的元素是同一個列表的
元素以下所示,定義playbook的結構化部分:
– name:deploy web server #定義一個任務模塊,跟上這個名稱
user:foouser #使用user模塊定義其使用的用戶是foouer
sudo:True #是否容許sudo權限
hosts:all #對全部的主機模塊生效web

tasks: #將一個任務定義應用某一個具體操做
– name:install apache #定義名稱爲install apache
apt: pkg=apache2-mpm-prefork state=latest
#使用apt模塊進行安裝狀態爲latest
首先來定義一個httpd服務,並讓其可以開啓自動啓動服務
web.YAML
– name: web servers
remote_user: root
hosts: taokey
tasks:
– name: install httpd
yum: name=httpd state=present
– name: httpd service
service: name=httpd enabled=yes state=started
ansible-playbook web.yaml
[root@vmware1 ~]# ansible-playbook web.yamlshell

PLAY [install httpd servive] **************************************************apache

GATHERING FACTS ***************************************************************
ok: [192.168.1.82]vim

TASK: [install] ***************************************************************
changed: [192.168.1.82]服務器

TASK: [service on] ************************************************************
changed: [192.168.1.82]數據結構

PLAY RECAP ********************************************************************
192.168.1.82 : ok=3 changed=2 unreachable=0 failed=0
ok = 3 有3個設定執行成功,分別是1.安裝 2.修改服務啓動狀態 3.開啓服務
以上是playbook的最簡單用法
4、YAML
4.1 YAML介紹
YAML是一個可讀性高的用來表達資料序列的格式。YAML參考了其餘多種語言,包括:XML、
C語言、Python、perl以及電子郵件格式RFC2822等。Clark Evans在2001年在首次發表了這
種語言,另外Ingy dot Net與Oren Ben-Kiki也是這語言的共同設計者。
YAML不是xml。不過,在開發的這種語言時,YAML的意思實際上是:「還是一種標記語言」:
其特點:
YAML的可讀性好
YAML和腳本語言的交互性好
YAML使用實現語言的數據類型
YAML有一個一致的信息模型
YAML易於實現
YAML能夠基於流來處理
YAML表達能力強,擴展性好ide

4.2 YAML語法
YAML的語法和其餘高階語言相似,而且能夠簡單表達清單、散列表、標量等數據結構。其結構
經過空格來展現,序列裏的項用「-」來表示,Map裏的鍵值對用「:」分隔。下面是一個示例。
name: John Smith
age: 41
gender: Male
spouse:
name: Jane Smith
age: 37
gender: Female
children:
– name: Jimmy Smith
age: 17
gender: Male
– name: Jrnny Smith
age 13
gender: Female
YAML最關鍵的部分爲列表,字段 和映射
列表的全部元素均使用「-」大頭,例如:#A list of tasty fruits。
#A list of tasty fruits
-Apple
-Orange
-strawberry
-Mango
必定要作好縮進,經過空格進行排序,排列整齊的認爲是同一級別的
列表的全部元素
5、Ansible基本元素
facts
在套用某個主機以前先獲取其各類相關數據的一種機制ui

獲取某個節點的變量信息:
[root@vmware1 ~]# ansible taokey -m setup
直接返回了許多數據,這裏所描述的功能表示基於setup所獲取指定主機各類變量this

在跟對方節點通訊以前能夠先將對方節點返回一些可用信息,而這些信息都是經過facts所獲取
的,而這些信息都是變量 都是能夠被調用的。
有些變量頗有用,好比:
「ansible_os_family」: 「RedHat」,
「ansible_pkg_mgr」: 「yum」,
根據反饋的結果再來決定使用哪些組件來管理

能夠經過這些東西來判斷應用哪些任務
得到對方的線程,無非是cpu個數x2,那麼在變量中也有體現,以下所示:
「ansible_processor_cores」: 1,
變量的種類
1.自定義變量
2.facts
3.主機/invertory變量 host variables
4.組變量,將多個主機定義爲一個組,以下所示:
[taokey]
hostip
[taokey:vars]
ntp_server=ntp.xxx.com
組中能夠嵌套一個組
[apache]
host_1
host_2
[nginx]
ngx1.xx.com

[webserver:children]
nginx
apache

[webservers:vars]
ntp_server=ntp.xxx.com
所以常見變量有以上四類

根據以前的反饋來判斷任務是否執行成功
獲取到某一指定的結果狀態纔會運行,好比:
tasks:
-command: /bin/false
register: result
ingnore_errors: True
第二種方式:
– command: /bin/something
when: result|failed
when中能夠判斷此前某個結果是不是所指望的,進而根據其作出判斷的

迭代:
還能夠實現遍歷循環操做,以下所示:
在主機組taokey中,添加3個用戶
– name: add user
remote_user: root
hosts:taokey

tasks:
– name: add serveral users
user: name=`items` state=present
with_items:
– test4
– test5
– test6
這裏變量必須是以`items`進行標記,而調用變量必須是with_items:進行賦值。

讓各自執行安裝,可是要使用yum安裝,而且只有對方包管理器是yum
須要用到變量判斷的功能
涉及到:when
首先須要使用setup獲取對方變量,若是使用的匹配yum,那麼再進行操做
[root@vmware1 ~]# ansible taokey -m setup
涉及變量:「ansible_pkg_mgr」: 「yum」,
涉及參數:
when: ansible_pkg_mgr == 「yum」
當變量爲yum的時候才執行操做
– name: install zsh
remote_user: root
hosts: a
tasks:
– name: install zsh
yum: name=zsh state=present
when: ansible_pkg_mgr == 「yum」

[root@vmware1 ~]# ansible-playbook zsh.yaml

PLAY [install zsh] ************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.1.82]

TASK: [install zsh] ***********************************************************
changed: [192.168.1.82]

PLAY RECAP ********************************************************************
192.168.1.82 : ok=2 changed=1 unreachable=0 failed=0

補充:在使用with_items的時候還可使用hashes
user:name={{item,name}} state=present groups=`item`.`group`
with_items:
-{name:’test1′,gourp: ‘wheel’}
-{name:’test2′,gourp: ‘root’}

經過以上的操做,簡單來講就是由多個列表,其主要功能就是將事先歸併的主機,先經過tasks定義好的角色
聯合起來共同完成一個任務

關於狀態碼
若是命令或腳本的退出狀態碼不爲0,那麼可使用一下
方式代替
tasks:
– name: run this xxx
shell: /usr/bin/somecommand || /bin/true
#或:
ignore_errors: True
playbook基礎組建
handlers 觸發操做
handlers用於當關注的資源發生變化時採起必定的操做
就是說只有觸發的時候纔會操做

使用notify這個action能夠用於每一個play的最後被觸發,這樣能夠避免屢次發生改變每次都執行指定的操做
因此能夠指定每次何時出發handlers
官方給出的例子:
– name: template config file
template: stc=template.j2 dest: /etc/foo.conf #萬一這個配置文件發生了改變,那麼執行下面操做
notify:
– restart service
– restart service(httpd)
handlers:
– name restart memcache
service: name=memcache state=restarted
– name: restart httpd
service: name=httpd state=restarted
以上意思爲,若是src的配置文件發生了改變,那麼則觸發這個handlers定義的操做

接下來咱們定義一個httpd的配置文件,當文件發生改變的時候,則推送這個配置文件到
各個節點並重啓httpd服務。
[root@agent ~]# mkdir httpd
[root@agent ~]# cd httpd/
[root@agent httpd]# pwd
/root/httpd
[root@agent httpd]# cp /etc/httpd/conf/httpd.conf .
[root@agent httpd]# ls
httpd.conf
爲了演示效果將端口監聽在8080端口
Listen 8080
效果以下:

– name: conf
remote_user: root
hosts: taokey
tasks:
– name: install httpd
yum: name=httpd state=present
when: ansible_pkg_mgr == 「yum」
– name: conf file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
– name: start httpd
service: name=httpd enabled=yes state=started

handlers:
– name: restart httpd
service: name=httpd state=restarted
以上爲從檢測對端是否爲yum包管理器 若是是yum則安裝httpd 再cp配置文件到指定目錄
最後將服務加到開機啓動項並啓動,最後重啓服務。可是以上有幾個步驟重複了。

改進:
若是第二次執行的時候就沒有必要去從新yum安裝
在playbook中其中一個選項 -t
-t 表示能夠執行某些特定標籤對應的tasks
接下來爲每一個步驟都添加一個標籤,以下所示:
– name: conf file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
tags: conf
notify: restart httpd

執行:
[root@vmware1 ~]# ansible-playbook http.yaml -t conf

PLAY [conf] *******************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.1.82]

TASK: [conf file] *************************************************************
ok: [192.168.1.82]

PLAY RECAP ********************************************************************
192.168.1.82 : ok=2 changed=0 unreachable=0 failed=0

若是是多個任務,也能夠同時使用一個tags
都在每一個tasks 指定tags: conf 便可
經過目錄結構的方式也將其一組資源再次切分
以下所示:
直接使用inculde進行調用其餘的yaml文件
[root@vmware1 ~]# cat tasks/main.yaml
– name: install httpd
yum: name=httpd state=present
when: ansible_pkg_mgr == 「yum」

– name: conf file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
tags: conf
notify: restart httpd

– name: start httpd
service: name=httpd enabled=yes state=started

編輯httpd.yaml
[root@vmware1 ~]# cat httpd.yaml
– name: conf
remote_user: root
hosts: a
tasks:
include: tasks/main.yaml

定義handlers
[root@vmware1 ~]# mkdir handlers
[root@vmware1 ~]# cd handlers/
[root@vmware1 handlers]# vim main.yaml
– name: restart httpd
service: name=httpd state=restarted
經過以上的定義,再修改主配置文件:
[root@vmware1 ~]# cat httpd.yaml
– name: conf
remote_user: root
hosts: taokey
tasks:
include: tasks/main.yaml
handlers:
include: handlers/main.yaml

若是沒有問題則改一下httpd.conf配置文件並指定標籤
一個roles的案例以下所示:
site.yml
webservers.yml
fooservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
meta/
webservers/
files/
templates/
tasks/
handlers/
vars/
meta/

建立role的步驟
(1) 建立以roles命名的目錄;
(2) 在roles目錄中分別建立以各角色名稱命名的目錄,如webservers等;
(3) 在每一個角色命名的目錄中分別建立files、handlers、meta、tasks、templates和vars目錄;用不到的目錄能夠建立爲空目錄,也能夠不建立;
(4) 在playbook文件中,調用各角色;

rolse
再定義一個目錄rolse
[root@testA httpd]# mkdir roles
將目錄的文件都挪到roles裏
[root@testA httpd]# mv files/ handlers/ httpd.yaml tasks/ roles/web/
[root@testA httpd]# ls roles/
web

這裏的roles 表示角色目錄,而這個角色名就叫作web
而在這個角色當中,一般命名爲site.yaml
[root@testA web]# mv httpd.yaml site.yaml

定義一個yaml 用於調用site.yaml[root@testA httpd]# cat webserver.yaml– name: web serviceremote_user: roothosts: taokeyroles:– web

相關文章
相關標籤/搜索