git地址
https://github.com/ledrsnet/my-ansible-example/tree/main/compile_nginx
目录结构
[root@maple-c8-n1 ansible]# tree
.
├── ansible.cfg
├── compile_nginx.yml
├── hosts
└── roles
└── nginx
├── files
├── handlers
│ └── main.yml
├── README.md
├── tasks
│ ├── compile.yml
│ ├── main.yml
│ ├── postCompile.yml
│ └── preCompile.yml
├── templates
│ └── nginx.service.j2
└── vars
└── main.yml
自定义变量
[root@maple-c8-n1 ansible]# cat roles/nginx/vars/main.yml
---
# vars file for nginx
pk_dir: /usr/local/src/
install_path: /apps/nginx/
pid_dir: "{{ install_path }}/run/"
packages:
- https://nginx.org/download/nginx-1.18.0.tar.gz
filenames:
nginx: "{{ packages[0].partition('.tar')[0]|basename }}"
task文件
#主task文件
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/main.yml
---
# tasks file for nginx
- include: preCompile.yml
- include: compile.yml
- include: postCompile.yml
#preCompile.yml
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/preCompile.yml
- name: install gcc..
yum: name="gcc,pcre-devel,openssl-devel,zlib-devel" state=present
when: ansible_distribution == "CentOS"
- name: apt install packages
apt: name="gcc,make,libapr1-dev,libaprutil1-dev,libpcre3,libpcre3-dev,libssl-dev,wget"
when: ansible_distribution != "CentOS"
- name: add user nginx
user: name=nginx shell=/sbin/nologin create_home=no
- name: download nginx src packages
unarchive: src={{ packages[0] }} dest={{ pk_dir}} copy=no
#compile.yml
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/compile.yml
- name: configure scripts
shell:
chdir: "{{ pk_dir }}{{ filenames.nginx }}"
cmd: ./configure --prefix={{ install_path }} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
- name: compile nginx
make:
chdir: "{{ pk_dir }}{{ filenames.nginx }}"
target: install
file: "{{ pk_dir }}{{ filenames.nginx }}/Makefile"
#postCompile.yml
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/postCompile.yml
- name: change app owner
file: path={{ install_path }} owner=nginx group=nginx recurse=true
- name: create soft link
file: src={{ install_path }}/sbin/nginx dest=/usr/sbin/nginx state=link
- name: copy nginx service file
template: src=nginx.service.j2 dest=/usr/lib/systemd/system/nginx.service
when: ansible_distribution == "CentOS"
- name: copy nginx service file
template: src=nginx.service.j2 dest=/lib/systemd/system/nginx.service
when: ansible_distribution != "CentOS"
- name: mkdir pid dir
file: path={{ pid_dir }} state=directory
- name: modify pid path in nginx.conf
lineinfile:
path: "{{ install_path }}/conf/nginx.conf"
regexp: '^#pid'
line: "pid {{ pid_dir }}/nginx.pid;"
- name: reload service daemon
shell: systemctl daemon-reload
- name: start Service
service: name=nginx state=started enabled=yes
模板文件
[root@maple-c8-n1 ansible]# cat roles/nginx/templates/nginx.service.j2
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile={{ install_path }}/run/nginx.pid
ExecStart={{ install_path }}/sbin/nginx -c {{ install_path }}/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
总调度文件
[root@maple-c8-n1 ansible]# cat compile_nginx.yml
---
- hosts: web
roles:
- nginx