Ansible Playbook用法

  • 1.什么是playbook?
  • 2.playbook与临时命令的区别
  • 3.playbook的写法和格式
  • 4.多任务执行playbook


1.什么是playbook?

首先简单说明一下playbook,playbook是什么呢?根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等等,在shell脚本中是一条一条的命令,而在playbook中是一个一个的task任务构成,每个task任务可以看做shell中的一条命令;shell脚本一般只是在当前服务器上执行,而playbook则是在不止一个服务器上执行,因此playbook需要在其中指定运行该playbook的服务器名。

2.playbook与临时命令的区别

临时命令可以作为一次性命令对一组目标主机运行一项简单的任务。但是在任务很多的情况下就显得比较繁忙,这时候就需要playbook了,playbook可以轻松的解决任务繁琐的场景。

下面我将简单示范同一个需求在playbook和临时命令会分别如何应用:

  • 在受控机上用临时命令创建一个pyd用户,指定uid为2021
[root@master project]# ansible 192.168.8.132 -m user -a 'name=pyd uid=2021 state=present'

//验证效果
[root@master project]# ansible 192.168.8.132 -m shell -a 'id pyd'
192.168.8.132 | CHANGED | rc=0 >>
uid=2021(pyd) gid=2021(pyd) 组=2021(pyd)
[root@master project]#
  • 用playbook创建pyd用户并指定uid为2021
[root@master project]# mkdir playbook   //创建一个playbook目录
[root@master project]# vim playbook/user.yml   创建一个文件以yml结尾
[root@master project]# cat playbook/user.yml 
---
- name: user
  hosts: 192.168.8.132
  tasks:
    - name: create user for pyd
      user:
        name: pyd
        uid: 2021
        state: present

[root@master project]# ansible 192.168.8.132 -m user -a 'name=pyd state=absent remove=yes'    //这里为了验证效果删除刚刚创建的用户
192.168.8.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "pyd",
    "remove": true,
    "state": "absent"
}
[root@master project]# ansible-playbook playbook/user.yml    //运行playbook

PLAY [user] ***************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.132]

TASK [create user for pyd] ************************************************************************************************
changed: [192.168.8.132]

PLAY RECAP ****************************************************************************************************************
192.168.8.132              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@master project]# 

[root@master project]# ansible 192.168.8.132 -m shell -a 'id pyd'
192.168.8.132 | CHANGED | rc=0 >>
uid=2021(pyd) gid=2021(pyd) 组=2021(pyd)
[root@master project]#

3.playbook的写法和格式

Playbook是以YAML格式编写的文本文件,通常以.yml结尾。Playbook使用空格字符缩进来表示其数据结构。YAML对用于缩进的空格数量没有严格的要求,但有两个基本的规则:

  • 处于层次结构中同一级别的数据元素(例如同一列表中的项目)必须具有相同的缩进量。
  • 如果项目属于其他项目的子项,其缩进量必须大于父项

playbook的开头一行由三个—(—)来进行分隔,这是文档开头的标记。其中的每个项目用一个—(-)来开头,所以playbook编写的格式如下:

---
- name1: name
  hosts: 1.1.1.1
  tasks: 
    - tasks1
    - tasks2
- name2
  hosts: 2.2.2.2
  tasks: 
    - tasks3
    - tasks4
- name3
  hosts: 3.3.3.3
  tasks: 
    - tasks5
    - tasks6
  • 案列
[root@master project]# vim playbook/httpd.yml 
[root@master project]# cat playbook/httpd.yml 
---
- name: setup httpd with enabled
  hosts: 192.168.8.132
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: latest

    - name: service is enabled
      service:
         name: httpd
         state: started
         enabled: yes
     
[root@master project]# ansible-playbook playbook/httpd.yml 

PLAY [setup httpd with enabled] *******************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.132]

TASK [install httpd] ******************************************************************************************************
changed: [192.168.8.132]

TASK [service is enabled] *************************************************************************************************
changed: [192.168.8.132]

PLAY RECAP ****************************************************************************************************************
192.168.8.132              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@master project]# ansible 192.168.8.132 -m shell -a 'systemctl status httpd'
192.168.8.132 | CHANGED | rc=0 >>
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-07-20 19:49:55 CST; 21s ago
     Docs: man:httpd.service(8)
 Main PID: 40756 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 23648)
   Memory: 37.7M
   CGroup: /system.slice/httpd.service
           ├─40756 /usr/sbin/httpd -DFOREGROUND
           ├─40757 /usr/sbin/httpd -DFOREGROUND
           ├─40758 /usr/sbin/httpd -DFOREGROUND
           ├─40759 /usr/sbin/httpd -DFOREGROUND
           └─40760 /usr/sbin/httpd -DFOREGROUND

7月 20 19:49:55 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
7月 20 19:49:55 localhost.localdomain httpd[40756]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
7月 20 19:49:55 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
7月 20 19:49:55 localhost.localdomain httpd[40756]: Server configured, listening on: port 80
[root@master project]#

playbook中play和任务列出的顺序很重要,因为Ansible会按照相同的顺序运行它们

提高输出详细程度

ansible-playbook命令提供的默认输出不提供详细的任务执行信息。ansible-playbook -v命令提供了额外的信息,总共有四个级别。
配置Playbook执行的输出详细程序

选项

描述

-v

显示任务结果

-vv

任务结果和任务配置都会显示

-vvv

包含关于与受管主机连接的信息

-vvvv

增加了连接插件相关的额外详细程序选项,包括受管主机上用于执行脚本的用户以及所执行的脚本

语法验证
在执行playbook之前,最好要进行验证,确保其内容的语法正确无误。ansible-playbook命令提供了一个–syntax-check选项,可用于验证playbook的语法。

[root@master project]# ansible-playbook --syntax-check playbook/httpd.yml

playbook: playbook/httpd.yml    //出现这个即是成功
[root@master project]#

失败

[root@master project]# ansible-playbook playbook/user.yml 
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/opt/project/playbook/user.yml': line 8, column 12, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

       name: pyd
        uid: 2021
           ^ here
[root@master project]#

4.多任务执行playbook

  • 利用playbook下载httpd和mysql服务并且开机自启
[root@master project]# cat playbook/httpdmysql.yml
---                            //文件开头
- name: httpd                  //项目的名字
  hosts: 192.168.8.132         //执行任务的主机ip/组
  tasks:                       //任务列表
    - name: install httpd      //任务描述
      dnf:                     //使用dnf模块
        name: httpd            //下载httpd服务
        state: latest          //状态为存在
                                           
    - name: httpd 
      service:
        name: httpd
        state: started
        enabled: yes
                                                                               
- name: mysql
  hosts: 192.168.8.133
  tasks:
    - name: install mariadb*
      yum:
        name: mariadb*
        state: latest

    - name: mariadb
      service:
        name: mariadb
        state: started
        enabled: yes
                                                                                        
[root@master project]# 

[root@master project]# ansible-playbook playbook/httpdmysql.yml 

PLAY [httpd] **************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.132]

TASK [install httpd] ******************************************************************************************************
changed: [192.168.8.132]

TASK [httpd] **************************************************************************************************************
changed: [192.168.8.132]

PLAY [mysql] **************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [192.168.8.133]

TASK [install mariadb*] ***************************************************************************************************
changed: [192.168.8.133]

TASK [mariadb] ************************************************************************************************************
changed: [192.168.8.133]

PLAY RECAP ****************************************************************************************************************
192.168.8.132              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.8.133              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0