playbook

簡介:

        playbook是由一個或多個“play”組成的列表。play的主要功能是在於將事先歸併爲一組的主機裝扮成事先用過ansible中的task定義好的角色。從根本上來講,所謂task無非是調用ansible的一個module。將多個play組織在一個playbook中,即可以讓他們聯合起來按事先編排的機制完成某一任務。



target section可用配置項:


hosts:定義遠程主機組

user:執行改任務組的用戶

remote_user:與user相同

sudo:如果設置爲yes,執行改任務組的用戶在執行任務的時候,獲取root權限

sudo_user:如果你設置user爲tom,sudo爲yes,sudo_user爲jerry則tom用戶則會獲取jerry用戶的權限

connection:通過什麼方式連接到遠程主機,默認爲ssh

gather_facts:除非你明確說明不需要在遠程主機上執行setup模塊,否則默認會自動執行。如果你確實不需要setup模塊所傳遞過來的變量,你可以啓用該選項。


variable section(變量)

vars

vars_files

vars_prompt


task section

- name: install apache

  action: yum name=httpd state=installed  #第一種方法


- name: configure apache

  copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf    #第二種方法

 

- name: restart apache

  service:

       name: httpd 

       state: restarted      #第三種方法


########### target section ##############
---- hosts: webservers  
     remote_user: root
########### variable section ############ 
     vars:    
       http_port: 80    
       max_clients: 200
########### task section ################  
     tasks:  
     - name: ensure apache is at the latest version    
       yum: pkg=httpd state=latest  
     - name: write the apache config file    
       template: src=/srv/httpd.j2 dest=/etc/httpd.conf    
       notify:    
       - restart apache  
     - name: ensure apache is running    
       service: name=httpd state=started
###########################  
     handlers:    
       - name: restart apache      
         service: name=httpd state=restarted

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