最近在学ansible-playbook,然后就想要自己写一个脚本去测试,选择了nginx进行测试,就是编写ansible-playbook在远程主机上面部署nginx
主要参考的是ansible部署nginx,该文章是在本机编译好后,再将文件copy到目标主机处,我想着把从nginx下载的压缩包推送到目标主机。然后在目标主机处进行解压和编译安装
- ansible-playbook文件结构
大部分都是参考引用的文章的文件结构,在这里还是贴出自己的结构
1)这里是总目录,nginx_install是参考引用文章的脚本,nginx_install_new则是自己创的,在目标主机进行编译的一个playbook
[root@iZwz9jbs6mikeqqelfuxbhZ ansible]# ll
total 16
-rw-r--r-- 1 root root 799 Mar 16 22:20 ansible.cfg
-rw-r--r-- 1 root root 57 Apr 2 08:47 hosts
drwxr-xr-x 3 root root 4096 Apr 16 10:47 nginx_install
drwxr-xr-x 3 root root 4096 Apr 16 15:53 nginx_install_new
2)
[root@iZwz9jbs6mikeqqelfuxbhZ nginx_install_new]# ll
total 8
-rw-r--r-- 1 root root 145 Apr 16 15:53 install.yml
drwxr-xr-x 4 root root 4096 Apr 16 17:13 roles
3)
[root@iZwz9jbs6mikeqqelfuxbhZ nginx_install_new]# cd roles/
[root@iZwz9jbs6mikeqqelfuxbhZ roles]# ls
common install
4)common的结构(tree工具可以用yum -y install tree 来安装)
[root@iZwz9jbs6mikeqqelfuxbhZ roles]# tree common/
common/
├── files
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
5)install的结构
[root@iZwz9jbs6mikeqqelfuxbhZ install]# tree
.
├── files
│ ├── configure.sh
│ └── nginx.tar.gz(这里放的是nginx官网下载的nginx-1.9.7)
├── handlers
├── meta
├── tasks
│ ├── copy.yml
│ ├── install.yml
│ └── main.yml
├── templates
│ ├── nginx(本机的/etc/init.d/nginx 文件,用于开机自启动)
│ └── nginx.conf
└── vars
└── main.yml
- 接下来是我写的各种yml文件
1)/common/tasks/main.yml
#该文件是/common/tasks/main.yml
#common/tasks的总入口配置文件
#该文件是安装nginx之前,需要装的以来,with_item的格式还坑了我一下,百度了一下,才找到正式的格式
---
#- hosts: nginx
# remote_user: root
# tasks:
- name: install initialization require software
yum:
name: "{{ item }}"
state: installed
with_items:
- zlib
- zlib-devel
- pcre
- pcre-devel
- openssl
- openssl-devel
2)/install/tasks/copy.yml
- name: copy nginx software
copy: src=/etc/ansible/nginx_install_new/roles/install/files/nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
shell: tar zvxf /tmp/nginx.tar.gz -C /usr/local/
#本来是打算将解压后的文件改名为nginx,可是如果改名后,编译的时候会报错
#- name: change nginx filename
# shell: mv /usr/local/nginx-1.9.7 /usr/local/nginx
#编译的工作,写成了一个shell文件,运用script去执行这个shell
#- name: open nginx file and configure
# shell: cd /usr/local/nginx;./configure--user=root --group=root --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module --with-pcre;make&& make install
- name: configure the installation
script: /etc/ansible/nginx_install_new/roles/install/files/configure.sh
#- name: configure nginx
# shell: sh /usr/local/nginx/configure
#- name: make nginx
# shell: make
#- name: make install nginx
# shell: make install
- name: copy nginx start ssh
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
3)/install/tasks/install.yml
- name: create nginx user
user: name={{nginx_user}} state=present createhome=no shell=/sbin/nologin
#用service和systemctl开启服务,都会报错,就使用这个配置文件开启后,就不报错了
- name: Start Nginx Service
# service: name=nginx state=started
# shell: systemctl start nginx
shell: /etc/init.d/nginx start
- name: Add Boot Start Nginx Service
# shell: chkconfig --level 345 nginx on
shell: systemctl enable nginx
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz
4)/install/tasks/main.yml
#编写整个程序的总入口配置文件
- include: copy.yml
- include: install.yml
5)/install/vars/main.yml
#install/vars的总入口配置文件
nginx_user: root
nginx_basedir: /usr/local/nginx-1.9.7
6 ) /install/files/configure.sh
#!/bin/bash
cd /usr/local/nginx-1.9.7;
./configure --user=root --group=root --prefix=/usr/local/nginx-1.9.7 --conf-path=/usr/local/nginx-1.9.7/conf/nginx.conf ;
# --with-http_stub_status_module --with-http_ssl_module --with-pcre
#./configure
make & make install
##我在make & make install 的时候,报错了。重新再编译一次就好了
if ["$?" -ne 0 ];
then
./configure --user=root --group=root --prefix=/usr/local/nginx-1.9.7 --conf-path=/usr/local/nginx-1.9.7/conf/nginx.conf ;
make & make install;
fi
以上就是使用ansible-playbook成功安装和编译nginx的流程和文件,一路磕磕碰碰,百度了很多,在此记录一下