使用Ansible部署Nginx
2017-07-20 00:30
381 查看
基于学习的目的,演示使用ansible往远程linux服务器上部署nginx。
通常来讲,Ansible会将一般文件放在名为files的子目录中,将Jinja2模版文件放在名为templates的子目录中。
文件路径:./playbook/ansible-nginx/files
文件路径:./playbook/ansible-nginx/templates
创建playbook相关目录和文件
mkdir ansible-study mkdir -p ansible-study/files mkdir -p ansible-study/templates touch ansible-study/hosts touch ansible-study/web-notls.yml
创建主部署文件web-notls.yml
文件路径:./playbook/ansible-nginx- name: Configure webserver with nginx hosts: webservers sudo: True tasks: - name: Nginx Install rpm yum: name: http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm - name: Nginx install nginx yum: name: nginx state: latest - name: copy nginx config file copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default - name: copy index.html template: src: templates/index.html.j2 dest: /usr/share/nginx/html/index.html mode: 0644 - name: restart nginx service: name=nginx state=restarted
编辑一个nginx配置文件
运行Playbook之前,它还需要2个额外的文件,首先,需要定义个nginx的配置文件通常来讲,Ansible会将一般文件放在名为files的子目录中,将Jinja2模版文件放在名为templates的子目录中。
文件路径:./playbook/ansible-nginx/files
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html index index.html index.htm; server_name localhost; location / { try_files $uri $uri/ = 404; } }
创建一个定制的首页
添加一个定制的首页,用来测试Nginx的服务正常与否。文件路径:./playbook/ansible-nginx/templates
<html> <head> <title>Welcome to ansible</title> </head> <body> <h1>nginx,configured by Ansible</h1> <p>If you can see this,Ansible successfully installed nginx.</p> <p>{{ ansible_managed }}</p> </body> </html>
运行playbook,部署nginx
执行playbook需要使用ansible-playbook命令,按照如下方法运行playbookLS-MacBook-Pro:ansible-study wangzhen$ ansible-playbook web-notls.yml [DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. PLAY [Configure webserver with nginx] ********************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************* ok: [testserver] TASK [Nginx Install rpm] *********************************************************************************************************************************** ok: [testserver] TASK [Nginx install nginx] ********************************************************************************************************************************* ok: [testserver] TASK [copy nginx config file] ****************************************************************************************************************************** ok: [testserver] TASK [copy index.html] ************************************************************************************************************************************* changed: [testserver] TASK [restart nginx] *************************************************************************************************************************************** changed: [testserver] PLAY RECAP ************************************************************************************************************************************************* testserver : ok=6 changed=2 unreachable=0 failed=0
测试nginx
相关文章推荐
- 使用ansible结合keepalived高可用,nginx反向代理部署小型企业环境
- 部署 使用 acme.sh 给 Nginx 安装 Let’ s Encrypt 提供的免费 SSL 证书
- ansible部署nginx客户端
- ubuntu使用salt部署nginx
- 使用Ansible部署LAMP环境
- 【转】五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下)
- FastDFS的配置、部署与API使用解读(7)Nginx的FastDFS模块
- Django|Nginx|Uwsgi|Redis|Websocket配置、使用与部署
- FastDFS的配置、部署与API使用解读(7)Nginx的FastDFS模块
- Ansible-安装-秘钥-部署-使用
- 5.saltstack使用指南:使用saltstack部署nginx
- ubuntu下使用nginx部署Laravel
- 使用原子主机、Ansible 和 Cockpit 部署容器
- docker 使用案例:部署nginx
- 使用nginx + uwsgi socket的方式来部署Django项目
- ansible服务部署与使用
- ubunutu 12.04 下使用passenger+nginx 部署 rails应用
- 五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)
- centos7.0 使用Nginx部署flask应用教程