playbook+roles安装nginx实战

基本目录结构

在这里插入图片描述

host文件夹

用于存放主机清单文件

hosts文件

hosts文件内容如下:(仅供参考)

[proxy]
node2
[web]
192.168.xx.xx

playbook-all-roles.yml文件

用于指定执行哪个role的文件(命名可以自定义)

文件内容如下:(仅供参考)

因为roles文件夹下只有nginx一个文件夹,所以- role: nginx就指定的是nginx这个role

- hosts: proxy
  remote_user: root
  gather_facts: no
  roles:
    - role: nginx

roles文件夹

用于存放各种类型的role,本次只演示nginx这一个

nginx文件夹

代表着nginx这个role,里面存放着各种配置文件

主要框架如下:

  • default:在每个角色文件夹内,‌default文件夹用于存放该角色的默认变量。‌这些变量可以在其他地方被引用,‌用于配置角色的行为。‌
  • files:用于存放角色部署时可能需要用到的一些文件。‌这些文件可以通过Ansible的copy模块传输到远程主机上。‌
  • handlers:包含处理任务的配置文件,‌当某些条件满足时,‌会触发这些处理器执行相应的操作,‌如重启服务或应用配置更改等。‌
  • meta:主要编写依赖关系,即一个独立模块引用另一个role
  • tasks:包含角色的具体任务定义,‌这些任务描述了如何在远程主机上执行操作,‌如安装软件、‌配置服务等。‌
  • templates:模板文件
  • vars:虽然roles目录结构中没有明确提到vars文件夹,‌但在实际使用中,‌有时会在角色内部定义一些变量,‌这些变量可以存放在vars文件夹中,‌以便在任务中使用。‌

本次只讲解使用到的文件夹

files文件夹

目录结构

[root@master files]# tree
.
├── index.html
├── nginx-1.22.1.tar.gz
├── nginx.conf
├── nginx.service
└── yohoo.conf

index.html

自定义的nginx访问首页,按需添加

nginx-1.22.1.tar.gz

nginx安装包

nginx.conf

nginx的主配置文件

内容如下:(仅供参考)

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    #include       /usr/local/nginx/conf.d/yohoo.conf;
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #开启gzip的功能,目的:提高传输效率,节约带宽
    gzip  on;
    #限制最小压缩,小于一字节文件不会压缩
    gzip_min_length 1;
    #定义压缩的级别(压缩比,文件越大,压缩越多,但是cpu使用就越多)
    gzip_comp_level 3;
    #include /usr/local/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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;
        }

    }
    include yohoo.conf;
    server {
        listen       89;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
        #include /usr/local/nginx/conf.d/*.conf;
}

nginx.service

用于设置开机自启动,以及系统级启动,例如:systemctl start nginx

内容如下:

[Unit]
Description=nginx web service
After=network.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
 
[Install]
WantedBy=default.target
yohoo.conf

自定义nginx的conf文件,按需使用,需要使用则在nginx.conf中配置include xxx.conf,可参考上面nginx.conf文件配置

文件内容如下:

upstream tomcats {
        server 192.168.xx.xx1:8080 max_conns=2;
        server 192.168.xx.xx2:8080 max_conns=2;
        }

