sdfsf 发表于 2017-10-25 08:52:01

ansible 角色定义及调用(nginx)

Roles的介绍
Roles是ansible自1.2版本引入的新特性,用于层次性,结构化地组织playbook,roles能够根据层次型结构自动自动装在变量文件、tasks以及handlers等。

创建roles的步骤


[*]创建以roles命名的目录:
[*]在roles目录中分别创建以各角色名称命名的目录,如webservers等:
[*]在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录:用不到的目录可以创建为空目录,也可以不创建
[*]在playbook文件中,调用各角色

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.在服务器生成免密钥文件,推送到客户端
# ssh-keygen
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.131


2.安装ansible

# yum install -y ansible


3.到/etc/ansible 有个可以自定义roles的目录

# cd /etc/ansible/
# ls
ansible.cfghostsnginx.yamlroles

4.定义要执行的角色路径
# cat /etc/ansible/nginx.yaml
- hosts: 10.0.0.131
remote_user: root
roles:
- nginx



5.定义掩码安装nginx,在roles目录下的目录及文件都要自己创建
# ls
nginx
# cd nginx
# ls
fileshandlerstaskstemplatesvars

# cd 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目录创建任务
# 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压缩包目录
# ls files/
nginx-1.12.0.tar.gz    ##对应tasks第二行

8.template这一行对应的是template这个目录和主服务端定义的变量
# 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
#usernobody;
worker_processes
{{ ansible_processor_vcpus }};
#pid      logs/nginx.pid;
events {
    worker_connections65532;
}
http {
    include       mime.types;
    default_typeapplication/octet-stream;
    sendfile      on;
    #tcp_nopush   on;
    #keepalive_timeout0;
    keepalive_timeout65;
    #gzipon;
    server {
   
   listen       {{ ngxport }};
      server_namelocalhost;
      #charset koi8-r;
      #access_loglogs/host.access.logmain;
      location / {
            root   /web;
            indexindex.html index.htm;
      }
      #error_page404            /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_indexindex.php;
      #    fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
      #    include      fastcgi_params;
      #}
    }
    include vhosts/*.conf;
}








9.查看我们定义的变量,在vars目录下# cat vars/main.yml
ngxport: "8080"


10.编辑触发器
# cat handlers/main.yml
- name: start nginx   
shell: /usr/local/nginx/sbin/nginx


11.开始执行

# ansible-playbook/etc/ansible/nginx.yaml

PLAY *************************************************************

GATHERING FACTS ***************************************************************
ok:

TASK: ******************************
changed:

TASK: *****************************************************
changed:

TASK: ***********************************************
ok: => (item=openssl-devel,pcre-devel,gcc)

TASK: *******************************************************
changed:

TASK: *************************************************
changed:

TASK: *************************************
changed:

NOTIFIED: ***********************************************
changed:

PLAY RECAP ********************************************************************
10.0.0.131               : ok=8    changed=6    unreachable=0    failed=0   



12.查看client客户端nginx服务已经启动
# ps -ef |grep nginx
root      34655      10 02:13 ?      00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx   34656346551 02:13 ?      00:00:01 nginx: worker process
root      34660281300 02:16 pts/1    00:00:00 grep --color=auto nginx
# 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目录:# cat temp_server.conf server { listen80; server_name{{ server_name }}; indexindex.php index.html; root {{root_dir }}; } ##在vars定义变量:# cat main.yml ngxport:"8080"server_name:"www.xxx.com"root_dir:"/web" 重写tasks步骤:# cat main.yml- name:copy conf file nginx.conf          # 调用templates模块template: src=temp_server.conf dest=/usr/local/nginx/conf/vhosts/{{server_name }}.conftags: ngxconfnotify: reload nginx service                # 调用handlers模块
页: [1]
查看完整版本: ansible 角色定义及调用(nginx)