ansible-初始playbook安装nginx

匿名 (未验证) 提交于 2019-12-02 22:10:10

1. ansible-初始playbook安装nginx

  1) 创建一个ansible存放路径

1 [root@test-1 scripts]# mkdir -p /ansible/nginx/{conf,bin}

  2) 验证存放路径

1 [root@test-1 bin]# tree /ansible/ 2 /ansible/ 3 └―― nginx 4     ├―― bin 5        └―― nginx.yaml 6     └―― conf 7         └―― site.conf 8  9 3 directories, 2 files

2. 编写playbook的nginx安装yaml文件

  1) 编写nginx.yaml配置文件

 1 [root@test-1 bin]# vim /ansible/nginx/bin/nginx.yaml  2 [root@test-1 bin]#cat /ansible/nginx/bin/nginx.yaml  3 - hosts: web1  4   remote_user: root  5   vars:  6     hello: Ansible  7    8   tasks:  9   - name: Add repo  10     yum_repository: 11       name: nginx 12       description: nginx repo 13       baseurl: http://nginx.org/packages/centos/7/$basearch/ 14       gpgcheck: no 15       enabled: 1 16   - name: Install nginx 17     yum: 18       name: nginx 19       state: latest 20   - name: Copy nginx configuration file 21     copy: 22       src: /ansible/nginx/conf/site.conf 23       dest: /etc/nginx/conf.d/site.conf 24   - name: Start nginx 25     service: 26       name: nginx 27       state: started 28   - name: Create wwwroot directory 29     file: 30       dest: /var/www/html 31       state: directory 32   - name: Create test page index.html 33     shell: echo "hello {{hello}}" > /var/www/html/index.html

  2) 编写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 www.ctnrs.com;  6     location / {  7         root   /var/www/html;  8         index  index.html;  9     } 10 }

3. 验证playbook是否正确

  1) 验证

1 [root@test-1 bin]# ansible-playbook /ansible/nginx/bin/nginx.yaml  --syntax-check 2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting 3  4  5 playbook: /ansible/nginx/bin/nginx.yaml

  提示:
       验证配置文件正确,没问题

4. 执行ansible-playbook远程安装

  1) 执行安装nginx

 1 [root@test-1 bin]# ansible-playbook /ansible/nginx/bin/nginx.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 [Add repo] ************************************************************************************************************************************************************************************************************************************************************** 10 ok: [192.168.200.132] 11 ok: [192.168.200.133] 12  13 TASK [Install nginx] ********************************************************************************************************************************************************************************************************************************************************* 14 changed: [192.168.200.132] 15 changed: [192.168.200.133] 16  17 TASK [Copy nginx configuration file] ***************************************************************************************************************************************************************************************************************************************** 18 ok: [192.168.200.132] 19 ok: [192.168.200.133] 20  21 TASK [Start nginx] *********************************************************************************************************************************************************************************************************************************************************** 22 changed: [192.168.200.133] 23 changed: [192.168.200.132] 24  25 TASK [Create wwwroot directory] ********************************************************************************************************************************************************************************************************************************************** 26 ok: [192.168.200.132] 27 ok: [192.168.200.133] 28  29 TASK [Create test page index.html] ******************************************************************************************************************************************************************************************************************************************* 30 changed: [192.168.200.133] 31 changed: [192.168.200.132] 32  33 PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************* 34 192.168.200.132            : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0    35 192.168.200.133            : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

5. 验证是否部署成功
  1) 验证

 1 [root@test-1 bin]# ansible   web1 -m shell -a "ps -ef |grep nginx"  2 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting  3   4 192.168.200.132 | CHANGED | rc=0 >>  5 root     17792     1  0 15:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf  6 nginx    17793 17792  0 15:04 ?        00:00:00 nginx: worker process  7 root     17970 17965 68 15:22 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx  8 root     17972 17970  0 15:22 pts/1    00:00:00 grep nginx  9  10 192.168.200.133 | CHANGED | rc=0 >> 11 root     17881     1  0 15:04 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 12 nginx    17882 17881  0 15:04 ?        00:00:00 nginx: worker process 13 root     18058 18053 73 15:22 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx 14 root     18060 18058  0 15:22 pts/1    00:00:00 grep nginx 15  16  17 [root@test-1 bin]# ansible   web1 -m shell -a "netstat -lntup |grep 80" 18 [WARNING]: log file at /var/log/ansible/ansible.log is not writeable and we cannot create it, aborting 19  20 192.168.200.132 | CHANGED | rc=0 >> 21 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17792/nginx: master  22  23 192.168.200.133 | CHANGED | rc=0 >> 24 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17881/nginx: master 

2) 模拟浏览器请求

1 [root@test-1 scripts]# curl 192.168.200.132 -H "Host:www.ctnrs.com" 2 hello Ansible 3 [root@test-1 scripts]# curl 192.168.200.133 -H "Host:www.ctnrs.com" 4 hello Ansible

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!