自动化运维ansible一playbook介绍与使用

1.什么是playbook?

  • playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为
    一组的主机装扮成事先通过ansible的task定义好的角色,从根本上讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可让他们联合起
  • Playbooks 的格式是YAML(详见:YAML 语法),语法做到最小化,意在避免 playbooks
    成为一种编程语言或是脚本,但它也并不是一个配置模型或过程的模型。 赖按事先编排的机制完成某一任务。

2.playbook组成

主要有四部分

  • target sction:定义将要执行playbook的远程主机组
  • variable sction:定义playbook运行时需要使用的变量
  • task sction:定义将要在远程主机上执行的任务列表
  • handler section:定义task执行完成以后需要调用的任务

3.拿一个playbook进行说明解释

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: write the apache config file
    template: src=/etc/httpd.conf 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
格式:详情见yaml语法,不用了解太多。
hosts: 定义远程主机组
remote_user:执行该任务组的用户
vars:定义变量,http_port=80,max_clients=20
tasks(重点):
    name:描述执行任务的说明
    yum:ansible的模块,意思是安装httpd 服务
    template:模块,类似copy,将本地的http.conf文件复制到远程主机。
    notify: 完成tasks后要做的事,安装完httpd,是不是该启动服务了
     - restart apache #这里是调用handler的
handlers:(主要是给notify调用)
    name:重启Apache 
    service:模块,意思是重启httpd服务

4.编写一个安装httpd服务的playbook

(和上面的大致相同,意思是安装httpd服务,将本地文件复制到远程主机,httpd已经被修改监听8080端口,重启服务)
[root@liang ansible]# cat httpd.yml 
---
- hosts: all
  tasks:
    - name: install httpd service
      yum: name=httpd state=installed
    - name: Modify the port 8080
      template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify:
      - restart httpd
  handlers:
    - name: restart httpd
      service: name=httpd state=restarted
    - name: stop http
      service: name=httpd state=stoped

5.运行playbook

[root@liang ansible]# ansible-playbook httpd.yml 

PLAY [all] *********************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.130]
ok: [10.0.0.131]

TASK [install httpd service] ***************************************************************
changed: [10.0.0.131]
changed: [10.0.0.130]

TASK [Modify the port 8080] ****************************************************************
changed: [10.0.0.130]
changed: [10.0.0.131]

RUNNING HANDLER [restart httpd] ************************************************************
changed: [10.0.0.130]
changed: [10.0.0.131]

PLAY RECAP *********************************************************************************
10.0.0.130                 : ok=4    changed=3    unreachable=0    failed=0   
10.0.0.131                 : ok=4    changed=3    unreachable=0    failed=0   

5.1查看httpd服务

[root@liang ansible]# ansible all -m shell -a "netstat -lntup|grep httpd"
10.0.0.131 | SUCCESS | rc=0 >>
tcp        0      0 :::8080                     :::*                        LISTEN      1577/httpd          

10.0.0.130 | SUCCESS | rc=0 >>
tcp        0      0 :::8080                     :::*                        LISTEN      2175/httpd    

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81224954