Ansible(3)—— Playbook语法的使用

本实验是基于前篇《Ansible(2)—— Ansible-doc的用法以及一些常用模块的使用》实验环境下进行的,其中普通用户devops已经创建好,授权、免密都配置好了。

上一个实验的连接:https://mp.csdn.net/console/editor/html/104800208

目录

一、palybook简介

  • 1、playbook是什么?
  • 2、playbook的核心元素
  • 3、playbook语法

二、Playbook编写实例

(一)、远程安装apache并启动httpd服务

  • 1、编写playbook
  • 2、测试

(二)、创建httpd服务的默认发布信息和发布路径

  • 1、编辑playbook.yml文件
  • 2、编译playbook文件 、测试

(三)、httpd服务端口的修改、用触发器控制重启

  • 1、编辑 playbook.yml 文件
  • 2、修改当前目录下httpd.conf服务中的端口号
  • 3、测试

(四)、添加防火墙模块并配置httpd相关规则

  • 1、在server2上清空防火墙的规则
  • 2、编辑playbook.yml 文件
  • 3、测试:

(五)、多个任务的创建 多加play

  • 1、创建一个检测模板
  • 2、编译文件
  • 3、测试:

(六)、标签的定义

  • 1、标签命令的了解
  • 2、编辑playbook文件相对指定的 模块添加标签
  • 3、测试:

一、palybook简介

  • 1、playbook是什么?
  • 2、playbook的核心元素
  • 3、playbook语法

1、playbook是什么?

  • palybook与ad-hoc相比,是一种完全不同都应用。
  • playbook 是一种简单的配置管理系统与系统多多机器部署的基础,且非常适合于复杂的应用的部署
  • palybook中可以编排由序的执行过程,甚至于做到在多组机器间。来回有序的执行特别指定的步骤,并且可以同步或者异步的发起任务使用playbook可以方便的重用这些代码,可以移种到不同的机器上面。
  • playbook才是ansible真正强大指出
  • playbook有YAML 语言编写,以下为playbook常用到的YAML格式
  • playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。值得注意的是playbook是通过YAML格式来进行描述定义的。

2、playbook的核心元素

  1. Tasks:任务,由模板定义的操作列表
  2. Variables:变量
  3. Templates:模板,即使用模板语法的文件
  4. Handlers:处理器 ,当某条件满足时,触发执行的操作
  5. Roles:角色

3、playbook语法

playbook由YMAL语言编写,以下为playbook常用到的YMAL格式:

  •     文件的第一行应该以"—"三个连字符开始,表明YMAL文件的开始。
  •     在同一行中,#之后的内容表示注释,类似于shell,python和ruby。
  •     大小写敏感
  •     使用缩进表示层级关系(只能空格不能使用tab),我一般使用两个空格分层。

YAML支持的数据结构:

    YMAL中的列表元素以"-"开头然后紧跟着一个空格,同一个列表中的元素应该保持相同的缩进
    例:

---
#一个水果列表
- Apple
- Orange
- Mango

  •     一个字典是由一个简单的键:值的形式组成(这个冒号后面必须是一个空格):

---

#一位职工的信息

name: Example Develope

job: Developer

skill: Elite

  •     字典也可以使用缩进形式来表示

---

#一位职工的信息

{name: Example Develope,job: Developer,skill: Elite}

二、Playbook编写实例

以在远程主机上部署httpd服务为例

(一)、远程安装apache并启动httpd服务

  • 1、编写playbook
  • 2、测试

(二)、创建httpd服务的默认发布信息和发布路径

  • 1、编辑playbook.yml文件
  • 2、编译playbook文件 、测试

(三)、httpd服务端口的修改、用触发器控制重启

  • 1、编辑 playbook.yml 文件
  • 2、修改当前目录下httpd.conf服务中的端口号
  • 3、测试

(四)、添加防火墙模块并配置httpd相关规则

  • 1、在server2上清空防火墙的规则
  • 2、编辑playbook.yml 文件
  • 3、测试:

(五)、多个任务的创建 多加play

  • 1、创建一个检测模板
  • 2、编译文件
  • 3、测试:

(六)、标签的定义

  • 1、标签命令的了解
  • 2、编辑playbook文件相对指定的 模块添加标签
  • 3、测试:

实验环境

虚拟机名称 ip 版本 角色
server1 172.25.6.1 7.5 ansible
server2 172.25.6.2 7.5 远程主机hosts
server3 172.25.6.3 7.5 远程主机hosts

(一)、远程安装apache并启动httpd服务

  • 1、编写playbook
  • 2、测试

在普通用户下:

[ansible@server1 ~]$ cat playbook.yml          ##配置playbook.yml文件

---
- hosts: test                                  ##host名称
  tasks:                                       ##任务
    - name: install apache                     ##任务名称
      yum:                                     ##下载软件
        name: httpd                            ##软件名称
        state: present                         ##要进行的动作
    - name: start apache                       ##任务的名称
      service:                                 ##服务
        name: httpd                            ##要进行操作的 services: ssh dhcpv6-client http
  ports: 8080/tcp 3306/tcp服务名称
        state: started                         ##要进行操作的动作
        enabled: yes                           ##服务的状态

