自动化运维实践 | Ansible playbook重用

时间:2022-07-23
本文章向大家介绍自动化运维实践 | Ansible playbook重用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

今天我们讲讲如何重用ansible的playbook。

playbook支持两种重用机制,一种是重用静态的单个playbook脚本,另外一种是重用实现特定功能的文件夹,类似于python等编程语言中的包。

  • include语句:重用单个playbook脚本,使用起来简单、直接
  • role语句:重用实现特定功能的playbook文件夹,使用方法稍复杂,功能强大。Ansible 还为role创建了一个共享平台AnsibleGalaxy,role是Ansible最为推荐的重用和分享Playbook的方式。

Include语句

include语句是最基本的Ansible代码重用机制,主要重用任务,同时,include还可将任务分割成多个文件,避免playbook过于臃肿,使用户更关注于整体的架构,而不是实现的细节上。

  • 基本的include用法

与其他语言的include一样,直接include即可

tasks:
    - include: tasks/add_firewalld_rule.yml
  • 在include语句中使用参数

这里有两个知识点,一个是如何在被include的yml文件中定义参数,而是如何向include文件中传入参数。

1)被include的add_firewalld_rule.yml文件定义如下:

---
  - name: insert the firewalld rule
    firewalld: port={{ port }}/tcp permanent=true state=enabled immediate=yes

2)使用include, 并在使用时像include的文件中传入参数

tasks:
    - include tasks/add_firewalld_rule.yml port=80
    - include tasks/add_firewalld_rule.yml port=3260
    - include tasks/add_firewalld_rule.yml port=8080

或者直接在playbook中定义参数,就不需要传入值了

vars:
    port: 3260
remote_user: root
tasks:
   - include tasks/add_firewalld_rule.yml

Role - Playbook中的"Package"

role比include有更强大的代码重用和分享机制。include类似于编程语言中的include,是重用单个文件的,重用的功能有限。

而role类似于编程语言中的"Package",可以重用一组文件,形成完整的功能。

  • roles说明

roles意为角色,主要用于封装playbook实现复用性。在ansible中,roles通过文件的组织结构来展现。

首先需要有一个roles目录。同时,在roles目录所在目录中,还要有一个playbook文件,以下示例为nginx.yml,nginx.yml文件是ansible-playbook需要执行的文件,在此文件中定义了角色,当执行到角色时,将会到roles中对应的角色目录中寻找相关文件。

roles目录中的子目录是即是各个role。例如,此处只有一个名为halo的role,在role目录中,有几个固定名称的目录(如果没有则忽略)。在这些目录中,还要有一些固定名称的文件,除了固定名称的文件,其他的文件可以随意命名。以下是各个目录的含义:

  • tasks目录:存放task列表。若role要生效,此目录必须要有一个主task文件main.yml,在main.yml中可以使用include包含同目录(即tasks)中的其他文件。
  • handlers目录:存放handlers的目录,若要生效,则文件必须名为main.yml文件。
  • files目录:在task中执行copy或script模块时,如果使用的是相对路径,则会到此目录中寻找对应的文件。
  • templates目录:在task中执行template模块时,如果使用的是相对路径,则会到此目录中寻找对应的模块文件。
  • vars目录:定义专属于该role的变量,如果要有var文件,则必须为main.yml文件。
  • defaults目录:定义角色默认变量,角色默认变量的优先级最低,会被任意其他层次的同名变量覆盖。如果要有var文件,则必须为main.yml文件。
  • meta目录:用于定义角色依赖,如果要有角色依赖关系,则文件必须为main.yml。

所以,相对完整的role的文件组织结构如下图。

├── roles
│   ├── halo
│   │   ├── defaults
│   │   ├── files
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── meta
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   │   └── main.yml
└── site.yml

举例:

使用role的方式来安装halo,相对来说,结构会比较清晰明了。还是使用ansible去自动部署halo博客系统。功能很简单,就是在一台机器上面自动部署java+halo+nginx服务。目录如下:

├── roles
│   ├── halo
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   ├── application.yaml
│   │   │   └── halo.service
│   │   └── vars
│   │   └── main.yml
│   ├── java
│   │   └── tasks
│   │   └── main.yml
│   └── nginx
│   ├── meta
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   └── templates
│   └── halo.conf
└── site.yml
主要分为3个role,由site.yml引入,内容如下:
# cat site.yml
---
- hosts: 192.168.1.61
  remote_user: root
  strategy: free
  pre_tasks:
    - name: config nginx repo for centos 
      yum_repository:
        name: nginx
        description: nginx
        baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
        gpgcheck: no
      when: ansible_distribution_major_version == "7"
    - name: Disable SELinux
      selinux: state=disabled

  roles:
    - nginx

  post_tasks:
    - shell: echo 'Deplay halo finished.'
      register: ret
    - debug: var=ret.stdout
pre_tasks为运行play之前的操作,post_tasks为运行完play之后的操作。主要是看nginx这个role的内容:
[root@localhost nginx]# cat tasks/main.yml
---
  - name: make sure nginx state is installed
    yum: name=nginx state=installed
  - name: copy halo to nginx config file
    template: src=halo.conf dest="/etc/nginx/conf.d/halo.conf"
  - name: make sure nginx service is running
    service: name=nginx state=started
  - name: make sure port is open
    wait_for: port="{{ nginx_port }}"

[root@localhost nginx]# cat meta/main.yml
---
  dependencies:
    - role: java
    - role: halo

[root@localhost nginx]# cat templates/halo.conf
server {
    listen ;

    server_name {{ halo_domain }};

    client_max_body_size m;

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://127.0..:{{halo_port}}/;
    }
}
meta/main.yml为role的依赖关系,要先运行这里面的内容才会运行自己的nginx这个role。

java这个role很简单,只需要安装jdk即可。

[root@localhost java]# cat tasks/main.yml
---
  - name: install java
    yum: name=java-1.8.0-openjdk state=installed

而halo的内容如下:

[root@localhost halo]# cat tasks/main.yml
---
  - name: get halo
    get_url: url=http://halo.ryanc.cc/release/halo-latest.jar dest={{ halopath }}

  - name: add halo service file
    template: src=halo.service dest=/etc/systemd/system/halo.service

  - name: touch ~/.halo directory
    file: path=~/.halo state=directory

  - name: copy halo config file
    template: src=application.yaml dest="~/.halo/application.yaml"

  - name: restart halo
    systemd:
      daemon_reload: yes
      name: halo
      state: started
      enabled: yes

  - name: wait to start halo
    wait_for: port={{ halo_port }}

[root@localhost halo]# cat vars/main.yml
---
  memory: m
  halo_port: 
  halopath: /root/halo.jar

今天我们先介绍到这里,未完待续!

参考资料:

Ansible快速入门, 技术原理与实战。

https://www.wumingx.com/linux/ansible-roles.html