ansible的playbook介紹和實戰

1、playbook 介紹:

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

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

2、playbook 的組成結構:

target section

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

variable section

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

task section

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

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]#

示例 二、

安裝 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]#

結果正確。


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