98.playbook管理配置文件

24.29/24.30 playbook管理配置文件

 

 

 

24.29/24.30 playbook管理配置文件(部署及更改文件)

 

 

 

 

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

我们已经把nginx部署到机器上去了。后期我们会经常的去修改配置文件,加载等等。那么这时候就用到了管理配置文件。这个时候就会遇到一个问题,就是比如配置文件改错了,我想回滚一下,就是把原来的配置文件回滚回去并做一个加载:

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

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

关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致

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名字要保持一致

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上查看就恢复到之前变更的状态了

 

 

 

 

 

转载于:https://my.oschina.net/u/3866149/blog/3054541

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值