Ansible 中的 Role

Role

role(角色) 用来实现代码的组织管理功能,将实现各种不同功能的 playook 文件,变量文件,模板文 件,handlers 文件根据约定,分别放置在不同的目录,分门别类的管理起来,使其看起来更像一个项 目,其主要用来解决多文件之间的相互包含,引用,组合等问题,将各个功能模块进行拆分,使其原子 化,要实现一个大型复杂需求时,再用 include 指令来引用不同的功能

角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中,复杂的场景中,建 议使用 roles,代码复用度高

Role 工作原理和组成

在使用 ansible 实现复杂配置时,难免需要配置不同的主机分组,配置各种变量和模板,使用各种功能 模块等。那我们就可以将这种大型的复杂度高的任务进行分解,拆分成角色(role),来实现

例如:需要在 A 主机上配置 LNMP 环境,在 B 主机上配置 LAMP 环境,在 C 主机上配置 MySQL,在 D 主机上 Redis,则我们可以将各种不同的服务定义成 Role,然后根据需求搭配不同的 Role 来实现不同 的需求,达到代码复用,灵活配置的目的

默认roles存放路径

bash 复制代码
/root/.ansible/roles
/usr/share/ansible/roles
/etc/ansible/roles

role 的目录结构

  • tasks 任务文件目录,至少有一个名为 main.yaml 文件,该文件通过 include 来引用目录下的其它文件

  • files 该 role 使用过程中要用到的文件,比如安装包,比如 copy 要用到的文件

  • vars 变量文件目录,至少有一个名为 main.yaml 的文件,该文件通过 include 来引用目录下的其 它文件

  • templates 模板文件目录,如果没有特别指定,则在该role 中其它文件要引用模板文件都默认存放在此目录

  • handlers 触发器目录,至少要有一个名为main.yaml的文件,该文件通过include 来引用目录下 的其它文件

  • default 在该 role 中会用到的默认变量,此处的变量优先级比 vars 中的变量优先级更高

  • meta 额外信息需要用到的一些数据,至少有一个名为 main.yaml 的文件,该文件通过 include 来 引用目录下的其它文件

在 playbook 中调用 role

直接调用

bash 复制代码
- hosts: websrvs
  remote_user: root
  roles:
   - mysql
   - memcached
   - nginx

传参

bash 复制代码
- hosts: websrvs
  remote_user: root
  roles:
   - role: mysql
     var1: 123
   - {role: memecached, var1: 456}

条件判断

bash 复制代码
- hosts: websrvs
  remote_user: root
  roles:
   - role: mysql
     var1: 123
     when: ansible_distribution_major_version == '7'
     
   - {role: mysql, var1: 456, when: ansible_distribution_major_version == '7'}

用 role 实现 LNMP

节点 系统 IP 服务
ansible ubuntu 10.0.0.157
node-1 ubuntu 10.0.0.161 nginx,php,wordpress
node-2 ubuntu 10.0.0.141 mysql

在ansible主机

bash 复制代码
#创建目录
[root@ubuntu24 ~]# tree roles/
roles/
├── mysql
│   ├── files
│   ├── tasks
│   └── templates
├── nginx
│   ├── files
│   ├── tasks
│   └── templates
├── php
│   ├── files
│   ├── tasks
│   └── templates
├── service
│   ├── files
│   ├── tasks
│   └── templates
└── wordpress
    ├── files
    ├── tasks
    └── templates

21 directories, 0 files

nginx role 实现

bash 复制代码
[root@ubuntu24 ~]# tree /root/roles/nginx/
/root/roles/nginx/
├── files
├── tasks
│   ├── install.yaml
│   ├── main.yaml
│   └── user.yaml
└── templates

[root@ubuntu24 ~]# cat roles/nginx/tasks/user.yaml
- name: add-nginx-group
  group: name=nginx gid=800 system=yes

- name: add-nginx-user
  user: name=nginx group=800 system=yes uid=800 create_home=no
  
  
[root@ubuntu24 ~]# cat roles/nginx/tasks/install.yaml
- name: install-nginx
  apt: name=nginx state=present
  
  
[root@ubuntu24 ~]# cat roles/nginx/tasks/main.yaml
- include_tasks: user.yaml
- include_tasks: install.yaml

php role 实现

bash 复制代码
[root@ubuntu24 ~]# cat roles/php/tasks/user.yaml
- name: add-php-group
  group: name=www-data gid=33 system=yes

- name: add-php-user
  user: name=www-data group=33 system=yes uid=33 create_home=yes home=/var/www shell=/usr/sbin/nologin
  
[root@ubuntu24 ~]# cat roles/php/tasks/install.yaml
- name: install-php
  apt: name=php-fpm,php-mysqlnd,php-json,php-gd,php-xml,php-mbstring,php-zip state=present
  
[root@ubuntu24 ~]# cat roles/php/tasks/main.yaml
- include_tasks: user.yaml
- include_tasks: install.yaml

wordpress role 实现

bash 复制代码
[root@ubuntu24 ~]# cat roles/wordpress/tasks/wp_get_code.yaml
- name: wget-wordpress
  get_url: url=https://cn.wordpress.org/latest-zh_CN.zip dest=/var/www/html/wordpress.zip
  
  
[root@ubuntu24 ~]# cat roles/wordpress/tasks/wp_unarchive.yaml
- name: wp-unarchive
  unarchive: src=/var/www/html/wordpress.zip dest=/var/www/html/ owner=www-data group=www-data remote_src=yes
  