1.2、语法检测:

[ansible@server1 ~]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml


[ansible@server1 ~]$ ansible-playbook playbook.yml --list-hosts

playbook: playbook.yml

  play #1 (test): test    TAGS: []
    pattern: [u'test']
    hosts (1):
      server2


1.3、预检测:

[ansible@server1 ~]$ ansible-playbook playbook.yml -C

1.4、进行playbook文件编译

[ansible@server1 ~]$ ansible-playbook playbook.yml     


2、测试

在server2上查看服务的安装

[root@server2 ~]# netstat -anltp      ##查看端口信息 

tcp6       0      0 :::80                   :::*                    LISTEN      3432/httpd

root@server2 ~]# systemctl status httpd    


(二)、创建httpd服务的默认发布信息和发布路径

  • 1、编辑playbook.yml文件
  • 2、编译playbook文件 、测试

1、编辑playbook.yml文件

添加默认发布目录模块


[ansi[ansible@server1 ~]$ ansible-playbook playbook.ymlble@server1 ~]$ vim playbook.yml
---
- hosts: test
  tasks:
    - name: install apache                 ##安装apache服务
      yum:
        name: httpd                        ##安装的软件名称httpd
        state: present                     ##运行的状态
    - name: start apache                   ##开启服务
      service:
        name: httpd                        ##开启服务的名称
        state: started                     ##要进行操作的动作
        enabled: yes                       ##开启自启的状态
    - name: create index.html                ##创建httpd服务默认的分布目录
      copy:
        content: "www.westos.org"            ##复制此内容到默认的分布目录下
        dest: /var/www/html/index.html       ##httpd服务的默认发布目录

2、测试:

2.1、编译playbook文件

[ansible@server1 ~]$ ansible-playbook playbook.yml

2.1.1、登录server2主机

[ansible@server1 ~]$ curl server2
www.westos.org

(三)、httpd服务端口的修改、用触发器控制重启

  • 1、编辑 playbook.yml 文件
  • 2、修改当前目录下httpd.conf服务中的端口号
  • 3、测试:

修改httpd服务的默认发布的端口号为8080 、用触发器控制重启

1、编辑 playbook.yml 文件

[ansible@server1 ~]$ vim playbook.yml


---
- hosts: test
  tasks:
    - name: install apache                 ##安装apache服务
      yum:
        name: httpd                        ##安装的软件名称httpd
        state: present                     ##运行的状态

    - name: config apache                  ##修改服务端口号模块
      copy:
        src: httpd.conf                      ##将当前目录下的httpd.conf文件复制到/etc/httpd/conf/httpd.conf
        dest:  /etc/httpd/conf/httpd.conf
        mode: 644                            ##给定目录的权限为644  777-022-111=644(超户权限)


      notify: restart apache                ################################# 一旦上边模块有变更就通知下边的触发器############################

    - name: start apache                     ##开启服务
      service:
        name: httpd                        ##开启服务的名称
        state: started                     ##要进行操作的动作
        enabled: yes                       ##开启自启的状态
    - name: create index.html                ##创建httpd服务默认的分布目录
      copy:
        content: "www.westos.org"            ##复制此内容到默认的分布目录下
        dest: /var/www/html/index.html       ##httpd服务的默认发布目录

###################################################
  handlers:                                  ##添加触发器
    - name: restart apache        
      service:
        name: httpd
        state: restarted      

######################################################

2、修改当前目录下httpd.conf服务中的端口号


tcp6       0      0 :::8080                 :::*                    LISTEN      6760/httpd
[ansible@server1 ~]$ vim httpd.conf
Listen 8080

[ansible@server1 ~]$ ansible-playbook playbook.yml

2.1、编译:

3、测试:

3.1、在server2上查看配置文件中的端口是否改变


[root@server2 ~]# vim /etc/httpd/conf/httpd.conf

Listen 8080

3.2、在server2上查看8080端口是否生效

[root@server2 ~]# systemctl restart httpd
[root@server2 ~]#
[root@server2 ~]# netstat -anltp

tcp6       0      0 :::8080                 :::*                    LISTEN      5502/httpd   

3.3、在server1上:

访问server2的8080端口
[ansible@server1 ~]$ curl server2:8080
www.westos.org

(四)、添加防火墙模块并配置httpd相关规则

  • 1、在server2上清空防火墙的规则
  • 2、编辑playbook.yml 文件
  • 3、测试:

1、在server2上清空防火墙的规则

  1. [root@server2 ~]# iptables -F           ##清空防火墙规则
  2. [root@server2 ~]# iptables -L             ##查看防火墙防火墙规则的命令

 

[root@server2 ~]# systemctl stop firewalld                                        ##关闭防火墙服务
[root@server2 ~]# systemctl disable firewalld                                   ##开机不自启动


2、编辑playbook.yml 文件

