ansible-playbook-jinja2管理nginx配置文件

1. 案例1:创建jinja2的nginx的主配置文件
  1) 编写jinja2的nginx的主配置文件

 1 [root@test-1 jinja2]# vim /ansible/jinja2/test.yaml 
 2 [root@test-1 jinja2]# cat /ansible/jinja2/test.yaml 
 3 ---
 4 - hosts: web1
 5   vars:
 6    http_prot: 80
 7    server_name: test.scajy.cn
 8 
 9   tasks:
10     - name: copy nginx config file
11       template: 
12         src: site.j2  
13         dest: /etc/nginx/conf.d/site2.conf
14       notify: reload nginx
15 
16   handlers:
17     - name: reload nginx
18       service:
19         name: nginx
20         state: reloaded

  2) 创建nginx的jinja2的site.j2的配置文件

1 [root@test-1 jinja2]# cat /ansible/jinja2/site.j2 
2 server {
3     listen {{http_prot}};
4     server_name {{server_name }}};
5     location / {
6         root   /var/www/html;
7         index  index.html;
8     }
9 }

  3) 执行远程安装

 1 [root@test-1 jinja2]# ansible-playbook  test.yaml 
 2 
 3 PLAY [web1] *************************************************************************************************************************************************************
 4 
 5 TASK [Gathering Facts] **************************************************************************************************************************************************
 6 ok: [192.168.200.133]
 7 ok: [192.168.200.132]
 8 
 9 TASK [copy nginx config file] *******************************************************************************************************************************************
10 changed: [192.168.200.132]
11 changed: [192.168.200.133]
12 
13 RUNNING HANDLER [reload nginx] ******************************************************************************************************************************************
14 changed: [192.168.200.132]
15 changed: [192.168.200.133]
16 
17 PLAY RECAP **************************************************************************************************************************************************************
18 192.168.200.132            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
19 192.168.200.133            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

  4) 远程测试是否web1组里的是否正常

1 [root@test-1 jinja2]# curl 192.168.200.132 -H "Host:test.scajy.cn"
2 hello Ansible
3 [root@test-1 jinja2]# curl 192.168.200.133 -H "Host:test.scajy.cn"
4 hello Ansible

2. 案例2:创建jinja2的upstream反向代理nginx配置测试
  1) 编写jinja2的nginx的主配置文件

 1 [root@test-1 jinja2]# vim /ansible/jinja2/test.yaml 
 2 [root@test-1 jinja2]# cat /ansible/jinja2/test.yaml 
 3 ---
 4 - hosts: web1
 5   vars:
 6    http_prot: 80
 7    server_name: test.scajy.cn
 8 
 9   tasks:
10     - name: copy nginx config file
11       template: 
12         src: site.j2  
13         dest: /etc/nginx/conf.d/site2.conf
14       notify: reload nginx
15 
16   handlers:
17     - name: reload nginx
18       service:
19         name: nginx
20         state: reloaded

  2) 创建nginx的jinja2的site.j2的配置文件

 1 [root@test-1 jinja2]# vim site_upstream.j2 
 2 [root@test-1 jinja2]# cat site_upstream.j2 
 3 {% set list=['132','133'] %}
 4 upstream {{server_name}} {
 5   {% for i in list %}
 6   server 192.168.200.{{i}}:80;
 7   {% endfor%}
 8 
 9 }
10 
11 server {
12     listen {{http_prot}};
13     server_name {{server_name }};
14     location / {
15         root   /var/www/html;
16         index  index.html;
17     }
18 }

  3) ansible执行test.yaml文件,执行远程安装

 1 [root@test-1 jinja2]# ansible-playbook test.yaml 
 2 
 3 PLAY [web1] ******************************************************************************************************************************************************************************************************************************************************************
 4 
 5 TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
 6 ok: [192.168.200.132]
 7 ok: [192.168.200.133]
 8 
 9 TASK [copy nginx config file] ************************************************************************************************************************************************************************************************************************************************
10 changed: [192.168.200.132]
11 changed: [192.168.200.133]
12 
13 RUNNING HANDLER [reload nginx] ***********************************************************************************************************************************************************************************************************************************************
14 changed: [192.168.200.133]
15 changed: [192.168.200.132]
16 
17 PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
18 192.168.200.132            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
19 192.168.200.133            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

  4) 远程验证是否发布成功

 1 [root@test-1 jinja2]# ansible  web1 -m shell -a "cat /etc/nginx/conf.d/site2.conf "
 2 192.168.200.133 | CHANGED | rc=0 >>
 3 upstream test.scajy.cn {
 4     server 192.168.200.132:80;
 5     server 192.168.200.133:80;
 6   
 7 }
 8 
 9 server {
10     listen 80;
11     server_name test.scajy.cn;
12     location / {
13         root   /var/www/html;
14         index  index.html;
15     }
16 }
17 
18 192.168.200.132 | CHANGED | rc=0 >>
19 upstream test.scajy.cn {
20     server 192.168.200.132:80;
21     server 192.168.200.133:80;
22   
23 }
24 
25 server {
26     listen 80;
27     server_name test.scajy.cn;
28     location / {
29         root   /var/www/html;
30         index  index.html;
31     }
32 }
原文地址:https://www.cnblogs.com/scajy/p/11645769.html