Ansible-roles(角色)
文章目录
一、Ansible Roles介绍
一个数据中心有可能存在好多类型的服务器。比如WEB类型、DB类型、开发人员使用的开发类型、QA使用的测试类型等等。实际生产中,基本每个类型的服务器的初始化行为都不一致。那要在一个PlayBook中将这些动作完成,这个PlayBook将变得臃肿、庞大,且难以后续维护和更新。如果能够针对每个类型的服务器单独编写PlayBook,最后通过某种方式整合这PlayBook, 在管理方式上就又会变得简单。Ansible中提供了类似的概念,也就是Role。它允许管理员将他们复杂的PlayBook分解成一个个小的逻辑单元, 以便于维护和管理
二、Roles结构
三、制作一个Role
1、最终版本的playbook
name: template playbook example
hosts: 10.11.67.32
remote_user: root
vars:
createuser:
- tomcat
- www
- mysql
tasks:
- name: create user
user: name={{ item }} state=present
with_items: "{{ createuser }}"
- name: yum nginx webserver
yum: name=nginx state=present
# use ansible template
- name: update nginx main config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
tags: updateconfig
notify: reload nginx server
- name: add virtualhost config
copy:
src: nginx.conf
dest: /etc/nginx/conf.d/
tags: updateconfig
notify: reload nginx server
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
tags: updateconfig
- name: check nginx running
stat: path=/var/run/nginx.pid
register: nginxrunning
tags: updateconfig
- name: print nginx syntax
debug: var=nginxsyntax
- name: start nginx server
service: name=nginx state=started
when:
- nginxsyntax.rc == 0
- nginxrunning.stat.exists == false
handlers:
- name: reload nginx server
service: name=nginx state=started
when:
- nginxsyntax.rc == 0
- nginxrunning.stat.exists == true
2、nginx的主配置文件nginx.conf.j2
user nginx;
{# start process equal cpu cores #}
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
gzip on;
gzip_min_length 1k;
gzip_buffers 8 64k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/json application/xml application/x-shockwave-flash application/javascript image/svg+xml image/x-icon;
gzip_vary on;
{# add_header {{ ansible_hostname }}; #}
add_header x-hostname {{ ansible_hostname }};
include /etc/nginx/conf.d/*.conf;
}
3、nginx的子配置文件nginx.conf
server {
listen 80;
server_name www.kakaops.com;
root /usr/share/nginx/html;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 1d;
}
location ~ .*\.(js|css)?$ {
expires 1d;
}
}
4、把playbook分解成role
首先利用ansible-galaxy快速创建一个role
role的名称是nginx
ansible-galaxy init nginx
- Role nginx was created successfully
5、files文件夹存放通过此角色部署的文件
存放 www.qfedu.com.conf 配置文件
6、handlers 文件夹中的main.yml包含处理程序
name: reload nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == true
7、tasks 文件夹中的 main.yml 文件包含任务列表
name: create user
user: name={{ item }} state=present
with_items: "{{ createuser }}"
- name: yum nginx webserver
yum: name=nginx state=present
# use ansible template
- name: update nginx main config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
tags: updateconfig
notify: reload nginx server
- name: add virtualhost config
copy:
src: www.qfedu.com.conf
dest: /etc/nginx/conf.d/
tags: updateconfig
notify: reload nginx server
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
tags: updateconfig
- name: check nginx running
stat: path=/var/run/nginx.pid
register: nginxrunning
tags: updateconfig
- name: print nginx syntax
debug: var=nginxsyntax
- name: start nginx server
service: name=nginx state=started
when:
- nginxsyntax.rc == 0
- nginxrunning.stat.exists == false
...
8、templates 文件夹存放模板文件
存放 nginx.conf.j2 模板
9、vars 文件夹中的 main.yml 文件包含变量列表
createuser:
- tomcat
- www
- mysql
经过以上对PlayBook 的拆分,就形成了一个nginx 的 ROLE。
回到本章开始的问题,当一个数据中心存在多种类型的服务器时,我们可以针对每个类型去单独写一个ROLE,这些ROLE 有可能分给不同的人去开发,这样不但使开发的逻辑变得简单,且开发效率也随着人员的增加而提升。
四、使用角色
1、原始经典的方式
Role 本身不能被直接执行,还是需要借助PlayBook进行间接的调用
需要创建一个入口文件,来调用 role 中的各种资源
在roles下面的nginx同级目录创建入口文件
---
- name: use roles
hosts: 10.11.67.32
roles:
- nginx
2、新的方式
在 playbook 中给定 import_role 属性,并且可以在执行 role之前和之后执行其他的任务。这种方式适用于 Ansible 2.4及以上。import_role相当于一个模块
---
- name: use role
hosts: 10.11.67.32
tasks:
- name: first 1
¦ debug:
¦ msg: "before we run our role"
- import_role:
¦ name: nginx
- name: last 1
¦ debug:
¦ msg: "after we run our role"
五、使用Galaxy获取更多的Role
ansible-galaxy init nginx
Ansible的galaxy 工具,类似程序员使用的github。运维人员可以将自己编写的Role通过galaxy这个平台进行分享。同样,我们也可以通过galaxy 这个平台去获取一些我们想要的role。
官网为:https://galaxy.ansible.com
而ansible-galaxy 则是一个使用 galaxy 命令行的工具。它使我们不用访问galaxy 的网站而获取到需要的内容
// 在galaxy 上搜索共享的ROLE
# ansible-galaxy search [galaxy tag or author]
// 安装 galaxy 上共享的 ROLE
# ansible-galaxy install [galaxy tag or author]
// 列举已经通过 ansible-galaxy 工具安装的ROLE
# ansible-galaxy list
// 创建一个ROLE 的空目录架构, 这样我们在开发一个ROLE的时候,就 不需要手动创建目录了。
# ansible-galaxy init --offline
# ansible-galaxy --help
Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...
Options:
-h, --help show this help message and exit
-v, --verbose verbose mode (-vvv for more, -vvvv to enable connection
debugging)
--version show program's version number and exit
在 ansible-galaxy --help 中可以看到子指令
子指令包含: delete|import|info|init|install|list|login|remove|search|setup
ansible-galaxy delete|import|info|init|install|list|login|remove|search|setup --help
# ansible-galaxy install --help
Usage: ansible-galaxy install [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]
Options:
-f, --force Force overwriting an existing role
-h, --help show this help message and exit
-c, --ignore-certs Ignore SSL certificate validation errors.
-i, --ignore-errors Ignore errors and continue with the next specified
role.
-n, --no-deps Don't download roles listed as dependencies
-r ROLE_FILE, --role-file=ROLE_FILE
A file containing a list of roles to be imported
-p ROLES_PATH, --roles-path=ROLES_PATH
The path to the directory containing your roles. The
default is the roles_path configured in your
ansible.cfg file (/etc/ansible/roles if not
configured)
-s API_SERVER, --server=API_SERVER
The API server destination
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exit
// 创建了名字为testrole的空ROLE目录结构,默认在执行命令的目录生产。
# ansible-galaxy init testrole
# tree testrole/
testrole/
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── tests
│ ├── hosts
│ └── test.yml
└── vars
└── main.yml