初始Ansible自动化运维工具之playbook剧本编写

一、playbook的相关知识

1.1 playbook 的简介 

playbook是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。我们完成一个任务,例如安装部署一个httpd服务,我们需要多个模块(一个模块也可以称之为task)提供功能来完成。而playbook就是组织多个task的容器,他的实质就是一个文件,有着特定的组织格式,它采用的语法格式是YAML(Yet Another Markup Language)。

1.2 playbook的 各部分组成

(1)Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
(2)Variables:变量
(3)Templates:模板
(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
(5)Roles:角色

二、基础的playbook剧本编写实例

playbook中运用的模块就是ansible中的模块,就像docker-compose一样将docker操作容器的指令归纳为一个yaml文件,开启运行yaml中的指令模块就能按照预设计的方向去完成。 

实例1:playbook编写apache的yum安装部署剧本

剧本编写实现的需求对Ansible管理的所有的webservers组的成员,yum安装最新版本的apache服务软件,并进行相应环境的调整,确保webservers的apache服务能够正常运行并设置开机自启 

cd /etc/ansible    #在ansible的所在目录中创建该项目的目录
mkdir apache
vim apache.yaml
---     #yaml文件以---开头,以表明这是一个yaml文件,可省略
- name: first play     #定义一个play的名称,可省略
  gather_facts: false    #设置不进行facts信息收集,这可以加快执行速度,可省略
  hosts: webservers    #指定要执行任务的被管理主机组,如多个主机组用冒号分隔
  remote_user: root    #指定被管理主机上执行任务的用户
  tasks:     #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
   - name: test connection    #自定义任务名称
     ping:     #使用 module: [options] 格式来定义一个任务
   - name: disable selinux
     command: '/sbin/setenforce 0'    #command模块和shell模块无需使用key=value格式
     ignore_errors: True     #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务
   - name: disable firewalld
     service: name=firewalld state=stopped    #使用 module: options 格式来定义任务,option使用key=value格式
   - name: install httpd
     yum: name=httpd state=latest
   - name: install configuration file for httpd
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf    #这里需要一个事先准备好的/opt/httpd.conf文件
     notify: "restart httpd"    #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作
   - name: start httpd service
     service: enabled=true name=httpd state=started
  handlers:     #handlers中定义的就是任务,此处handlers中的任务使用的是service模块
   - name: restart httpd    #notify和handlers中任务的名称必须一致
     service: name=httpd state=restarted
##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。

运行剧本的方法: 

//运行playbook
ansible-playbook httpd.yaml
  
//补充参数:
-k(–ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户
ansible-playbook apache.yaml --syntax-check    #检查yaml文件的语法是否正确
ansible-playbook apache.yaml --list-task       #检查tasks任务
ansible-playbook apache.yaml --list-hosts      #检查生效的主机
ansible-playbook apache.yaml --start-at-task='install httpd'     #指定从某个task开始运行

 

 实例2:playbook编写apache的yum安装部署剧本

mkdir nginx

\vim nginx.yaml
---
- name: first play for install nginx   #设置play的名称
  gather_facts: false                  #设置不收集facts信息
  hosts: webservers:dbservers          #指定执行此play的远程主机组
  remote_user: root                    #指定执行此play的用户
  tasks:                               #指定此play的任务列表
  - name: disabled firewalld
    service: name=firewalld  state=stopped  enabled=no
  - name: disable selinux
    command: '/sbin/setenforce 0'
    ignore_errors: yes
  - name: disabled selinux forever
    replace: path=/etc/selinux/config  regexp=enforcing  replace=disabled  after=loaded
  - name: copy nginx yum repo file
    copy: src=/etc/yum.repos.d/nginx.repo  dest=/etc/yum.repos.d/nginx.repo
  - name: install nginx by yum
    yum: name=nginx state=latest
  - name: start nginx service
    systemd: name=nginx state=started enabled=yes

运行结果: 

 

实例3:playbook编写nginx的本地安装部署剧本 

vim nginx-local.yaml

---
- name: second play for install httpd
  gather_facts: false
  hosts: webservers
  remote_user: root
  tasks:
#关闭防火墙
  - name: disabled firewalld
    service: name=firewalld  state=stopped  enabled=no

#关闭 selinux
  - name: disable selinux
    command: '/sbin/setenforce 0'
    ignore_errors: yes
  - name: disabled selinux forever
    replace: path=/etc/selinux/config  regexp=enforcing  replace=disabled  after=loaded

#关闭和删除nginx 
  #- name: disabled nginx
  #  service: name=nginx  state=stopped  enabled=no
  - name: remove nginx
    yum: name=nginx state=absent

#准备本地yum仓库,安装httpd
  - name: archive yum repos
    archive: path=/etc/yum.repos.d/*.repo dest=/etc/yum.repos.d/repo_bak.tar.gz format=gz remove=yes
  - name: copy local yum repo file
    copy: src=/etc/yum.repos.d/repo.bak/local.repo dest=/etc/yum.repos.d/local.repo
  - name: mount cdrom
    mount: src=/dev/sr0  path=/mnt  fstype=iso9660 state=mounted
  - name: install httpd by local yum
    yum: name=httpd state=latest

#使用本地准备的配置文件启动httpd服务
  - name: copy httpd config file
    copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: "reload httpd"
  - name: start httpd
    service: name=httpd state=started enabled=yes

#设置notify触发的任务
  handlers:
  - name: reload httpd
    service: name=httpd state=reloaded

 

运行结果:

 

 

相关推荐

  1. 自动化工具Ansibleplaybooks剧本

    2024-02-05 12:00:03       49 阅读
  2. 自动化工具----Ansible playbook详解

    2024-02-05 12:00:03       47 阅读
  3. Ansible自动化工具---Playbook

    2024-02-05 12:00:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-05 12:00:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-05 12:00:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-05 12:00:03       82 阅读
  4. Python语言-面向对象

    2024-02-05 12:00:03       91 阅读

热门阅读

  1. RabbitMQ集群搭建

    2024-02-05 12:00:03       48 阅读
  2. Linux下对线程的理解(上)

    2024-02-05 12:00:03       52 阅读
  3. 深度学习环境指南【1】:Nvidia 驱动

    2024-02-05 12:00:03       53 阅读
  4. 可控概率抽奖算法

    2024-02-05 12:00:03       53 阅读
  5. PostgreSQL解决序列(自增id)自动增长冲突

    2024-02-05 12:00:03       50 阅读
  6. Python 处理小样本数据的文档分类问题

    2024-02-05 12:00:03       52 阅读
  7. 字符串工具类

    2024-02-05 12:00:03       40 阅读
  8. Chapter 8 - 6. Congestion Management in TCP Storage Networks

    2024-02-05 12:00:03       59 阅读
  9. 算法提升——LeetCode383场周赛总结

    2024-02-05 12:00:03       56 阅读