ansible的playbook介紹和實戰

一、playbook 介紹:
node

簡單的說就是定義一個配置文件,文件中寫入你須要安裝的服務,配置文件,變量等信息,使他們能夠按照事先定義好的機制完成一個任務。shell

Playbook使用YAML語法結構,因此配置閱讀起來都比較簡單。apache

二、playbook 的組成結構:
bash

target sectionssh

定義將要執行playbook的遠程主機組
tcp

variable sectionide

定義playbook運行時須要使用的變量
spa

task sectionrest

定義將要在遠程主機上執行的任務列表
server

handler section

定義task執行完成之後須要調用的任務

Target section經常使用參數

  1. hosts:定義遠程主機組

  2. remote_user:執行該任務的用戶

  3. sudo: 設置爲yes的時候,執行任務的時候使用root權限

  4. sudo_user 若是你設置用戶爲 lansgg ,那麼你執行的時候會使用 lansgg 用戶的權限

  5. connection 經過什麼方式鏈接到遠程主機,默認是ssh

  6. gather_facks 是否啓用在遠程主機執行setup模塊,默認是會執行的,可用同setup模塊獲取遠程主機的信息,在定義變量的時候使用



Variabler section經常使用參數

  1. vars  定義格式 變量名:變量值

  2. vars_files  指定變量文件

  3. vars_prompt  用戶交互模式自定義變量

  4. setup 模塊去遠程主機的值


Task section

  1. name:輸出到屏幕的信息

  2. action:定義執行的動做調用ansible的模塊例如:yum name=http state=installed就是安裝apache服務

  3. copy:複製本地文件到遠程主機

  4. template:複製本地文件到遠程主機可是他能夠在本地文件中調用變量

  5. service :定義服務的狀態


handler section


能夠理解爲處理器,已經爲 task section 進行調用,爲任務列表操做完畢後的後續動做當關注的資源發生變化時執行的操做


playbook 示例一:

編寫一個 playbook 劇本文件,安裝 httpd 服務,並將本地準備好的配置文件 copy 過去某一個位置,這裏示例爲 /tmp 下

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  remote_user: root
  tasks:
  - name: instal httpd service
    yum: name=httpd state=present
  - name: copy httpd conf
    copy: src=/etc/httpd/conf/httpd.conf dest=/tmp/httpd.conf
[root@node1 ansible]#


開始執行:

