ddsdjn 发表于 2018-7-30 08:45:22

Ansible nginx管理配置文件

  l生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面我们来写个管理nginx配置文件的playbook
  lmkdir-p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
  l其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令
  l关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致
  l先把nginx.conf和vhosts目录放到files目录下面
  lcd /usr/local/nginx/conf/
  lcp -r nginx.conf vhosts/etc/ansible/nginx_conf/roles/new/files/
  lvim /etc/ansible/nginx_config/roles/new/vars/main.yml //定义变量
  lnginx_basedir: /usr/local/nginx
  lvim /etc/ansible/nginx_config/roles/new/handlers/main.yml//定义重新加载nginx服务
  l- name: restart nginx

  lshell: /etc/init.d/nginx>  lvim /etc/ansible/nginx_config/roles/new/tasks/main.yml //这是核心的任务
  l- name: copy conf file
  lcopy: src=` item`.`src ` dest=` nginx_basedir `/` item`.`dest ` backup=yes owner=root group=root mode=0644
  lwith_items:
  l    - { src: nginx.conf, dest: conf/nginx.conf }
  l    - { src: vhosts, dest: conf/ }
  lnotify: restart nginx
  lvim /etc/ansible/nginx_config/update.yml // 最后是定义总入口配置
  l---
  l- hosts: testhost
  luser: root
  lroles:
  l- new
  l执行: ansible-playbook /etc/ansible/nginx_config/update.yml
  l而回滚的backup.yml对应的roles为old
  lrsync -av/etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
  l回滚操作就是把旧的配置覆盖,然后重新加载nginx服务
页: [1]
查看完整版本: Ansible nginx管理配置文件