使用ansible剧本编译安装nginx

目录结构

[root@hiwangqi ansible]# tree roles/nginx/
roles/nginx/
├── files
│   └── nginx-1.12.2.tar.gz
├── tasks
│   └── main.yml
├── templates
│   └── nginx.conf.j2
└── vars
    └── main.yml

files目录

nginx软件包

vars/main.yml

定义变量信息

[root@hiwangqi ansible]# cat roles/nginx/vars/main.yml 
tarball_name: nginx-1.12.2.tar.gz
nginx_configuration: nginx.conf.j2
nginx_dir: nginx-1.12.2

tasks/main.yml

剧本

# 安装依赖
- name: install dependent
  yum:
    name: "{{ item }}"
    state: installed
  with_items:
    - pcre
    - pcre-devel
    - openssl
    - openssl-devel

# 创建用户组      
- name: create group
  group:
    name: www
    
# 创建用户      
- name: create user
  user:
    name: www
    group: www
    createhome: no
    shell: /sbin/nologin

# 包名
- name: copy and extract nginx tabball
  unarchive:
    src: "{{ tarball_name }}"
    dest: /server/tools/

# 编译        
- name: ./configure
  command: ./configure --user=www --group=www --prefix=/www/server/{{ nginx_dir }} --with-http_stub_status_module --with-http_ssl_module
  args:
    chdir: /server/tools/{{ nginx_dir }}

- name: make
  command: make
  args:
    chdir: /server/tools/{{ nginx_dir }}
 
- name: make install    
  command: make install
  args:
    chdir: /server/tools/{{ nginx_dir }}

# 复制配置文件        
- name: modify nginx configuration
  template:
    src: "{{ nginx_configuration }}"
    dest: /www/server/{{ nginx_dir }}/conf/nginx.conf

# 创建软连接        
- name: make link
  file:
    src: /www/server/{{ nginx_dir }}
    dest: /www/server/nginx
    state: link

# 创建执行命令的软连接        
- name: make link bin
  file:
    src: /www/server/nginx/sbin/nginx
    dest: /usr/bin/nginx
    state: link

# 启动nginx       
- name: start nginx
  command: nginx

templates/nginx.conf.j2

nginx配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    ...省略下面内容