ansible角色role实现编译安装nginx

文件准备如下:
tasks完成编译安装nginx的相关任务;files存放service文件及网站首页文件;handelers中存放config_file及service任务中更新完配置后的重启服务任务;templates中存放nginx配置文件及子配置文件的模板;vars中存放模板中需要的变量;需要提前准备的有:nginx源码压缩包、首页文件、service文件、nginx主配置文件模板、nginx子配置文件模板

/etc/ansible/roles/nginx/
├── files
│   ├── index.html
│   └── nginx.service
├── handlers
│   ├── main.yml
│   ├── reload.yml
│   └── restart.yml
├── tasks
│   ├── config_file.yml
│   ├── group.yml
│   ├── index_file.yml
│   ├── install_dependency.yml
│   ├── install_path.yml
│   ├── install.yml
│   ├── main.yml
│   ├── service.yml
│   ├── unarchive.yml
│   └── user.yml
├── templates
│   ├── nginx.conf.j2
│   └── virtual_host.conf.j2
└── vars
    └── main.yml

tasks

tasks/main.yml

[root@17 nginx]# cat tasks/main.yml 
---
- include: install_dependency.yml
- include: group.yml
- include: user.yml
- include: unarchive.yml
- include: install.yml
- include: index_file.yml
- include: service.yml
- include: config_file.yml
- include: install_path.yml

tasks/install_dependency.yml

[root@17 nginx]# cat tasks/install_dependency.yml 
- name: install dependency package for redhat
  yum: name={{ item }} state=present
  with_items:
  - pcre
  - pcre-devel
  - openssl
  - openssl-devel
  - zlib
  - zlib-devel
  - gcc-c++
  when: ansible_os_family == "RedHat"


- name: install dependency package for ubuntu
  apt: name={{ item }} state=present
  with_items:
  - pcre
  - pcre-devel
  - openssl
  - openssl-devel
  - zlib
  - zlib-devel
  - gcc-c++
  when: ansible_os_family == "Ubuntu"

tasks/group.yml

[root@17 tasks]# cat group.yml 
- name: create group nginx
  group: name=nginx state=present system=yes

tasks/user.yml

[root@17 tasks]# cat user.yml 
- name: create user nginx
  user: name=nginx system=yes shell=/sbin/nologin group=nginx

tasks/unarchive.yml

[root@17 tasks]# cat unarchive.yml 
- name: unarchive nginx source code
  unarchive: src=/usr/local/src/nginx-1.18.0.tar.gz dest=/usr/local/src/

tasks/install.yml

[root@17 tasks]# cat install.yml 
- name: install nginx
  shell: chdir=/usr/local/src/nginx-1.18.0 ./configure --prefix=/data/nginx;make;make install

tasks/index_file.yml

[root@17 tasks]# cat index_file.yml 
- name: copy index.html
  copy: src=index.html dest=/data/nginx/html/ backup=yes

tasks/service.yml

[root@17 tasks]# cat service.yml 
- name: copy service file
  copy: src=nginx.service dest=/usr/lib/systemd/system/
  notify: systemctl reload

tasks/config_file.yml

[root@17 tasks]# cat config_file.yml 
- name: update config file
  template: src=nginx.conf.j2 dest=/data/nginx/conf/nginx.conf
  notify: restart nginx
- name: create sub config file
  file: path=/data/nginx/conf.d/ state=directory
- name: update vhost config file
  template: src=virtual_host.conf.j2 dest=/data/nginx/conf.d/virtual_host.conf
  notify: restart nginx

tasks/install_path.yml

[root@17 tasks]# cat install_path.yml 
- name: change owner /data/nginx/
  file: path=/data/nginx/ owner=nginx group=nginx recurse=yes

handlers

handlers/main.yml

[root@17 handlers]# cat main.yml 
- include: restart.yml
- include: reload.yml

handlers/restart.yml

[root@17 handlers]# cat restart.yml 
- name: restart nginx
  service: name=nginx state=restarted

handlers/reload.yml

[root@17 handlers]# cat reload.yml 
- name: systemctl reload
  shell: systemctl reload

templates

templates/virtual_host.conf.j2

[root@17 templates]# cat virtual_host.conf.j2 
{% for vhost in vhosts %}
server{
  listen {{ vhost }};
  location / {
    root /data/nginx/html/;
  }
}
{% endfor %}

templates/nginx.conf.j2
略,需在最后include子配置文件

vars

vars/main.yml

[root@17 vars]# cat main.yml 
vhosts:
- 81
- 88

files

files/index.html

[root@17 files]# cat index.html 
<h1>base role nginx v2 </h1>

files/nginx.service

[root@17 files]# cat nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /data/nginx/logs/nginx.pid
ExecStartPre=/data/nginx/sbin/nginx -t
ExecStart=/data/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

role调用

/etc/ansible/nginx-role.yml

[root@17 ansible]# cat nginx-role.yml 
---
- hosts: 10.0.0.37
  remote_user: root
  roles:
  - nginx

部署

[root@17 ansible]# ansible-playbook nginx-role.yml

验证

[root@17 ansible]# curl 10.0.0.37
<h1>base role nginx v2 </h1>
[root@17 ansible]# curl 10.0.0.37:81
<h1>base role nginx v2 </h1>
[root@17 ansible]# curl 10.0.0.37:88
<h1>base role nginx v2 </h1>
上一篇:RHCE-B4. 使用 RHEL 系统角色软件包创建配置时间角色


下一篇:kolla-ansible运维