server {
        listen       99;
        server_name  localhost;

        location / {
            proxy_pass http://tomcats;
        }
        location /img {
            root www;
        }
        location /static {
            alias www/img;
        }
        #正则匹配
        location ~ \.(png|jpg) {
            root www;
        }
        #正则匹配不区分大小写
        location ~* \.(jpg|png) {
            root www;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

server {
        listen       100;
        server_name  localhost;
        #精准匹配
        location = / {
            root www;
            index index.html;
        }
        #前缀匹配
        location ^~ /img {
            root www;
        }
        #正则匹配
        location ~ \.(png|jpg) {
            root www;
        }
}

handlers文件夹

目录结构

[root@master handlers]# tree
.
└── main.yml

注意:只能定义成main.yml文件名

文件内容如下:(仅供参考)

功能:重启nginx服务

#触发后重启nginx
- name: restart
  shell: systemctl restart nginx
vars文件夹

目录结构

[root@master vars]# tree
.
└── main.yml

注意:只能定义成main.yml文件名

文件内容如下:(仅供参考)

功能:定义一些参数

src_nginx: "nginx-1.22.1.tar.gz"
nginx_decompress_dir: "/usr/local"
nginx_install_dir: "/usr/local/nginx"
nginx_after_decompress_name: "nginx-1.22.1"
src_nginx_conf: "nginx.conf"
src_nginx_include_conf: "yohoo.conf"
dest_nginx_conf: "/usr/local/nginx/conf/nginx.conf"
dest_nginx_include_conf: "/usr/local/nginx/conf/yohoo.conf"
src_nginx_index: "index.html"
dest_nginx_index: "/usr/local/nginx/html/"
nginx_service: "nginx.service"
tasks文件夹

目录结构

[root@master tasks]# tree
.
├── bianyi_and_install_nginx.yml
├── config_enabled_start.yml
├── config_nginx.yml
├── firewall_open_port.yml
├── index_nginx.yml
├── install_nginx_bianyi.yml
├── install_nginx_yilai.yml
├── main.yml
└── nginx_pkg_transfer_and_jieya.yml

注意:除了main.yml文件必须要有,其他yml文件按需自定义名

main.yml

控制执行顺序

文件内容如下:

- include: install_nginx_bianyi.yml
- include: install_nginx_yilai.yml
- include: nginx_pkg_transfer_and_jieya.yml
- include: bianyi_and_install_nginx.yml
- include: config_nginx.yml
- include: index_nginx.yml
- include: config_enabled_start.yml
- include: firewall_open_port.yml
install_nginx_bianyi.yml

安装编译环境

文件内容如下:

- name: 安装nginx编译环境
  yum:
    name:
      - gcc
      - gcc-c++
    state: installed
install_nginx_yilai.yml

安装依赖

文件内容如下:

- name: 安装nginx依赖
  yum:
    name:
      - pcre
      - pcre-devel
      - zlib
      - zlib-devel
      - openssl
      - openssl-devel
    state: installed
nginx_pkg_transfer_and_jieya.yml

nginx安装包传输和解压

文件内容如下:

- name: 传输并解压
  unarchive:
    src: "{{ src_nginx }}"
    dest: "{{ nginx_decompress_dir }}"

注意:这里的"{{}}"就是引用vars中main.yml定义的参数

如果参数不放在最开头,双引号就不用加,例如:

- name: 解释一下什么时候不需要用双引号
  unarchive: src={{ src_nginx }} dest={{ nginx_decompress_dir }}
- name: 又或者这种情况
  unarchive:
    src: /opt/{{ src_nginx }}
    dest: /usr/{{ nginx_decompress_dir }}
bianyi_and_install_nginx.yml

编译并且安装nginx

文件内容如下:

- name: 创建nginx用户
  shell: user_name=`cat /etc/passwd|grep nginx|wc -l` &&
         [ ${user_name} -eq 0 ] && useradd -s /sbin/nologin nginx || break
- name: 编译并安装nginx
  shell: cd {{ nginx_decompress_dir }} &&
         cd {{ nginx_after_decompress_name }} &&
         ./configure --user=nginx --group=nginx --prefix={{ nginx_install_dir }} &&
         make &&
         make install
config_nginx.yml

配置nginx文件

文件内容如下:

- name: 传输并配置nginx文件
  copy: src={{ src_nginx_conf }} dest={{ dest_nginx_conf }} backup=yes
  notify: restart
- name: 传输并配置include文件
  copy: src={{ src_nginx_include_conf }} desc={{ dest_nginx_include_conf }}
  notify: restart

注意:这里的notify对应的就是handles中main.yml的操作,notify后面的名字需要和handlers中main.yml的name名字对应

index_nginx.yml

配置nginx访问首页

文件内容如下:

- name: 传输index首页
  copy:
    src: "{{ src_nginx_index }}"
    dest: "{{ dest_nginx_index }}"
  notify: restart
config_enabled_start.yml

配置自启动

文件内容如下:

- name: 将自启动配置文件传输
  copy:
    src: "{{ nginx_service }}"
    dest: /usr/lib/systemd/system/
- name: 启动nginx服务,设置自启动
  service:
    name: nginx
    state: started
    enabled: yes
firewall_open_port.yml

开放相关端口

文件内容如下:

- name: 开放80、99、89端口
  firewalld:
    port: "{{ item }}"
    permanent: yes
    immediate: yes
    state: enabled
  with_items:
    - 80/tcp
    - 89/tcp
    - 99/tcp

整体校验是否通过

[root@master ansible-playbook-roles-nginx]# ansible-playbook -i host/hosts playbook-all-roles.yml --syntax-check

playbook: playbook-all-roles.yml

到playbook-all-roles.yml同级目录执行,没有报错就没有问题

执行

 ansible-playbook -i host/hosts playbook-all-roles.yml
  • 31
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值