使用playbook安装nginx

2020-09-23 12:48:58 浏览数 (1)

笔记内容:使用playbook安装nginx 笔记日期:2018-01-31

  • 24.27/24.28 playbook安装nginx

playbook实战-nginx安装(1)

需求:虽然nginx可以直接使用yum安装,但是我想要的是使用源码包编译安装,并且需要自定义一些模块。而且我需要通过playbook下发到远程机器上。

实现思路:先在一台机器上编译安装好nginx、打包,也就是把这台机器作为模板机,然后再用ansible playbook去进行下发到远程机器上。

1.进入ansible配置文件目录,并创建一个nginx_install的目录,方便管理:

代码语言:javascript复制
[root@server ~]# cd /etc/ansible
[root@server /etc/ansible]# mkdir nginx_install
[root@server /etc/ansible]# cd nginx_install
[root@server /etc/ansible/nginx_install]# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
[root@server /etc/ansible/nginx_install]# ls roles/
common  install
[root@server /etc/ansible/nginx_install]# ls roles/install/
files  handlers  meta  tasks  templates  vars
[root@server /etc/ansible/nginx_install]# ls roles/common/
files  handlers  meta  tasks  templates  vars
[root@server /etc/ansible/nginx_install]# 

说明:

roles目录下有两个角色,common为一些准备操作,install为安装nginx的操作。每个角色下面又有几个目录,handlers下面是当发生改变时要执行的操作,通常用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates通常存一些配置文件,启动脚本等模板文件,vars下为定义的变量。


playbook实战-nginx安装(2)

1.需要事先准备好安装用到的文件,具体如下:

  • 在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件
  • 安装好后,我们需要把nginx目录打包,并放到/etc/ansible/nginx_install/roles/install/files/下面,名字为nginx.tar.gz
  • 启动脚本、配置文件都要放到/etc/ansible/nginx_install/roles/install/templates下面

nginx我这里早已编译安装好了:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install]# ls /usr/local/
apache2.4  apr-util  etc    include  lib    libexec  mysql  php   php-fpm  share  tomcat
apr        bin       games  jdk1.8   lib64  mariadb  nginx  php7  sbin     src
[root@server /etc/ansible/nginx_install]# ls /usr/local/nginx/
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp

所以我先在要做的事情就是把nginx目录打包,并放到files下面,以及把启动脚本、配置文件放到templates下面:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install]# cd /usr/local/
[root@server /usr/local]# tar -czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" nginx/
[root@server /usr/local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
[root@server /usr/local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@server /usr/local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
[root@server /usr/local]# 

2.完成以上的操作后,还需要定义common的tasks,因为nginx是需要一些依赖包的:

代码语言:javascript复制
[root@server /usr/local]# cd  /etc/ansible/nginx_install/roles
[root@server /etc/ansible/nginx_install/roles]# vim  ./common/tasks/main.yml
- name: Install initializtion require software
  yum: name="zlib-devel,pcre-devel" state=installed

playbook实战-nginx安装(3)

1.定义变量:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install/roles]# vim install/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx

2.然后要把所有用到的文档拷贝到目标机器:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install/roles]# vim install/tasks/copy.yml
- name: Copy Nginx Software   # 拷贝nginx包
  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software  # 解压nginx包
  shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script  # 拷贝nginx的启动脚本
  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config  # 拷贝nginx的配置文件
  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644

注:这里src参数指定的是相对路径,这个相对路径相对的是模块,例如copy模块里的src参数的值为nginx.tar.gz,那么就会去files目录下找nginx.tar.gz,而template模块则是会去templates目录下找。


playbook实战-nginx安装(4)

1.接下来是建立用户,启动服务,删除压缩包:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install/roles]# vim install/tasks/install.yml
- name: Create Nginx User  # 创建nginx用户
  user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service   # 启动nginx服务
  shell: /etc/init.d/nginx start
- name: Add Boot Start Nginx Service   # 将nginx服务添加到开机启动列表
  shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files   # 删除nginx的压缩包
  shell: rm -rf /tmp/nginx.tar.gz

注:nginx_user变量是之前我们在vars目录下的main.yml文件中定义的,所以在这里可以直接引用。


playbook实战-nginx安装(5)

1.再创建一个总体的main.yml,在这个文件里对copy.yml以及install.yml进行一个调用:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install/roles]# vim install/tasks/main.yml
- include: copy.yml
- include: install.yml

到此两个roles:common和install就定义完成了,接下来要定义一个作为总入口的配置文件:

代码语言:javascript复制
[root@server /etc/ansible/nginx_install/roles]# cd ../
[root@server /etc/ansible/nginx_install]# vim install.yml
---
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

注意:接着就是需要执行这个总入口文件了,但是如果你的目标机器之前安装过nginx需要先清理掉,不然可能会有冲突:

1、之前编译安装的清理办法:

find / -name "nginx*" -exec rm -rf {} ;

2、如果是之前yum安装的清理办法:

rpm -qa nginx yum remove -y nginx

执行这个总入口文件:

代码语言:javascript复制
[root@server ~]# ansible-playbook /etc/ansible/nginx_install/install.yml
[DEPRECATION WARNING]: The use of 'include' for tasks has been deprecated. Use 'import_tasks' for static inclusions or 
'include_tasks' for dynamic inclusions. This feature will be removed in a future release. Deprecation warnings can be 
disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: include is kept for backwards compatibility but usage is discouraged. The module documentation 
details page may explain more about this rationale.. This feature will be removed in a future release. Deprecation warnings 
can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [testhost] *************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.77.128]

TASK [common : Install initializtion require software] **********************************************************************
ok: [192.168.77.128]

TASK [install : Copy Nginx Software] ****************************************************************************************
changed: [192.168.77.128]

TASK [install : Uncompression Nginx Software] *******************************************************************************
 [WARNING]: Consider using unarchive module rather than running tar

changed: [192.168.77.128]

TASK [install : Copy Nginx Start Script] ************************************************************************************
changed: [192.168.77.128]

TASK [install : Copy Nginx Config] ******************************************************************************************
changed: [192.168.77.128]

TASK [install : Create Nginx User] ******************************************************************************************
changed: [192.168.77.128]

TASK [install : Start Nginx Service] ****************************************************************************************
changed: [192.168.77.128]

TASK [install : Add Boot Start Nginx Service] *******************************************************************************
changed: [192.168.77.128]

TASK [install : Delete Nginx compression files] *****************************************************************************
 [WARNING]: Consider using file module with state=absent rather than running rm

changed: [192.168.77.128]

PLAY RECAP ******************************************************************************************************************
192.168.77.128             : ok=10   changed=8    unreachable=0    failed=0   

[root@server ~]# 

执行成功后,到客户端上查看服务进程是否已启动,以及80端口是否已监听:

代码语言:javascript复制
[root@client ~]# ps aux |grep nginx
root       5130  0.0  0.0  45864  1100 ?        Ss   16:53   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     5131  0.0  0.1  50436  4076 ?        S    16:53   0:00 nginx: worker process
nobody     5132  0.0  0.1  50436  4076 ?        S    16:53   0:00 nginx: worker process
root       5365  0.0  0.0 112660   960 pts/0    S    16:53   0:00 grep --color=auto nginx
[root@client ~]# netstat -lntp |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5596/nginx: master  
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5596/nginx: master  
[root@client ~]# 

到此,我们就把nginx安装完成了。

最后上一张思维导图来总结一下,每个目录所包含的文件及.yml文件所要做的事情:

命令执行流程图:

0 人点赞