简介:

        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