Go - Ansible Overview

Ansible overview:

With the continuous development of various computer virtualization technologies, the industrialization level of cloud computing is also becoming more and more mature. Under the new situation, IT operation and maintenance is faced with challenges from all aspects. The number of machines to be maintained ranges from tens of hundreds to thousands. The structure of the application becomes more and more complex, and the update speed also increases. Come faster. Various automated configuration management tools have also emerged in this ecological environment, such as puppet, saltstack, and ansible. This article will use ansible to specifically discuss its use in different scenarios, so that operators and developers can more easily deal with various configuration management and application deployment requirements.

  This article uses ansible deployment as an example to focus on the use of ansible as an automation tool in various environments, including the following aspects:

  Introduction to Ansible Tools

  Configuration management and application deployment automation

  Combined with jenkins continuous deployment automation

  summary

  1. Introduction to Ansible Tools

  1.1. Introduction

  The original quote from the official statement "ansible is a simple automated IT tool". Its purpose is: configuration management automation, application deployment automation, continuous delivery automation, and more. It is not fundamentally different from other configuration management tools. Like an integrated toolbox, it can remotely send commands to other servers for configuration management and application deployment, and can also expand modules through various programming languages ​​at any time. Its salient features are:

  Provides a simple and programmable process menu, which can perform related operations according to the pre-defined logic, and automatically complete the deployment requirements

  No need for clients or agents to manage servers remotely

  Complete all deployment tasks based on paramiko (ssh protocol library implemented in Python)

  1.2. How to use ansible and ansible-playbook

  Ansible command is a command line tool provided by ansible, which is used based on the hosts list, as follows:

  ansible <host group name> [-f number of concurrency] [-m module name] [-a args] -i host list name

  默认的Host 清单配置文件在/etc/ansible/hosts,也可以定制新的路径。内容格式与window下的.ini文件类似。

  #hosts

  [web]

  192.168.100.10

  192.168.100.11

  [db]

  192.168.100.11

  192.168.100.12

  这里定义了俩个组,服务器可以是名字也可以是IP地址,同一个服务器可以属于不同的组。

  用用户root向所有的服务器发ping命令,-k需要输入root密码,也可以现将所有服务器的密钥批量添加到本地,就可以无交互操作远程机器。

  $ ansible all -m ping -u root -k -i hosts

  SSH password:

  192.168.100.10 | success >> {

  "changed": false,

  "ping": "pong"

  }

  192.168.100.11 | success >> {

  "changed": false,

  "ping": "pong"

  }

  192.168.100.12 | success >> {

  "changed": false,

  "ping": "pong"

  }

  默认不指定-m参数的时候使用的module只是执行命令,不会用一些shell下的environment变量等设置

  copy|file--文件复制及文件属性设置

  $ansible dbservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"

  $ansible webservers -m file -a "dest=/srv/foo/test.txt mode=600 owner=test group=test"

  git--使用git指定服务器

  $ansible webservers -m git -a "repo=git://test.example.org/repo.git dest=/srv/myapp version=HEAD"

  service--对指定服务操作

  ansible webservers -m service -a "name=httpd state=started"

  命令行工具提供简单任务操作,实际部署应用常常需要很多操作。ansible-playbook则可以按照一定先后顺序和逻辑关系事先定义好所有操作,然后一次完成所有的部署任务。

  如下例部署apache服务:

  ---

  - hosts: webservers

  vars:

  http_port: 80

  max_clients: 200

  remote_user: root

  tasks:

  - name: ensure apache is at the latest version

  yum: pkg=httpd state=latest

  - name: write the apache config file

  template: src=/srv/httpd.j2 dest=/etc/httpd.conf

  notify:

  - restart apache

  - name: ensure apache is running

  service: name=httpd state=started

  handlers:

  - name: restart apache

  service: name=httpd state=restarted

  说明:在所有的webservers服务上以root身份按照最新的httpd,用httpd.j2做模板配置新的服务,配置之后重启httpd服务。

  执行playbook命令即可完成所有部署,“-f 10”表示一次最多可同时在10个服务器上执行:

  $ ansible-playbook playbook.yml -f 10

  2. 配置管理和应用部署自动化

  简单介绍之后,我们对ansible的使用方式有了大概的了解。接下来将用几个实例来具体展示ansible在不同场景下的使用模式。

 

  2.1. 配置管理自动化

  在一个典型的Web实际应用中,我们需要配置Web服务器和Mysql 数据库服务器,如图一所示


