ansible管理配置文件

一个可以管理nginx配置文件的playbook
1、首先,就像安装一样,需要创建一些目录,存放管理配置文件数据的目录

[root@ansible-01 ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

可以看到在roles下,分出了两个目录new和old
new:更新时用到的
old:回滚时用到的
在下面的目录
files下面为nginx.conf文件和vhosts目录
handlers为重启nginx服务的命令

2、接下来,把虚拟主机文件vhost目录和配置文件nginx.conf复制到files目录下
但是我的没有虚拟主机文件,所以需要创建一个vhosts

[root@ansible-01 ansible]# cd /usr/local/nginx/conf/
[root@ansible-01 conf]# mkdir vhosts
[root@ansible-01 conf]# cd vhosts/
[root@ansible-01 vhosts]# touch 1.conf

3、还要把虚拟主机目录写到配置文件
添加下面的
include /usr/local/nginx/conf/vhosts/*.conf;

[root@ansible-01 ]# vim /usr/local/nginx/conf/nginx.conf
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
  cation ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_scrip
t_name;
}
}
include /usr/local/nginx/conf/vhosts/*.conf;
}
"nginx.conf" 62L, 1526C 已写入

编写定义变量的文件

[root@ansible-01 conf]# cd cd /etc/ansible/nginx_config
[root@ansible-01 nginx_config]# vim roles/new/vars/main.yml
nginx_basedir: /usr/local/nginx
~                                                                
~                                                                                                                           
~                                                                                                                            
~                                                                
~                                                                
~                                                                
"roles/new/vars/main.yml" [] 1L, 32C 已写入  

编写重新加载nginx的文件

[root@ansible-01 nginx_config]#  vim roles/new/handlers/main.yml
- name: restart nginx
  shell: /etc/init.d/nginx reload
~                                                                                                                         
~                                                            
~                                                            
~                                                            
~                                                            
~                                                                                                            
~                                                            
~                                                            
<ew/handlers/main.yml" [] 2L, 56C 已写入 

核心任务,复制.conf 和hosts文件

[root@ansible-01 nginx_config]# vim roles/new/tasks/main.yml
- name: copy conf file
  copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.d
est }}backup=yes owner=root group=root mode=0644
  with_items:
    - { src: nginx.conf, dest: conf/nginx.conf }
    - { src: vhosts, dest: conf/ }
  notify: restart nginx
~                                                            
~                                                            
~                                                            
~                                                                                                                   
~                                                            
~                                                            
~                                                                                                                    
~                                                            
~                                                            
</new/tasks/main.yml" [] 6L, 255C 已写入 

更新文件

[root@ansible-01 nginx_config]# vim /etc/ansible/nginx_config/update.yml
---
- hosts: testhost
  user: root
  roles:
    - new
~                                                            
~                                                            
~                                                            
~                                                            
~                                                                                                                      
~                                                            
~                                                                                                              
~                                                            
~                                                            
"update.yml" [] 5L, 54C 已写入   

执行一下

[root@ansible-01 nginx_config]# ansible-playbook /etc/ansible/nginx_config/update.yml 

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

TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
ok: [10.30.59.216]

TASK [new : copy conf file] ****************************************************
changed: [127.0.0.1] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})
changed: [10.30.59.216] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})
changed: [127.0.0.1] => (item={u'dest': u'conf/', u'src': u'vhosts'})
changed: [10.30.59.216] => (item={u'dest': u'conf/', u'src': u'vhosts'})

  NNING HANDLER [new : restart nginx] ******************************************
changed: [127.0.0.1]
changed: [10.30.59.216]

PLAY RECAP *********************************************************************
10.30.59.216               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
127.0.0.1                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible-01 nginx_config]#

在执行update.yml前,应备份当前配置文件,当执行之后发现错误,则进行回滚操作。关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致,命令如下:

[root@ansible-01 nginx_config]# rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
sending incremental file list
files/
files/nginx.conf
files/vhosts/
files/vhosts/1.conf
handlers/
handlers/main.yml
tasks/
tasks/main.yml
vars/
vars/main.yml

sent 2,418 bytes  received 131 bytes  5,098.00 bytes/sec
total size is 1,869  speedup is 0.73
[root@ansible-01 nginx_config]# vim /etc/ansible/nginx_config/rollback.yml
---
- hosts: testhost
  user: root
  roles:
    - old
~                                                            
~                                                            
~                                                            
~                                                            
~                                                            
~                                                            
~                                                            
~                                                                                                                      
~                                                            
~                                                            
"rollback.yml" [] 5L, 54C 已写入         
[root@ansible-01 nginx_config]# ansible-playbook /etc/ansible/nginx_config/rollback.yml 

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

TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]
ok: [10.30.59.216]

TASK [old : copy conf file] ****************************************************
ok: [127.0.0.1] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})
ok: [10.30.59.216] => (item={u'dest': u'conf/nginx.conf', u'src': u'nginx.conf'})
ok: [127.0.0.1] => (item={u'dest': u'conf/', u'src': u'vhosts'})
ok: [10.30.59.216] => (item={u'dest': u'conf/', u'src': u'vhosts'})

PLAY RECAP *********************************************************************
10.30.59.216               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible-01 nginx_config]# 

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值