[root@ubuntu24 ~]# cat roles/wordpress/tasks/wp_set_domain.yaml
- name: set-wp-domain
  template: src=domain.conf.j2 dest=/etc/nginx/sites-enabled/{{ WP_DOMAIN }}.conf
  
[root@ubuntu24 ~]# cat roles/wordpress/tasks/main.yaml
- include_tasks: wp_get_code.yaml
- include_tasks: wp_unarchive.yaml
- include_tasks: wp_set_domain.yaml

[root@ubuntu24 ~]# cat roles/wordpress/templates/domain.conf.j2
server{
   listen {{ WP_PORT }};
   server_name {{ WP_DOMAIN }};
   include /etc/nginx/default.d/*.conf;
   root {{ WP_PATH }};
   index index.php index.html;

   location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
   }
}

service role 实现

bash 复制代码
[root@ubuntu24 ~]# tree roles/service/
roles/service/
├── files
├── tasks
│   ├── main.yaml
│   └── service.yaml
└── templates

4 directories, 2 files

[root@ubuntu24 ~]# cat roles/service/tasks/service.yaml
- name: service
   service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
   loop: "{{ SERVICE_LIST }}"
   
[root@ubuntu24 ~]# cat roles/service/tasks/main.yaml
- include_tasks: service.yaml

mysql role 实现

bash 复制代码
[root@ubuntu24 ~]# tree roles/mysql/
roles/mysql/
├── files
│   └── grant.sql
├── tasks
│   ├── copy_file.yaml
│   ├── grant.yaml
│   ├── install.yaml
│   ├── main.yaml
│   ├── restart.yaml
│   └── user.yaml
└── templates

4 directories, 7 files

[root@ubuntu24 ~]# cat roles/mysql/tasks/user.yaml
- name: add-mysql-group
  group: name=mysql gid=306 system=yes

- name: add-mysql-user
  user: name=mysql group=306 system=yes uid=306 create_home=no


[root@ubuntu24 ~]# cat roles/mysql/tasks/install.yaml
- name: apt-install-mysql-server
  apt: name=mysql-server state=present update_cache=yes

- name: set-mysqld-conf-task-1
  lineinfile: path=/etc/mysql/mysql.conf.d/mysqld.cnf backrefs=yes regexp='^(bind-address.*)$' line='#\1'

- name: set-mysqld-conf-task-2
  lineinfile: path=/etc/mysql/mysql.conf.d/mysqld.cnf line='skip-name-resolve'

- name: set-mysqld-conf-task-3
  lineinfile: path=/etc/mysql/mysql.conf.d/mysqld.cnf line='default-authentication-plugin=mysql_native_password'
  
[root@ubuntu24 ~]# cat roles/mysql/tasks/restart.yaml
- name: restart-mysql-service
  service: name=mysql enabled=yes state=restarted
  
[root@ubuntu24 ~]# cat roles/mysql/tasks/copy_file.yaml
- name: copy-mysql-file
  copy: src=files/grant.sql dest=/tmp/grant.sql
  
[root@ubuntu24 ~]# cat roles/mysql/tasks/grant.yaml
- name: mysql-client-init
  shell: mysql </tmp/grant.sql
  
[root@ubuntu24 ~]# cat roles/mysql/tasks/main.yaml
- include_tasks: user.yaml
- include_tasks: install.yaml
- include_tasks: restart.yaml
- include_tasks: copy_file.yaml
- include_tasks: grant.yaml  

[root@ubuntu24 ~]# cat roles/mysql/files/grant.sql
create database if not exists wordpress;
create user 'wp_user'@'10.0.0.%' identified by '123456';
grant all on wordpress.* to 'wp_user'@'10.0.0.%';

flush privileges;

playbook 中配置 role

bash 复制代码
[root@ubuntu24 ~]# cat lnmp_wp.yaml
- hosts: 10.0.0.161
  gather_facts: no
  vars:
    WP_PORT: 80
    WP_DOMAIN: blog.baidu.com
    WP_PATH: /var/www/html/wordpress
    SERVICE_LIST: [ {name: nginx, state: restarted, enabled: yes},{name: php8.3-fpm, state: started, enabled: yes} ]
  roles:
    - nginx
    - php
    - wordpress
    - service
  
  
[root@ubuntu24 ~]# cat mysql.yaml
- hosts: 10.0.0.141
  gather_facts: no
  roles:
   - mysql

ansible-galaxy

ansible-galaxy 用来管理官方在云端提供的 role

bash 复制代码
ansible-galaxy [-h] [--version] [-v] TYPE ...

#常用选项
--version 	#显示版本信息
-h|--help 	#查看帮助
-v|--verbose #显示详细信息

#TYPE,不写时默认 type 为 roel
collection 	#合集
role 		#角色

#常用子命令
init 	#初始化
list 	#列出所有己安装的role或collection,
 		#此处的己安装,表示将相关文本下载到本地了,role 还要再调用 ansibleplaybook
search 	#在服务器上搜索
info 	#显示 role 
install #安装,即下载到本机,后面要再使用 ansible-playbook 进行安装
remove 	#移除,即删除本地相关文件
相关推荐
风清再凯21 小时前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机21 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星14 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥14 天前
ANSIBLE
ansible
码农101号14 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号14 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信15 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li15 天前
ANSIBLE(运维自动化)
运维·自动化·ansible
@donshu@18 天前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible
Kendra91921 天前
Ansible
ansible