ansible 批量安装nginx
流程思路:先在一台主机上编译安装好nginx后->打包->ansible下发->启动
- ansible环境不再叙述,准备好正常环境即可。
- 创建对应目录
[root@k8s3 ~]$ cd /etc/ansible/
[root@k8s3 ansible]$ mkdir nginx_install
[root@k8s3 ansible]$ cd nginx_install/
[root@k8s3 nginx_install]$ mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
[root@k8s3 nginx_install]$ tree ./
./
└── roles # 定义角色目录,common为一些准备操作,install为安装nginx的操作
├── common
│ ├── files # 存放安装包的目录
│ ├── handlers # 存放当发生变更是要执行的操作,如修改配置文件、重启服务等
│ ├── meta # 存放说明信息,如说明角色依赖等信息
│ ├── tasks # 存放核心的任务配置文件
│ ├── templates # 存放配置文件、启动文件等模板文件
│ └── vars # 用来定义变量的目录
└── install
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
- 编译安装nginx:
[root@k8s3 ~]$ cd /usr/local/src/
[root@k8s3 ~]$ yum install -y pcre-devel zlib-devel gcc gcc-c++
[root@k8s3 src]$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@k8s3 src]$ tar xf nginx-1.8.0.tar.gz
[root@k8s3 src]$ cd nginx-1.8.0.tar.gz/
[root@k8s3 nginx-1.8.0]$ ./configure --prefix=/usr/local/nginx
[root@k8s3 nginx-1.8.0]$ make && make install
[root@k8s3 nginx-1.8.0]$ vim /etc/init.d/nginx # https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx
[root@k8s3 nginx-1.8.0]$ chmod 755 /etc/init.d/nginx
[root@k8s3 nginx-1.8.0]$ chkconfig --add nginx
[root@k8s3 nginx-1.8.0]$ chkconfig nginx on
[root@k8s3 nginx-1.8.0]$ cd /usr/local/nginx/conf/
[root@k8s3 conf]$ mv nginx.conf nginx.conf.bak
[root@k8s3 conf]$ vim nginx.conf # https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
[root@k8s3 conf]$ /usr/local/nginx/sbin/nginx -t
[root@k8s3 conf]$ /etc/init.d/nginx start
[root@k8s3 conf]$ netstat -tunlp | grep 80
- 打包
[root@k8s3 ~]$ cd /etc/ansible/nginx_install/roles/install/files/
[root@k8s3 files]$ tar czfP nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" /usr/local/nginx
# 这里一定要注意 打包出现的绝对路径和相对路径问题 参数P(大写)解压时也必定此参数 在压缩或解压文件时,当后面的备份目录使用绝对路径时,会提示“ tar: 从成员名中删除开头的“/” ”的错误
# exclude不包括的文件或目录
- 拷贝配置文件和启动文件到模板目录:
[root@k8s3 ~]$ cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@k8s3 ~]$ cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
- 安装一些依赖包:
[root@k8s3 ~]$ cd /etc/ansible/nginx_install/roles/
[root@k8s3 roles]$ cat common/tasks/main.yml
- name: Install initializtion require software
yum: name={{ item }} state=installed
with_items:
- zlib-devel
- pcre-devel
- 定义对应的变量
[root@k8s3 roles]$ cat install/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx
- 把ansible文件拷贝至目标主机:
[root@k8s3 roles]$ cat 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 zxfP /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
- 创建用户 ,启动服务 ,删除压缩包:
[root@k8s3 roles]$ cat install/tasks/install.yml
- name: Create Nginx User
user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
shell: /etc/init.d/nginx start
- name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
- 创建 main.yml ,去调用 copy.yml 和 install.yml:
[root@k8s3 roles]$ cat install/tasks/main.yml
- include: copy.yml
- include: install.yml
- 最后,创建总入口文件:
[root@k8s3 ~]$ cat /etc/ansible/nginx_install/install.yml
---
- hosts: 192.168.3.42
remote_user: root
gather_facts: True
roles:
- common
- install
- 执行ansible playbook安装nginx
[root@k8s3 ~]$ ansible-playbook /etc/ansible/nginx_install/install.yml