[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [instal httpd service] ************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

TASK: [copy httpd conf] ******************************************************* 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=3    changed=2    unreachable=0    failed=0   
192.168.100.132            : ok=3    changed=2    unreachable=0    failed=0   

[root@node1 ansible]# ansible testservers -m shell -a 'ls -l /tmp/httpd*'
192.168.100.132 | success | rc=0 >>
-rw-r--r-- 1 root root 34421 Mar  1 12:17 /tmp/httpd.conf

192.168.100.131 | success | rc=0 >>
-rw-r--r-- 1 root root 34421 Mar  1 12:18 /tmp/httpd.conf

[root@node1 ansible]#

示例 2、

安裝 httpd 服務,將本地準備好的配置文件 copy 過去,而且啓動服務

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  remote_user: root
  tasks:
  - name: instal httpd service
    yum: name=httpd state=present
  - name: copy httpd conf
    copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: start httpd service
    service: name=httpd state=started
    
[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [instal httpd service] ************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

TASK: [copy httpd conf] ******************************************************* 
changed: [192.168.100.132]
changed: [192.168.100.131]

TASK: [start httpd service] *************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=4    changed=3    unreachable=0    failed=0   
192.168.100.132            : ok=4    changed=3    unreachable=0    failed=0   

[root@node1 ansible]# ansible testservers -m shell -a 'netstat -naptl |grep 8080'
192.168.100.131 | success | rc=0 >>
tcp        0      0 :::8080                     :::*                        LISTEN      4018/httpd          

192.168.100.132 | success | rc=0 >>
tcp        0      0 :::8080                     :::*                        LISTEN      35438/httpd         

[root@node1 ansible]#

示例 三 :

咱們將 httpd.conf 監聽的端口改成 8090 ,而後從新覆蓋配置文件,當這個配置文件發生改變時,就觸發 handler 進行服務重啓


notify 這個 action可用於在每一個play的最後被觸發,這樣能夠避免屢次有改變發生時每次都執行指定的操做,notify中列出的操做稱爲handler,

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  remote_user: root
  tasks:
  - name: instal httpd service
    yum: name=httpd state=present
  - name: copy httpd conf
    copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: 
      - restart httpd service
  - name: start httpd service
    service: name=httpd state=started enabled=true
  handlers:
    - name: restart httpd service
      service: name=httpd state=restarted
      
[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [instal httpd service] ************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [copy httpd conf] ******************************************************* 
changed: [192.168.100.132]
changed: [192.168.100.131]

TASK: [start httpd service] *************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

NOTIFIED: [restart httpd service] ********************************************* 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=5    changed=3    unreachable=0    failed=0   
192.168.100.132            : ok=5    changed=3    unreachable=0    failed=0   

[root@node1 ansible]# ansible testservers -m shell -a 'netstat -nltp |grep 8090'
192.168.100.131 | success | rc=0 >>
tcp        0      0 :::8090                     :::*                        LISTEN      4216/httpd          

192.168.100.132 | success | rc=0 >>
tcp        0      0 :::8090                     :::*                        LISTEN      36215/httpd         

[root@node1 ansible]#


示例 四:

帶有 vars 變量

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  vars:
    src_http_dir: "/etc/httpd"
    dest_http_dir: "/tmp"
  remote_user: root
  tasks:
  - name: copy httpd conf
    copy: src="`src_http_dir`/conf/httpd.conf" dest="`dest_http_dir`/http.conf.ansible"
    
[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [copy httpd conf] ******************************************************* 
changed: [192.168.100.131]
changed: [192.168.100.132]

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

[root@node1 ansible]# ansible testservers -m shell -a 'ls -l /tmp/http*'
192.168.100.131 | success | rc=0 >>
-rw-r--r-- 1 root root 34421 Mar  1 13:25 /tmp/http.conf.ansible

192.168.100.132 | success | rc=0 >>
-rw-r--r-- 1 root root 34421 Mar  1 13:25 /tmp/http.conf.ansible

[root@node1 ansible]#


示例 五 :

結合 template 模板,從 setup 模塊中獲取 變量,替換到模板文件中,咱們的模塊文件中有兩項使用了 setup 中的 facts ,還使用了 vars 設定的變量 分別是ServerName 和 Listen

[root@node1 ansible]# pwd
/etc/ansible
[root@node1 ansible]# grep Listen httpd.conf  |grep -v ^#
Listen `ansible_all_ipv4_addresses`.`0`:`http_port`
[root@node1 ansible]# grep ServerName httpd.conf  |grep -v ^#
ServerName `ansible_nodename`
[root@node1 ansible]#

咱們的 yaml 文件

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  vars:
    http_port: 8010
    http_dir: /etc/httpd/conf
  remote_user: root
  tasks:
  - name: copy httpd conf
    template: src=/etc/ansible/httpd.conf dest="`http_dir`/httpd.conf"
    notify:
     - restart httpd service
  handlers:
   - name: restart httpd service
     service: name=httpd state=restarted
[root@node1 ansible]#

執行 playbook

[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [copy httpd conf] ******************************************************* 
changed: [192.168.100.132]
changed: [192.168.100.131]

NOTIFIED: [restart httpd service] ********************************************* 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=3    changed=2    unreachable=0    failed=0   
192.168.100.132            : ok=3    changed=2    unreachable=0    failed=0   

[root@node1 ansible]#

查看遠程主機的配置文件及監聽端口

[root@node1 ansible]# ansible testservers -m shell -a 'netstat -natpl |grep httpd'
192.168.100.131 | success | rc=0 >>
tcp        0      0 192.168.100.131:8010        0.0.0.0:*                   LISTEN      5777/httpd          

192.168.100.132 | success | rc=0 >>
tcp        0      0 192.168.100.132:8010        0.0.0.0:*                   LISTEN      40652/httpd         

[root@node1 ansible]# ansible testservers -m shell -a ' grep ServerName /etc/httpd/conf/httpd.conf |grep -v ^#'
192.168.100.132 | success | rc=0 >>
ServerName v3.lansgg.com

192.168.100.131 | success | rc=0 >>
ServerName v2.lansgg.com

[root@node1 ansible]# ansible testservers -m shell -a 'grep Listen /etc/httpd/conf/httpd.conf |grep -v ^#'
192.168.100.132 | success | rc=0 >>
Listen 192.168.100.132:8010

192.168.100.131 | success | rc=0 >>
Listen 192.168.100.131:8010

[root@node1 ansible]#

結果正確。

相關文章
相關標籤/搜索