0.准备
安装ngixn源码包于主机的/usr/local下
1.创建ansible相关目录和配置文件
cd /etc/ansible/
#创建总目录
mkdir nginx_install
#建立角色目录
mkdir nginx_install/roles
#安装前准备工作目录
mkdir -p nginx_install/roles/common/tasks/
#创建安装主配置目录
mkdir nginx_install/roles/install
- #核心配置文件,分发资源和安装nginx
mkdir nginx_install/roles/install/tasks
#
存储由copy或script等模块调用的文件mkdir nginx_install/roles/install/files
#此目录中至少应该有一个名为main.yml的文件,用于定义各handler
#其它的文件需要由main.yml进行“包含”调用
mkdir nginx_install/roles/install/handlers
#
存储由template模块调用的模板文本;mkdir nginx_install/roles/install/templates
#此目录中至少应该有一个名为main.yml的文件,用于定义各variable;
#其它的文件需要由main.yml进行“包含”调用;
mkdir nginx_install/roles/install/vars
1.总安装文件(nginx_install/install.yml)
- vim /etc/ansible/nginx_install/install.yml
- hosts: web
remote_user: root
gather_facts: True
roles:
- common
- install
2.安装前准备的配置文件(roles/common)
vim /etc/ansible/nginx_install/roles/common/tasks/main.yml
- name: install initaliztion require software
yum: name={{ item }} state=installed
with_items:
- gcc
- zlib
- zlib-devel
- pcre
- pcre-devel
- openssl
- openssl-devel
3.安装主配置(roles/install)
1.task:核心配置文件,分发资源
1.main.yml #入口文件
vim /etc/ansible/nginx_install/roles/install/tasks/main.yml
- include: copy.yml
- include: install.yml
2.copy.yml #拷贝压缩包、解压缩、拷贝启动脚本、拷贝配置文件
vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml
- name: copy nginx software
copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: uncompression nginx software
shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: copy nginx start script
template: src=nginx dest=/etc/init.d/nginx owner=root group=ro
ot mode=0755- name: copy nginx config
template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
3.install.yml #创建运行用户、启动nginx系统服务、删除压缩文件
vim /etc/ansible/nginx_install/roles/install/tasks/install.yml
- name: create nginx user
user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: start nginx service
shell: service nginx restart
- name: add boot start nginx service shell: chkconfig nginx on
- name: delete nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
2.vars:定义变量
1.main.yml #定义变量运行用户、端口、安装目录
vim /etc/ansible/nginx_install/roles/install/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
3.files:存放nginx.tar.gz等文件
- cd /usr/local/
- tar -zcf nginx.tar.gz nginx/
cp nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
4.templates:模板配置文件,如启动脚本
- cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
5.handers:发生改变执行的操作,如配置文件改变,则重启
2.执行总配置文件
ansible-playbook install.yml
nginx ansible安装代码:https://github.com/msun1996/Ansible.git (更新中)