运维复杂的场景:建议使用 roles,代码复用度高

roles:多个角色的集合, 可以将多个的role,分别放至roles目录下的独立子目录中

roles/
  mysql/
  nginx/
  tomcat/
  redis/

72、ansible-roles_roles

roles/
└── webserver
    ├── defaults
    │   └── main.yml
    ├── files
    │   ├── index.html
    │   └── styles.css
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── tasks
    │   ├── main.yml
    │   ├── setup.yml
    │   └── update.yml
    ├── templates
    │   ├── nginx.conf.j2
    │   └── php.ini.j2
    └── vars
        └── main.yml

其中,各个目录和文件的作用如下:

defaults/main.yml:存放变量的默认值,当变量未定义时使用该值。

files/:存放一些无需模板化的文件,如静态网页文件、图片等。

handlers/main.yml:定义一些需要在任务完成后执行的操作,比如重新加载配置文件等。

meta/main.yml:定义当前角色的元数据信息,如作者、依赖等。

tasks/main.yml:定义当前角色的主要任务,按照顺序执行。该文件通常只包含include语句,将具体任务拆分到单独的文件中。

tasks/setup.yml:安装和配置Nginx、PHP等软件包的任务。

tasks/update.yml:更新系统软件包的任务。

templates/:存放模板文件,根据该目录下的jinja2模板文件生成配置文件等。

vars/main.yml:存放变量,这些变量在playbook或其他role中定义,并在该角色中使用。

websever的roles使用

可以根据需要修改和扩展这些目录和文件,下面是一个简单的webserver角色的示例代码:

#defaults/main.yml:

# defaults file for webserver
webserver_port: 80
#files/index.html:

<!DOCTYPE html>
<html>
<head>
  <title>Welcome to my website!</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <h1>Welcome to my website!</h1>
  <p>This website is powered by Nginx and PHP.</p>
</body>
</html>
#files/styles.css:

/* CSS styles for my website */
body {
  background-color: #f0f0f0;
  font-family: sans-serif;
}
h1 {
  font-size: 2.5rem;
  color: #0066cc;
  text-align: center;
}
p {
  font-size: 1.5rem;
  text-align: center;
}
#handlers/main.yml:

# handlers file for webserver
- name: reload nginx
  systemd:
    name: nginx
    state: restarted
#meta/main.yml:

# meta data for webserver
galaxy_info:
  author: Your Name
  description: A role for setting up Nginx and PHP
  license: MIT
  min_ansible_version: 2.10
  platforms:
  - name: Debian
    versions:
    - buster
playbook调用角色

调用方法一:

---
- hosts: websrvs
  remote_user: root
  roles:
    - mysql
    - memcached
    - nginx

调用方法二:

---
- hosts: all
  remote_user: root
  roles:
    - mysql
    - { role: nginx, username: nginx }

调用方法三:

---
- hosts: all
  remote_user: root
  roles:
    - { role: nginx, username: nginx, when: ansible_distribution_major_version== '7' }
roles 中 tags 使用
#nginx-role.yml
---
- hosts: websrvs
  remote_user: root
  roles:
    - { role: nginx ,tags: [ 'nginx', 'web' ] ,when:ansible_distribution_major_version == "6" }
    - { role: httpd ,tags: [ 'httpd', 'web' ] }
    - { role: mysql ,tags: [ 'mysql', 'db' ] }
    - { role: mariadb ,tags: [ 'mariadb', 'db' ] }
    
 ansible-playbook --tags="nginx,httpd,mysql" nginx-role.yml
案例1:实现 httpd 角色
#创建角色相关的目录
mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files}

#创建角色相关的文件
cd /data/ansible/roles/httpd/

#main.yml 是task的入口文件
vim tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml

vim tasks/group.yml
- name: create apache group
	group: name=apache system=yes gid=80
  
vim tasks/user.yml
- name: create apache user
	user: name=apache system=yes shell=/sbin/nologin home=/var/www/ uid=80
	group=apache
  
vim tasks/install.yml
- name: install httpd package
	yum: name=httpd
  
vim tasks/config.yml
- name: config file
	copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
	notify: restart
  
vim tasks/index.yml
- name: index.html
	copy: src=index.html dest=/var/www/html/
  
vim tasks/service.yml
- name: start service
	service: name=httpd state=started enabled=yes
  
vim handlers/main.yml
- name: restart
	service: name=httpd state=restarted
  
#在files目录下准备两个文件
ls files/
httpd.conf index.html

tree /data/ansible/roles/httpd/
/data/ansible/roles/httpd/
├── files
│ ├── httpd.conf
│ └── index.html
├── handlers
│ └── main.yml
└── tasks
├── config.yml
├── group.yml
├── index.yml
├── install.yml
├── main.yml
├── service.yml
└── user.yml

#在playbook中调用角色
vim /data/ansible/role_httpd.yml
---
# httpd role
- hosts: websrvs
	remote_user: root
	roles:
		- httpd
    
#运行playbook
ansible-playbook /data/ansible/role_httpd.yml