[ansible@server1 ~]$ vim playbook.yml
---
- hosts: test
  tasks:
    - name: install apache                 ##安装apache服务
      yum:
        name: httpd                        ##安装的软件名称httpd
        state: present                     ##运行的状态

    - name: config apache                  ##修改服务端口号模块
      copy:
        src: httpd.conf                      ##将当前目录下的httpd.conf文件复制到/etc/httpd/conf/httpd.conf
        dest:  /etc/httpd/conf/httpd.conf
        mode: 644                            ##给定目录的权限为644  


      notify: restart apache
    - name: start apache                     ##开启服务
      service:
        name: httpd                        ##开启服务的名称
        state: started                     ##要进行操作的动作
        enabled: yes                       ##开启自启的状态
    - name: create index.html                ##创建httpd服务默认的分布目录
      copy:
        content: "www.westos.org"            ##复制此内容到默认的分布目录下
        dest: /var/www/html/index.html       ##httpd服务的默认发布目录



############################## 加入的模块 #########################################

    - name: start firewalld                       ##开启防火墙规则
      service:
        name: firewalld                           ##开启服务的名称
        state: started                            ##动作
        enabled: yes                               ##开启自启动
    - name: custom firewalld                      ##设置防火墙的规则
      firewalld:
        service: http                             ##设置规则的名称
        permanent: yes                            ##永久允许http 访问防火墙
        immediate: yes                            ##立即生效
        state: enabled                            ##开机自启

###########################################################################


  handlers:                                       ##触发器
    - name: restart apache
      service:
        name: httpd
        state: restarted      




编译:

[ansible@server1 ~]$ ansible-playbook playbook.yml


3、测试:
在server2上

[root@server2 ~]# systemctl status firewalld

[root@server2 ~]# firewall-cmd --list-all

 services: ssh dhcpv6-client http
  ports: 8080/tcp 3306/tcp


(五)、多个任务的创建 多加play

  • 1、创建一个检测模板
  • 2、编译文件
  • 3、测试:

1、编译文件

[root@server1 ~]# ansible-doc uri          ##查看uri模块配置

[ansible@server1 ~]$ vim playbook.yml      ##编辑 playbook.yml文件 


---
- hosts: test
  tasks:
    - name: install apache                 ##安装apache服务
      yum:
        name: httpd                        ##安装的软件名称httpd
        state: present                     ##运行的状态

    - name: config apache                  ##修改服务端口号模块
      copy:
        src: httpd.conf                      ##将当前目录下的httpd.conf文件复制到/etc/httpd/conf/httpd.conf
        dest:  /etc/httpd/conf/httpd.conf
        mode: 644                            ##给定目录的权限为644  


      notify: restart apache
    - name: start apache                     ##开启服务
      service:
        name: httpd                        ##开启服务的名称
        state: started                     ##要进行操作的动作
        enabled: yes                       ##开启自启的状态
    - name: create index.html                ##创建httpd服务默认的分布目录
      copy:
        content: "www.westos.org"            ##复制此内容到默认的分布目录下
        dest: /var/www/html/index.html       ##httpd服务的默认发布目录

    - name: start firewalld                       ##开启防火墙规则
      service:
        name: firewalld                           ##开启服务的名称
        state: started                            ##动作
        enabled: yes                               ##开启自启动
    - name: custom firewalld                      ##设置防火墙的规则
      firewalld:
        service: http                             ##设置规则的名称
        permanent: yes                            ##永久允许http 访问防火墙
        immediate: yes                            ##立即生效
        state: enabled                            ##开机自启

  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted      


########################### 添加测试模块 ################
- hosts: localhost
  become: no                                      ##不改变当前环境变量         
  tasks:      
    - name: test apache                           ##服务器的名称
      uri:
        url: https://server2/index.html  
        return_content: yes                       ##返回内容

  • 查看文件中play个数
[ansible@server1 ~]$ ansible-playbook playbook.yml --list-hosts    ##查看文件中play的个数

  • 查看文件中任务数
[ansible@server1 ~]$ ansible-playbook playbook.yml --list-tasks

2、编译文件

[ansible@server1 ~]$ ansible-playbook playbook.yml

测试:

(六)、标签的定义

  • 1、标签命令的了解
  • 2、编辑playbook文件相对指定的 模块添加标签
  • 3、测试:

用标签来选择检测的哪个模块

1、标签的用法

1.1、查看标签的用法

[ansible@server1 ~]$ ansible-playbook --help        ##查看标签的用法 

1.2、指定从文件中的哪个任务为开始

[ansible@server1 ~]$ ansible-playbook playbook.yml --start-at-task "test apache"       ##从哪个任务开始

1.2、查看标签列表

[ansible@server1 ~]$ ansible-playbook playbook.yml --list-tasks                        ##查看变迁列表

2、编辑playbook文件相对指定的 模块添加标签

[ansible@server1 ~]$ vim playbook.yml
  tags: test                                      ##添加标签


3、测试:
3.1编译

[ansible@server1 ~]$ ansible-playbook playbook.yml -t test        ##通过标签来选择要执行的模块

指定编译对应标签所在的模块

发布了93 篇原创文章 · 获赞 1 · 访问量 1915

猜你喜欢

转载自blog.csdn.net/dghfttgv/article/details/104803816