▲图一 ansible配置web及数据库服务器结构图

  ansible目录结构如下:

  -- hosts

  -- site.yml

  `-- roles

  -- common

  -- handlers

  -- tasks

  `-- templates

  -- db

  -- handlers

  -- tasks

 `-- templates

  `-- web

  -- handlers

  -- tasks

  `-- templates

  具体hosts内容定义如下,web3作为web服务器,web2作为数据库服务器:

  [webservers]

  web3

  [dbservers]

  web2

  site.yml中定义如下,对所有hosts中的服务器以root用户执行common角色的操作;对所有hosts中的服务器以root用户执行common角色的操作:

  ---

  # This playbook deploys the whole application stack in this site.

  - name: apply common configuration to all nodes

  hosts: all

  user: root

  roles:

  - common

  - name: configure and deploy the webservers and application code

  hosts: webservers

  user: root

  roles:

  - web

  - name: deploy MySQL and configure the databases

  hosts: dbservers

  user: root

  roles:

  - db

  在对应role的目录下,需要组织针对不同的role准备相应的handler、task、template内容,比如时间服务器、mysql和apache的配置文件放到相应的templates目录下。详细的参考代码请参考附录的链接,这里就不再一一列举。所有准备就绪之后,执行如下命令,所有配置部署操作将由ansible自动完成:

  #ansible-playbook -i hosts site.yml

  2.2. 应用部署自动化

  基础环境部署好之后,接下来我们可以在这个环境上部署web应用。通常情况下,web应用部署都是用命令远程同步到web服务器,然后重启服务。利用ansible,我们可以将这个步骤自动化,如下图所示:


▲图二 ansible自动部署web应用

  在编译工具把代码编译成发布包以后,我们可以通过执行ansible-playbook将包部署到web服务器上,并自动重启服务。

  比如下面deploy.yml

  - hosts: webservers

  user: root

  pre_tasks:

  - name: Stop web service

  action: service name=httpd state=stopped

  - name: Deploy the code from repository

  git: repo=git://foosball.example.org/path/repo.git version=release-0.22 dest=/var/www/html/

  post_tasks:

  - name: Start web service

  action: service name=httpd state=started

  执行命令自动完成部署:

  #ansible-playbook -i hosts deploy.yml

  3. 结合jenkins持续部署

  Jenkins作为一个持续集成的开源工具,一直在持续集成和持续部署领域被广泛使用。本例将结合jenkins讨论ansible在持续部署方面的使用方法。


▲图三 ansible结合jenkins持续部署

  如图三所示,将应用源代码和playbook及服务器配置文件存放在git服务器中,将需要部署的服务器在hosts中相应分成test,staging,production三个组。

  部署的时候,运维人员在ansible服务器上用git从git服务器中取playbook并执行部署,可以一次部署三个环境,也可以选择指定的环境部署;在环境准备好后,开发人员通过jenkins工具取代码,build并构建部署任务。用jenkins调用ansible并执行相应的部署playbook进行自动应用部署,这里的playbook可以定义先判断在test环境是否部署正常,如正常则把当前应用部署到staging环境,否则将test环境回退到之前状态,同样情况在每次应用更新的时候也适用。具体详细的配置可以参照ansible官方文档,这里不一一列举。

  利用这种方式,开发人员将专注于代码的开发设计而不用每次变更的时候要花时间关心在各种部署操作;运维人员也会将更多的精力投入到playbook及服务器配置模板的维护中来,而不是频繁忙于各种服务器的登陆,安装,部署中。

  4. 小结

  本文以具体实例介绍了ansible使用方法,并着重说明了ansible在不同场景下的自动化部署功能,也对持续部署进行了探索性讨论。旨在为开发和运维团队在面对越来越复杂的各种环境中,可以多一些便捷的选择方案。(作者简介:曲家富 任职于某大型外资IT企业,从事系统管理工作,专注于系统自动化部署。)

  参考资料:

  Ansible Official Site

  http://www.ansible.com/home

  Ansible example resource link

  https://github.com/ansible/ansible-examples

  Zero Downtime Application Updates with Ansible

  http://radar.oreilly.com/2013/07/zero-downtime-application-updates-with-ansible.html

      Reprinted from: http://cio.it168.com/a2014/1014/1673/000001673109_all.shtml

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326804334&siteId=291194637
Recommended