一、安装和配置ansible
开启yum的epel源,安装ansible
yum -y install ansible
配置
# 添加被控端的IP
vim /etc/ansible/hosts
[all]
192.168.30.10[1:4]
# 实现主控端和被控端基于key的登录验证
ssh-keygen
copy_id_main.sh
拷贝key的脚本
copy_id.exp,这个脚本需要安装expect软件包
#!/usr/bin/expect
set ip [lindex $argv 0]
set timeout 10
spawn ssh-copy-id root@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "centos\n" }
}
expect eof
copy_id_main.sh
#!/bin/bash
hosts=$(ansible all --list-hosts |egrep -o '([0-9]+\.){3}[0-9]+')
for ip in $hosts;do
copy_id.exp $ip
done
二、编写playbook
组织角色目录
mkdir /root/.ansible/roles
cd /root/.ansible/rolse
mkdir -pv nginx1142/{files,tasks,templates,handlers}
准备文件
cd files
wget http://nginx.org/download/nginx-1.14.2.tar.gz
编写任务
# 1.
vim creat_installation_dirs.yaml
- name: "create installation dirs"
file: path=/app/{{ item }} state=directory
with_items:
- nginx
- srcs
- name: "etc dir"
file: path=/etc/nginx/conf.d state=directory
- name: "data dir"
file: path=/data/nginx/html state=directory
# 2.
vim prepare_source_code.yaml
- name: "copy and unpack source tar ball"
unarchive: src=nginx.tar.gz dest=/app/srcs copy=yes
- name: "rename source dir"
shell: mv /app/srcs/nginx-1.14.2 /app/srcs/nginx
# 3.
vim install_dependecies.yaml
- name: "install dependencies for building"
yum: name={{ packages }} state=latest
vars:
packages:
- pcre-devel
- openssl-devel
- zlib-devel
# 4.
vim create_user.yaml
- name: "create user for nginx"
user: name=nginx state=present system=yes shell=/sbin/nologin
# 5.
vim build.yaml
- name: configure
shell: ./configure --prefix=/app/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
args:
chdir: /app/srcs/nginx
- name: "make"
shell: make && make install
args:
chdir: /app/srcs/nginx
# 6.
vim copy_templates.yaml
- name: "copy the main configure file"
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: "copy enviroment var"
template: src=nginx.sh.j2 dest=/etc/profile.d/nginx.sh
- name: "copy error page"
template: src={{ item.src }} dest=/data/nginx/html/{{ item.dest }}
with_items:
- { src: 404.html.j2, dest: 404.html }
- { src: 50x.html.j2, dest: 50x.html }
# 7.
vim read_nginx_variables
- name: "source nginx.sh"
shell: source /etc/profile.d/nginx.sh
# 8.
vim start_service.yaml
- name: "start nginx"
shell: nginx
# 9.
vim delete_source.yaml
- name: "clean src dir"
shell: /bin/rm -rf /app/srcs/nginx*
# 10.
vim main.yaml
- include: delete_source.yaml
- include: creat_installation_dirs.yaml
- include: prepare_source_code.yaml
- include: install_dependecies.yaml
- include: create_user.yaml
- include: build.yaml
- include: copy_templates.yaml
- include: read_nginx_variables.yaml
- include: start_service.yaml
- include: delete_source.yaml
调用角色
---
- hosts: all
remote_user: root
roles:
- role: nginx1142
最后的目录
tree nginx1142/
nginx1142/
|-- files
| `-- nginx.tar.gz
|-- handlers
|-- tasks
| |-- build.yaml
| |-- copy_templates.yaml
| |-- creat_installation_dirs.yaml
| |-- create_user.yaml
| |-- delete_source.yaml
| |-- install_dependecies.yaml
| |-- main.yaml
| |-- prepare_source_code.yaml
| |-- read_nginx_variables.yaml
| `-- start_service.yaml
`-- templates
|-- 404.html.j2
|-- 50x.html.j2
|-- nginx.conf.j2
`-- nginx.sh.j2
三、测试结果
检查
ansible-playbook -C ~/.ansible/install_nginx_from_source.yaml
运行
ansible-playbook ~/.ansible/install_nginx_from_source.yaml
参考文章
Ansible使用playbook自动化编译安装NginxCentOS 6.10源码编译及使用ansible编译安装httpd2.4.39