24.29/24.30 playbook管理配置文件linux
24.29/24.30 playbook管理配置文件(部署及更改文件)nginx
生产环境中大多时候是须要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面咱们来写个管理nginx配置文件的playbookshell
咱们已经把nginx部署到机器上去了。后期咱们会常常的去修改配置文件,加载等等。那么这时候就用到了管理配置文件。这个时候就会遇到一个问题,就是好比配置文件改错了,我想回滚一下,就是把原来的配置文件回滚回去并作一个加载:vim
1.mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}rest
其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令对象
关于回滚,须要在执行playbook以前先备份一下旧的配置,因此对于老配置文件的管理必定要严格,千万不能随便去修改线上机器的配置,而且要保证new/files下面的配置和线上的配置一致rem
2.先把nginx.conf和vhosts目录放到files目录下面部署
cd /usr/local/nginx/conf/同步
cp -r nginx.conf vhost /etc/ansible/nginx_config/roles/new/files/ !!#此处的vhost目录名字要跟第5步的核心任务里的vhost目录名保持一致。若是你的机器上是vhosts,在此处以及在第5步的vhosts名字要保持一致it
3.vim /etc/ansible/nginx_config/roles/new/vars/main.yml //定义变量
nginx_basedir: /usr/local/nginx
4.vim /etc/ansible/nginx_config/roles/new/handlers/main.yml //定义从新加载nginx服务
- name: restart nginx
shell: /etc/init.d/nginx reload
5.vim /etc/ansible/nginx_config/roles/new/tasks/main.yml //这是核心的任务 #就是把对应的而配置文件拷贝过去,而后加载handlers
- 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/ } #!!注意目录名不要写错,axin把vhost写成vhosts就报错了
notify: restart nginx
6.vim /etc/ansible/nginx_config/update.yml // 最后是定义总入口配置
---
- hosts: testhost
user: root
roles:
- new
7.执行: ansible-playbook /etc/ansible/nginx_config/update.yml
实例:
[root@axinlinux-01 ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
[root@axinlinux-01 ~]# cd /etc/ansible/nginx_config #这个nginx_config做为咱们的总目录(总项目)
[root@axinlinux-01 nginx_config]# cd roles/
[root@axinlinux-01 roles]# ls
new old #new用来安装,old用来回滚
[root@axinlinux-01 roles]# cd /usr/local/nginx/conf/
[root@axinlinux-01 conf]# cp -r nginx.conf vhost /etc/ansible/nginx_config/roles/new/files/ #拷贝目录要加-r
[root@axinlinux-01 conf]# vim /etc/ansible/nginx_config/roles/new/vars/main.yml #定义变量
nginx_basedir: /usr/local/nginx #就一个变量,其实就是basedir
[root@axinlinux-01 conf]# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml #定义从新加载
- name: restart nginx
shell: /etc/init.d/nginx reload
[root@axinlinux-01 conf]# 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
#以上,src是一个循环,dest是前面定义的变量,item.dest是一个循环。也就是item被分红两部分,一个是item.src一个是item.dest。
with_items:
- { src: nginx.conf, dest: conf/nginx.conf } #循环对象里面有两个子对象,用逗号分隔。这个就是把nginx.conf拷贝到/usr/local/nginx/conf/nginx.conf
- { src: vhosts, dest: conf/ } #这个同理,vhost目录拷贝到/usr/local/nginx/conf/目录下
notify: restart nginx #调用handlers(restart nginx),handlers去刚才定义的 /etc/ansible/nginx_config/roles/new/handlers/目录下的main.yml去找
[root@axinlinux-01 conf]# vim /etc/ansible/nginx_config/update.yml #总入口,ansible-playbook时的指定文件
---
- hosts: axinlinux-02 #能够为testhost(机器组)
user: root
roles:
- new
[root@axinlinux-01 conf]# ansible-playbook /etc/ansible/nginx_config/update.yml #执行。报错了
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option
failed: [axinlinux-02] (item={u'dest': u'conf/', u'src': u'vhosts'}) => {"changed": false, "item": {"dest": "conf/", "src": "vhosts"}, "msg": "Could not find or access 'vhosts'\nSearched in:\n\t/etc/ansible/nginx_config/roles/new/files/vhosts\n\t/etc/ansible/nginx_config/roles/new/vhosts\n\t/etc/ansible/nginx_config/roles/new/tasks/files/vhosts\n\t/etc/ansible/nginx_config/roles/new/tasks/vhosts\n\t/etc/ansible/nginx_config/files/vhosts\n\t/etc/ansible/nginx_config/vhosts on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
to retry, use: --limit @/etc/ansible/nginx_config/update.retry
PLAY RECAP *****************************************************************************************************************************************
axinlinux-02 : ok=1 changed=0 unreachable=0 failed=1
解决:
[root@axinlinux-01 conf]# 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: vhosts, dest: conf/ } #这里的vhost目录要跟一开始建立的目录名保持一致,应该为vhost
notify: restart nginx
[root@axinlinux-01 conf]# ansible-playbook /etc/ansible/nginx_config/update.yml #再次执行
[root@axinlinux-01 conf]# cd /etc/ansible/nginx_config/roles/new/files/ #咱们实验一下
[root@axinlinux-01 files]# ls
nginx.conf vhost
[root@axinlinux-01 files]# vim nginx.conf #把nginx.conf目录作一个变动
[root@axinlinux-01 files]# ansible-playbook /etc/ansible/nginx_config/update.yml #而后在执行一下
[root@axinlinux-02 ~]# cat /usr/local/nginx/conf/nginx.conf #而后去02机器查看一下
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24.29/24.30 playbook管理配置文件(回滚)
咱们在变动文件的时候,要把以前的配置作备份。就是把new下的文件拷贝到old下去。由于回滚是针对备份文件作回滚的:
1.rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
#首先咱们要把new下面的拷贝到old下面去,new是什么样的,old就要是什么样的
2.rsync -av /etc/ansible/nginx_config/roles/new/files /etc/ansible/nginx_config/roles/old/files #每次变动以前都要把new下的files拷贝到old下的files
回滚操做就是把旧的配置覆盖,而后从新加载nginx服务, 每次改动nginx配置文件以前先备份到old里,对应目录为/etc/ansible/nginx_config/roles/old/files
3.vim /etc/ansible/nginx_config/rollback.yml // 最后是定义总入口配置 #总入口文件名就为rollback.yml了
---
- hosts: testhost
user: root
roles:
- old
回滚的backup.yml对应的roles为old
实例:
[root@axinlinux-01 files]# rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/ #首先将new和old作一个同步
[root@axinlinux-01 old]# rsync -av /etc/ansible/nginx_config/roles/new/files /etc/ansible/nginx_config/roles/old/files #而后作一个备份,就是将new下的files拷贝到old下的files里面去
[root@axinlinux-01 old]# vim /etc/ansible/nginx_config/rollback.yml
---
- hosts: testhost
user: root
roles:
- old #此处为old。跟update不一样
[root@axinlinux-01 old]# vim /etc/ansible/nginx_config/roles/new/files/nginx.conf
## include vhost/*.conf; #咱们作个修改,将这一行加个注释
[root@axinlinux-01 old]# ansible-playbook /etc/ansible/nginx_config/update.yml #而后执行
[root@axinlinux-02 ~]# cat /usr/local/nginx/conf/nginx.conf 到02上查看就有了
[root@axinlinux-01 old]# ansible-playbook /etc/ansible/nginx_config/rollback.yml #而后咱们作回滚,直接执行rollback.yml就行了,他针对的就是咱们刚才备份的文件
[root@axinlinux-02 ~]# cat /usr/local/nginx/conf/nginx.conf #再去02上查看就恢复到以前变动的状态了