手机版

ansible 角色定义及调用(nginx)

时间:2020-03-17 来源:互联网 编辑:宝哥软件园 浏览:

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. 在服务器生成免密钥文件,推送到客户端

[root@ansible ~]# ssh-keygen 
[root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.131

2. 安装ansible

[root@ansible ~]# yum install -y ansible

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

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  nginx.yaml  roles

4. 定义要执行的角色路径

[root@ansible ~]# cat /etc/ansible/nginx.yaml 
- hosts: 10.0.0.131
  remote_user: root
  roles:
  - nginx

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

[root@ansible roles]# ls
nginx
[root@ansible roles]# cd nginx
[root@ansible nginx]# ls
files  handlers  tasks  templates  vars
[root@ansible ansible]# cd roles/
[root@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目录创建任务

[root@ansible nginx]# cat tasks/main.yml 
- 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压缩包目录

[root@ansible nginx]# ls files/
nginx-1.12.0.tar.gz    ##对应tasks第二行

8. template这一行对应的是template这个目录和主服务端定义的变量

[root@ansible nginx]# cat templates/nginx.conf 
#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目录下

[root@ansible nginx]# cat vars/main.yml 
ngxport: "8080"

10. 编辑触发器

[root@ansible nginx]# cat handlers/main.yml 
- name: start nginx     
  shell: /usr/local/nginx/sbin/nginx

11. 开始执行

[root@ansible nginx]# ansible-playbook -C /etc/ansible/nginx.yaml(测试)
[root@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服务已经启动

[root@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
[root@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目录:

[root@ansible templates]# cat temp_server.conf
server
{
listen80;
server_name{{ server_name }};
indexindex.php index.html;
root {{root_dir }};
}

在vars定义变量:

[root@ansible vars]# cat main.yml
ngxport:"8080"
server_name:"www.xxx.com"
root_dir:"/web"

重写tasks步骤:

[root@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模块

版权声明:ansible 角色定义及调用(nginx)是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。

相关文章推荐