ansible - handlers 변경 실행 작업

14785 단어
1. ansible - handlers 변경 작업  1) 플레이 북 을 작성 한 handlers 의 프로필
 1 [root@test-1 bin]# vim /ansible/nginx/bin/handlers.yaml 
 2 [root@test-1 bin]# cat /ansible/nginx/bin/handlers.yaml 
 3 ---
 4 - hosts: web1
 5   remote_user: root
 6   gather_facts: no
 7   
 8   tasks:
 9     - name: Copy nginx config file
10       copy:
11         src: /ansible/nginx/conf/site.conf
12         dest: /etc/nginx/conf.d/site.conf
13       notify: 
14         - reload nginx                                                          #           handlers,     name,        handlers,  handlers name      handlers
15   
16   handlers:                                                                           #            ,   service    nginx
17   - name: reload nginx
18     service:
19       name: nginx
20       state: reloaded 

2) 원래 서버 의 nginx 프로필 보기
 1 [root@test-1 bin]# ansible web1 -a "cat /etc/nginx/conf.d/site.conf"
 2 
 3 192.168.200.133 | CHANGED | rc=0 >>
 4 server {
 5     listen 80;
 6     server_name www.ctnrs.com;
 7     location / {
 8         root   /var/www/html;
 9         index  index.html;
10     }
11 }
12 
13 192.168.200.132 | CHANGED | rc=0 >>
14 server {
15     listen 80;
16     server_name www.ctnrs.com;
17     location / {
18         root   /var/www/html;
19         index  index.html;
20     }
21 }

3) nginx 프로필 수정
 1 [root@test-1 bin]# vim /ansible/nginx/conf/site.conf 
 2 [root@test-1 bin]# cat /ansible/nginx/conf/site.conf 
 3 server {
 4     listen 80;
 5     server_name test.scajy.cn;
 6     location / {
 7         root   /var/www/html;
 8         index  index.html;
 9     }
10 }

2. handlers 파일 이 올 바른 지 검증  1) handlers 파일 작성 검증
1 [root@test-1 bin]# ansible-playbook  --syntax-check  handlers.yaml   
2 
3 
4 playbook: handlers.yaml

3. handlers 파일 실행  1) 실행 파일
 1 [root@test-1 bin]# ansible-playbook    handlers.yaml   
 2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
 3 
 4 
 5 PLAY [web1] ******************************************************************************************************************************************************************************************************************************************************************
 6 
 7 TASK [Copy nginx config file] ************************************************************************************************************************************************************************************************************************************************
 8 changed: [192.168.200.133]
 9 changed: [192.168.200.132]
10 
11 RUNNING HANDLER [reload nginx] ***********************************************************************************************************************************************************************************************************************************************
12 changed: [192.168.200.132]
13 changed: [192.168.200.133]
14 
15 PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
16 192.168.200.132            : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
17 192.168.200.133            : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

4. 성공 적 으로 실행 되 었 는 지 검증  1) 파일 변경 여부 검증
 1 [root@test-1 bin]# ansible web1 -a "cat /etc/nginx/conf.d/site.conf"
 2 
3 4 192.168.200.133 | CHANGED | rc=0 >> 5 server { 6 listen 80; 7 server_name test.scajy.cn; 8 location / { 9 root /var/www/html; 10 index index.html; 11 } 12 } 13 14 192.168.200.132 | CHANGED | rc=0 >> 15 server { 16 listen 80; 17 server_name test.scajy.cn; 18 location / { 19 root /var/www/html; 20 index index.html; 21 } 22 }

  2) nginx 가 정상적으로 작 동 하 는 지 검증
 1 [root@test-1 bin]# ansible web1 -m shell -a "netstat -lntup|grep 80"
 2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
 3 
 4 192.168.200.133 | CHANGED | rc=0 >>
 5 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17881/nginx: master 
 6 
 7 192.168.200.132 | CHANGED | rc=0 >>
 8 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17792/nginx: master 
 9 
10 [root@test-1 bin]# ansible web1 -m shell -a "ps -ef |grep nginx"
11 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting
12 
13 192.168.200.132 | CHANGED | rc=0 >>
14 root     17792     1  0 15:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
15 nginx    18524 17792  0 20:41 ?        00:00:00 nginx: worker process
16 root     18735 18730 58 20:51 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx
17 root     18737 18735  0 20:51 pts/1    00:00:00 grep nginx
18 
19 192.168.200.133 | CHANGED | rc=0 >>
20 root     17881     1  0 15:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
21 nginx    18613 17881  0 20:41 ?        00:00:00 nginx: worker process
22 root     18825 18820 62 20:51 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx
23 root     18827 18825  0 20:51 pts/1    00:00:00 grep nginx

3) curl 아 날로 그 브 라 우 저 요청
1 [root@test-1 bin]# curl 192.168.200.133 -H "host:test.scajy.cn"
2 hello Ansible
3 [root@test-1 bin]# curl 192.168.200.132 -H "host:test.scajy.cn"
4 hello Ansible

좋은 웹페이지 즐겨찾기