1,roles 簡介
- ansible 自1.2 版本引入的新特性,用於層次性、結構化地組織
playbook
。roles
可以根據層次型結構自動化轉載變量文件、tasks
以及 handlers
等。要使用 roles
只須要在 playbook
中使用 include
指令便可。簡單來說,roles
就是經過分別將變量、文件、任務、模板機處理器放置於單獨的目錄中,並能夠便捷地 include
它們的一種機制。角色通常用於基於主機構建服務的場景中,但也能夠是用於構建守護進程等場景中。
2,場景
- 複雜場景:建議使用
roles
,代碼複用度高
- 變動指定主機或主機組
- 如命名不規範維護和傳承成本大
- 某些功能需多個
playbook
,經過 ````includes````` 便可實現
3,角色(roles):目錄編排
4,角色(roles):
4.1 建立role 的步驟
- 1 建立以 roles 命名的目錄
- 2 在roles 目錄中分別建立以各角色名稱命名的目錄,如 nginx 等
- 3 在每一個角色命名的目錄中分別建立 files、handlers、meta、tasks、templates、vars 目錄;用不到的目錄能夠建立爲空目錄,也能夠不建立
- 4 在 playbook 文件中,調用各角色
4.2 目錄結構
- 每一個角色,以特定的層級目錄結構進行組織
- roles 目錄結構
playbook.yml
roles/
└── project
├── default
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
4.3 roles 各目錄做用
/roles/project/
:項目名稱,有如下子目錄
file/
:存放由 copy
或 script
模塊等調用的文件
templates/
:template
模塊查找所須要模塊文件的目錄
tasks/
:定義 tasks
,role
的基本元素,至少應該包含一個名爲 main.yml
的文件;其它的文件須要在此文件中經過 include
進行包含
handlers/
:至少應該包含一個名爲 main.yml
的文件;其它的文件須要在此文件中經過 include
進行包含
vars/
:定義比那裏,至少應該包含一個名爲 main.yml
的文件;其它的文件須要在此文件中經過 include
進行包含
meta/
:定義當前角色的特殊設定及其依賴關係,至少應該包含一個名爲 main.yml
的文件;其它的文件須要在此文件中經過 include
進行包含
default/
:設定默認變量時使用此目錄中的 main.yml
文件
4.4 調用角色
- hosts: websrvs
remote_user: root
roles:
- mysql
- nginx
- memcached
- 方法2
- 傳遞變量給角色
- 鍵role用於指定角色名稱
- 後續的 k/v 用於傳遞變量給角色
- hosts: websrvs
remote_user: root
roles:
- mysql
- { role: nginx, username: nginx}
- hosts: websrvs
remote_user: root
roles:
- { role: nginx, tags: ['web', 'nginx'], when: ansible_distribution_major_version == "7" }
ansible-playbook --tags="nginx,httpd,mysql" nginx-role.yml
---
- hosts: websrvs
remote_user: root
roles:
- { role: nginx, tags: ['web', 'nginx'], when: ansible_distribution_major_version == "7" }
- { role: httpd, tags: ['web', 'httpd'] }
- { role: mysql, tags: ['db', 'mysql'] }
- { role: marridb, tags: ['db', 'mysql'] }
- { role: php }
5,實例
5.1 nginx
nginx_role.yml
roles/
└── nginx
├── tasks
│ ├── group.yml
│ ├── main.yml
│ ├── restart.yml
│ ├── start.yml
│ ├── templ.yml
│ ├── user.yml
│ └── yum.yml
└── templates
└── nginx.conf.j2
- roles/nginx/tasks/main.yml
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: start.yml
- roles/nginx/tasks/group.yml
- name: create group
group: name=nginx gid=80
- roles/nginx/tasks/user.yml
- name: create user
user: name=nginx uid=80 group=nginx system=yes shell=/sbin/nologin
- roles/nginx/tasks/yum.yml
- name: install package
yum: name=nginx
- roles/nginx/tasks/templ.yml
- name: copy conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- roles/nginx/tasks/start.yml
- name: start service
service: name=nginx state=started enabled=true
- roles/nginx/templates/nginx.conf.j2
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes {{ ansible_processor_vcpus+2 }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
# ansible websrvs -m shell -a 'getent passwd nginx'
192.168.2.132 | CHANGED | rc=0 >>
nginx:x:80:80::/home/nginx:/sbin/nologin
192.168.2.131 | CHANGED | rc=0 >>
nginx:x:80:80::/home/nginx:/sbin/nologin
# ansible websrvs -m shell -a 'getent group nginx'
192.168.2.132 | CHANGED | rc=0 >>
nginx:x:80:
192.168.2.131 | CHANGED | rc=0 >>
nginx:x:80:
# ansible websrvs -m shell -a 'id nginx'
192.168.2.132 | CHANGED | rc=0 >>
uid=80(nginx) gid=80(nginx) groups=80(nginx)
192.168.2.131 | CHANGED | rc=0 >>
uid=80(nginx) gid=80(nginx) groups=80(nginx)
# ansible websrvs -m shell -a 'rpm -q nginx'
192.168.2.132 | CHANGED | rc=0 >>
nginx-1.12.2-2.el7.x86_64
192.168.2.131 | CHANGED | rc=0 >>
nginx-1.12.2-2.el7.x86_64
# ansible websrvs -m setup -a 'filter=ansible_processor_vcpus'
192.168.2.132 | SUCCESS => {
"ansible_facts": {
"ansible_processor_vcpus": 8
},
"changed": false
}
192.168.2.131 | SUCCESS => {
"ansible_facts": {
"ansible_processor_vcpus": 8
},
"changed": false
}
# ansible websrvs -m shell -a 'ps aux | grep nginx'
192.168.2.132 | CHANGED | rc=0 >>
root 24733 0.0 0.0 125052 2248 ? Ss 14:29 0:00 nginx: master process /usr/sbin/nginx
nginx 24734 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24735 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24736 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24737 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24738 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24739 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24740 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24741 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24742 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24743 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
root 25619 0.0 0.0 113128 1200 pts/1 S+ 14:45 0:00 /bin/sh -c ps aux | grep nginx
root 25621 0.0 0.0 112664 948 pts/1 S+ 14:45 0:00 grep nginx
192.168.2.131 | CHANGED | rc=0 >>
root 24864 0.0 0.0 125052 2248 ? Ss 14:29 0:00 nginx: master process /usr/sbin/nginx
nginx 24865 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24866 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24867 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24868 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24869 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24870 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24871 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24872 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24873 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
nginx 24874 0.0 0.0 125440 3148 ? S 14:29 0:00 nginx: worker process
root 25751 0.0 0.0 113128 1200 pts/1 S+ 14:45 0:00 /bin/sh -c ps aux | grep nginx
root 25753 0.0 0.0 112664 952 pts/1 S+ 14:45 0:00 grep nginx