Ansible中的角色使用

目录

[ansible roles](#ansible roles)

[ansible 角色简介](#ansible 角色简介)

roles目录结构

role存放的路径在配置文件ansible.cfg中定义

创建目录结构

控制任务执行顺序

ansible-galaxy命令工具

安装选择的角色

示例


ansible roles

ansible 角色简介

Ansible roles 是为了层次化,结构化的组织Playbook

roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们

roles一般用于基于主机构建服务的场景中,在企业复杂业务场景中应用的频率很高

以特定的层级目录结构进行组织的 tasks 、 variables 、 handlers 、 templates 、 files 等 ; 相当于函数的调用把各个功能切割成片段来执行。

roles目录结构

复制代码
files             //存放copy或script等模块调用的函数
tasks             //定义各种task,要有main.yml,其他文件include包含调用
handlers         //定义各种handlers,要有main.yml,其他文件include包含调用
vars             //定义variables,要有main.yml,其他文件include包含调用
templates             //存储由template模块调用的模板文本
meta                 //定义当前角色的特殊设定及其依赖关系,要有main.yml的文件 defaults##要有main.yml的文件,用于设定默认变量
tests                 //用于测试角色

role存放的路径在配置文件ansible.cfg中定义

roles_path = path / roles ( 默认目录 : / etc / ansible / roles )

创建目录结构

复制代码
ansible-galaxy init httpd             //创建目录结构
ansible-galaxy list            //显示当前 Ansible Galaxy 环境中安装的角色列表

控制任务执行顺序

复制代码
hosts: server2
 roles:
 - role: role1         //角色任务
 pre_tasks:             //角色执行前执行的play
 - tasks1
 tasks:                 //普通任务
 - tasks2
 post_tasks:             //在角色和普通任务执行完毕后执行的play
 - tasks3

ansible-galaxy命令工具

Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站 , 可以帮助我们更好的定义和学习 roles
ansible - galaxy 命令默认与 https :// galaxy.ansible.com 网站 API 通信 , 可以查找 、 下载各种社区开发的 Ansible 角色
galaxy.ansible.com 网站查询 roles

安装选择的角色

复制代码
install https://galaxy.ansible.com roles
ansible-galaxy install geerlingguy.nginx
install local roles
 vim install_apache_role.yml
---
- src: file:///mnt/apache.tar.gz
 name: apache
ansible-galaxy install -r install_apache_role.yml

示例

下载安装apache,根据变量更改配置文件,并在访问login.westos.org时需要用户认证

复制代码
ansible-galaxy init httpd        //创建目录结构

生成认证文件

复制代码
 htpasswd -cm htpasswd admin

tasks主任务

复制代码
[devops@ansible1 .ansible]$ cat roles/httpd/tasks/main.yml
---
# tasks file for httpd
- name: install httpd
  yum:
    name: httpd

- name: create vhosts configure
  template:
    src: vhosts.conf.j2
    dest: /etc/httpd/conf.d/vhosts.conf
  notify: restarted httpd

- name: create auth file
  copy:
    src: htpasswd
    dest: /etc/httpd/htpasswd

- name: start service
  service:
    name: httpd
    state: restarted
    enabled: yes

vars变量模块

复制代码
[devops@ansible1 .ansible]$ cat roles/httpd/vars/main.yml
---
# vars file for httpd
WEBS:
  - docroot: /var/www/html
    index: www.westos.org

  - docroot: /var/www/virtual/westos.org/bbs/html
    name: bbs.westos.org
    index: bbs.westos.org

  - docroot: /var/www/virtual/westos.org/login/html
    name: login.westos.org
    index: login.westos.org

templates模块

复制代码
[devops@ansible1 .ansible]$ cat roles/httpd/templates/vhosts.conf.j2
{% for web in WEBS %}
{% if web.name is not defined %}
        <VirtualHost _default_:80>
{% endif %}
{% if web.name is defined %}
        <VirtualHost *:80>
        ServerName {{web.name}}
{% endif %}
        DocumentRoot {{web.docroot}}
</VirtualHost>
{% endfor %}
<Directory /var/www/virtual/westos.org/login/html>
        AuthUserFile /etc/httpd/htpasswd
        AuthName "Please input username and password"
        AuthType basic
        Require valid-user
</Directory>

触发器模块

复制代码
[devops@ansible1 .ansible]$ cat roles/httpd/handlers/main.yml
---
# handlers file for httpd
- name: restarted httpd
  service:
    name: httpd
    state: restarted
    enabled: no

- name: firewalld
  firewalld:
    service: httpd
    state: enabled
    permanent: yes
    immediate: yes

启用模块

复制代码
[devops@ansible1 .ansible]$ cat httpd.yml
- name: install httpd server
  hosts: all
  roles:
    - role: httpd
  pre_tasks:
    - name: show pre
      debug:
        msg: start
  post_tasks:
    - name: show post
      debug:
        msg: end

测试

相关推荐
爱吃橘子橙子柚子1 天前
3CPU性能排查总结(超详细)【Linux性能优化】
运维·cpu
舒一笑3 天前
程序员效率神器:一文掌握 tmux(服务器开发必备工具)
运维·后端·程序员
NineData3 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
梦想很大很大4 天前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair4 天前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主4 天前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
甲鱼9295 天前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
碳基沙盒8 天前
OpenClaw 多 Agent 配置实战指南
运维
蝎子莱莱爱打怪10 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
DianSan_ERP11 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet