Roles的介绍
Roles是ansible自1.2版本引入的新特性,用于层次性,结构化地组织playbook,roles能够根据层次型结构自动自动装在变量文件、tasks以及handlers等。
创建roles的步骤
roles内各目录中可用的文件 tasks目录:至少创建一个名为main.yml的文件,其定义了此角色的任务列表:此文件可以使用include包含其他的位于此目录中的tasks文件 files目录:存放由copy或者script等模块调用的文件 templates目录:templates模块会自动在此目录中寻找模板文件 handlers目录:此目录中应当包含一个main yml文件:用于定义此角色用到的各handler:在handler中使用include包含的其他的handler文件也应该位于此目录中 vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量 meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系:ansible 1.3及其以后的版本才支持 default目录:为当前角色定义默认变量时使用此目录,应该包含一个main.yml文件
实验环境:
ansible:10.0.0.128
client :10.0.0.131
执行
1.在服务器生成免密钥文件,推送到客户端
[iyunv@ansible ~]# ssh-keygen
[iyunv@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.131
2.安装ansible
[iyunv@ansible ~]# yum install -y ansible
3.到/etc/ansible 有个可以自定义roles的目录
[iyunv@ansible ~]# cd /etc/ansible/
[iyunv@ansible ansible]# ls
ansible.cfg hosts nginx.yaml roles
4.定义要执行的角色路径
[iyunv@ansible ~]# cat /etc/ansible/nginx.yaml
- hosts: 10.0.0.131
remote_user: root
roles:
- nginx
5.定义掩码安装nginx,在roles目录下的目录及文件都要自己创建
[iyunv@ansible roles]# ls
nginx
[iyunv@ansible roles]# cd nginx
[iyunv@ansible nginx]# ls
files handlers tasks templates vars
[iyunv@ansible ansible]# cd roles/
[iyunv@ansible roles]# tree
.
└── nginx
├── files
│ └── nginx-1.12.0.tar.gz
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
│ └── nginx.conf
└── vars
└── main.yml
6 directories, 5 files
6.进入tasks目录创建任务
[iyunv@ansible nginx]# cat tasks/main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| - name: copy nginx packup to remote host
copy: src=nginx-1.12.0.tar.gz dest=/usr/local/src/nginx-1.12.0.tar.gz
tags: cppkg
- name: tar nginx
shell: cd /usr/local/src/; tar -xf nginx-1.12.0.tar.gz
- name: install packger
yum: name={{ item }} state=latest
with_items:
- openssl-devel
- pcre-devel
- gcc- name: useradd
shell: useradd nginx -s /sbin/nologin
- name: install nginx
shell: cd /usr/local/src/nginx-1.12.0;./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre;make && make install
- name: copy conf file nginx.conf
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
notify: start nginx
|
7.存放nginx压缩包目录
[iyunv@ansible nginx]# ls files/
nginx-1.12.0.tar.gz ##对应tasks第二行
8.template这一行对应的是template这个目录和主服务端定义的变量
[iyunv@ansible nginx]# cat templates/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| #user nobody;
worker_processes
{{ ansible_processor_vcpus }};
#pid logs/nginx.pid;
events {
worker_connections 65532;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen {{ ngxport }};
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /web;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
}
include vhosts/*.conf;
}
|
9.查看我们定义的变量,在vars目录下 [iyunv@ansible nginx]# cat vars/main.yml
ngxport: "8080"
10.编辑触发器
[iyunv@ansible nginx]# cat handlers/main.yml
- name: start nginx
shell: /usr/local/nginx/sbin/nginx
11.开始执行
[iyunv@ansible nginx]# ansible-playbook /etc/ansible/nginx.yaml
PLAY [10.0.0.131] *************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.0.131]
TASK: [nginx | copy nginx packup to remote host] ******************************
changed: [10.0.0.131]
TASK: [nginx | tar nginx] *****************************************************
changed: [10.0.0.131]
TASK: [nginx | install packger] ***********************************************
ok: [10.0.0.131] => (item=openssl-devel,pcre-devel,gcc)
TASK: [nginx | useradd] *******************************************************
changed: [10.0.0.131]
TASK: [nginx | install nginx] *************************************************
changed: [10.0.0.131]
TASK: [nginx | copy conf file nginx.conf] *************************************
changed: [10.0.0.131]
NOTIFIED: [nginx | start nginx] ***********************************************
changed: [10.0.0.131]
PLAY RECAP ********************************************************************
10.0.0.131 : ok=8 changed=6 unreachable=0 failed=0
12.查看client客户端nginx服务已经启动
[iyunv@zxb4 ~]# ps -ef |grep nginx
root 34655 1 0 02:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 34656 34655 1 02:13 ? 00:00:01 nginx: worker process
root 34660 28130 0 02:16 pts/1 00:00:00 grep --color=auto nginx
[iyunv@zxb4 ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 34655/nginx: master
####附加
假如我们经常要增加nginx站点,直接写好模板推送到vhos目录: [iyunv@ansible templates]# cat temp_server.conf server { listen80; server_name{{ server_name }}; indexindex.php index.html; root {{root_dir }}; } ##在vars定义变量: [iyunv@ansible vars]# cat main.yml ngxport:"8080" server_name:"www.xxx.com" root_dir:"/web" 重写tasks步骤: [iyunv@ansible tasks]# cat main.yml - name:copy conf file nginx.conf # 调用templates模块 template: src=temp_server.conf dest=/usr/local/nginx/conf/vhosts/{{server_name }}.conf tags: ngxconf notify: reload nginx service # 调用handlers模块
|