Roles小技巧:
1.创建roles目录结构,手动或使用ansible-galaxy init test roles
2.编写roles的功能,也就是tasks。 nginx rsyncd memcached
3.最后playbook引用roles编写好的tasks
mkdir /root/roles/nginx/{tasks,templates,handlers}
##tasks
[root@manager ~]# cat /root/roles/nginx/tasks/main.yml
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
##template
[root@manager roles]# cat /root/roles/nginx/templates/nginx.conf.j2
user www;
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections {{ ansible_processor_vcpus * 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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
###handlers
[root@manager ~]# cat /root/roles/nginx/handlers/main.yml
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
#调用playbook
[root@manager roles]# cat /root/roles/site.yml
- hosts: webservers
roles:
- nginx
##hosts ansible.cfg 自备
memcached roles
#安装
#配置
#启动
#1.创建roles的目录结构
[root@manager roles]# mkdir memcached/{tasks,templates,handlers} -p
#2.编写对应的tasks (1.安装 2配置(templates) 3.启动 4.重启(handlers) )
[root@manager roles]# cat memcached/tasks/main.yml
- name: Installed Memecached Server
yum:
name: memcached
state: present
- name: Configure Memcached Server
template:
src: memcached.j2
dest: /etc/sysconfig/memcached
notify: Restart Memcached Server
- name: System Memcached Server
systemd:
name: memcached
state: started
enabled: yes
[root@manager roles]# cat memcached/templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb //2 }}"
OPTIONS=""
[root@manager roles]# cat memcached/handlers/main.yml
- name: Restart Memcached Server
systemd:
name: memcached
state: restarted
#3.playbook调用roles
[root@manager roles]# cat site.yml
- hosts: webservers
roles:
- { role: nginx, tags: web }
- { role: memcached, tags: cache }
NFS服务
#1.创建项目目录结构 --->
[root@manager roles]# mkdir nfs/{tasks,templates,handlers} -p
#2.编写task任务
#3.playbook调用roles项目
- roles:
- 1.nginxProxy+keepalived 10.0.0.5 10.0.0.6 10.0.0.3
- 2.nginx静态网站 172.16.1.7 172.16.1.8