温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

七、用playbook安装Nginx、playbook管理配

发布时间:2020-02-27 07:01:30 来源:网络 阅读:729 作者:seventeen_ 栏目:系统运维

一、用playbook安装Nginx

思路:先在一台机器上编译安装好nginx、打包,然后再用ansible去下发

# cd /etc/ansible   进入ansible配置文件目录
# mkdir  nginx_install   创建一个nginx_install的目录,方便管理
# cd nginx_install
# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}

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


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

1、在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件。

2、安装好后,我们需要把nginx目录打包,并放到/etc/ansible/nginx_install/roles/install/files/下面,名字为nginx.tar.gz

3、启动脚本、配置文件都要放到/etc/ansible/nginx_install/roles/install/templates下面

nginx、启动脚本,nginx配置文件。

# ls /usr/local/nginx/
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@fuxi01 nginx_install]# ls /etc/init.d/nginx 
/etc/init.d/nginx
[root@fuxi01 nginx_install]# ls /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
编译好的nginx,启动脚本,配置文件。
然后打包:
# cd /usr/local/
[root@fuxi01 local]# tar czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhost" nginx/    //过滤掉nginx.conf和vhost目录不要。
[root@fuxi01 local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/   //移动到该目录下
[root@fuxi01 local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@fuxi01 local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/


定义common的tasks,nginx是需要一些依赖包的,用到了yum模块,安装zlib和pcre包。

# cd  /etc/ansible/nginx_install/roles
# vim  ./common/tasks/main.yml //内容如下
- name: Install initializtion require software
  yum: name={{ item }} state=installed
  with_items:
    - zlib-devel
    - pcre-devel

定义变量

为什么要把它放到模板里?

因为模板里支持变量的。

在部署的时候,可以根据机器环境的不同,定义不同的值。比如第一台机器定义的www,第二台机器定义的是Apache。左边是变量名,右边是变量值(www)。

# vim /etc/ansible/nginx_install/roles/install/vars/main.yml //内容如下
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx

首先要把所有用到的文档拷贝到目标机器

# 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=root mode=0755
- name: Copy Nginx Config
  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644

这里的nginx.tar.gz没有写绝对路径,因为copy模块默认就会在install/files目录下去找,template模块会到templates下面去找。


接下来会建立用户,启动服务,删除压缩包。(这就是一个总的yml。)

# 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: /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

说明:

模块user,变量nginx_user,在install/vars/main.yml中定义的,present已存在的,createhome是否创建用户的家目录。

创建用户,启动nginx服务,开机启动nginx服务,删除nginx的压缩包。


再创建main.yml并且把copy和install调用

# vim /etc/ansible/nginx_install/roles/install/tasks/main.yml //内容如下
- include: copy.yml
- include: install.yml

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

# vim /etc/ansible/nginx_install/install.yml  //内容如下
---
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

执行: ansible-playbook /etc/ansible/nginx_install/install.yml

执行这一步以前,一定要保证对方机器上是没有安装nginx的,执行成功后,到对方机器上查看nginx进程是这样的才为正确:

[root@yw03 ~]# ps aux |grep nginx
root       1127  0.0  0.0  45912  1284 ?        Ss   11月21   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     1128  0.0  0.2  48404  4168 ?        S    11月21   0:43 nginx: worker process
nobody     1129  0.0  0.2  48404  3912 ?        S    11月21   0:00 nginx: worker process
root       8061  0.0  0.0 112732   972 pts/0    S+   22:02   0:00 grep --color=auto nginx

思路图1:

七、用playbook安装Nginx、playbook管理配

思路图2:

七、用playbook安装Nginx、playbook管理配



二、playbook管理配置文件

生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面写一个管理nginx配置文件的playbook。

# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令。

关于回滚,就是改错了配置文件需要回滚到原来的配置文件,然后把对应的服务进行加载。需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致。

先把nginx.conf和vhosts目录放到files目录下面

# cd /usr/local/nginx/conf/
# cp -r nginx.conf vhost  /etc/ansible/nginx_config/roles/new/files/

# vim /etc/ansible/nginx_config/roles/new/vars/main.yml   //定义变量
nginx_basedir: /usr/local/nginx

# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml  //定义重新加载nginx服务
- name: restart nginx
  shell: /etc/init.d/nginx reload

# vim /etc/ansible/nginx_config/roles/new/tasks/main.yml //这是核心的任务
- name: copy conf file
  copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644
  with_items:
    - { src: nginx.conf, dest: conf/nginx.conf }
    - { src: vhost, dest: conf/ }
  notify: restart nginx
  说明:
  这里的循环对象有两个子对象。一个是src,一个是dest,item.src的只循环items里的src,item.dest的只循环items里的dest。
  将nginx.conf拷贝到/usr/local/nginx/conf/nginx.conf。
  notify,这里就是handlers,用的是restart nginx    在该文件内:/etc/ansible/nginx_config/roles/new/handlers/main.yml

# vim /etc/ansible/nginx_config/update.yml // 最后是定义总入口配置
---
- hosts: testhost
  user: root
  roles:
  - new

执行: ansible-playbook /etc/ansible/nginx_config/update.yml

当变更了某个文件内容时,再次执行此命令,只会对那一个变更的文件进行更新。

而回滚的backup.yml对应的roles为old

# rsync -av  /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/

回滚操作就是把旧的配置覆盖,然后重新加载nginx服务, 每次改动nginx配置文件之前先备份到old里,对应目录为/etc/ansible/nginx_config/roles/old/files

# vim /etc/ansible/nginx_config/rollback.yml // 最后是定义总入口配置
---
- hosts: testhost
  user: root
  roles:
  - old

顺序为:更改配置文件内容前,先rsync备份到old下,然后再对/new/files下的配置文件进行更改,更改后执行ansible-playbook update.yml,如果没问题,那就OK,如果有问题,再执行ansible-playbook rollback.yml回滚到前面的配置。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI