使用Ansible部署Nginx,ansible部署nginx


基于学习的目的,演示使用ansible往远程linux服务器上部署nginx。

创建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命令,按照如下方法运行playbook

LS-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

相关内容

    暂无相关文章