22343213 发表于 2016-5-3 09:33:16

轻量级自动化运维工具ansible之一:介绍及初步使用

一、常见的自动化运维工具:
   OS Provisioning:PXE, Cobbler   OS Config:puppet, saltstack, chef, func   Task Exec:fabric, saltstack, func   Program Deployment:fabric   管理主机控制被管理节点的方式:   agent:被管理节点上需要安装代理程序以接受管理主机的操作;如puppet, func   agentless:被管理节点不需安装代理程序,管理主机借助ssh传递操作指令,如ansible, fabric;
二、ansible介绍

   ansible是一款基于python开发,揉合了众多自动化运维工具功能的轻量级自动化运维工具,目前实现了除系统安装以外的批量系统配置、批量任务执行及批量程序部署等功能。
1、ansibler的架构
    ⑴ansible核心程序

    ⑵connection plugins:连接插件,负责和被控制端通信;
    ⑶host inventory:主机库,定义可控制的主机;
    ⑷modules:core modules、custom modules
    ⑸playbook:剧本,使用YAML编写的声明性的配置文件
    ⑹plugins:各种插件,完成日志记录、邮件等功能;
    ansible从host inventory中获取被控制的主机信息,通过connection plugins连接主机,调用指指定模块向被控制端发送操作指令。

2、ansible的特性
    ⑴高度模块化,借助模块完成各种任务
    ⑵agentless,即无需在被控制端安装agent
    ⑶默认基于ssh协议向被控制端发送操作指令
       ①基于密钥认证
       ②在inventory文件中指定账号和密码      
    ⑷一系列任务执行可写成剧本(playbook)
    ⑸具有幂等性:不会重复执行相同操作,比如不会重复安装软件

三、ansible安装配置
以下node1为ansible主机,node2、node3、node4为被控制主机
1、rpm安装(epel源)
   yum -y install ansible   #只需要在控制端安装
   主配置文件:/etc/ansible/ansible.cfg

   inventory:/etc/ansible/hosts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# yum -y install ansible

Installed:
ansible.noarch 0:1.9.4-1.el6                                                                                                                                             

Dependency Installed:
PyYAML.x86_64 0:3.10-3.1.el6               libyaml.x86_64 0:0.1.3-4.el6_6            python-babel.noarch 0:0.9.4-5.1.el6    python-crypto2.6.x86_64 0:2.6.1-2.el6   
python-httplib2.noarch 0:0.7.7-1.el6       python-jinja2.x86_64 0:2.2.1-2.el6_5      python-keyczar.noarch 0:0.71c-1.el6    python-pyasn1.noarch 0:0.0.12a-1.el6   
python-setuptools.noarch 0:0.6.10-3.el6    python-simplejson.x86_64 0:2.0.9-3.1.el6    sshpass.x86_64 0:1.05-1.el6         

Complete!

# rpm -ql ansible | less

/etc/ansible
/etc/ansible/ansible.cfg   #主配置文件
/etc/ansible/hosts   #host inventory
/etc/ansible/roles
/usr/bin/ansible   #主程序
/usr/bin/ansible-doc   #获取帮助信息
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook   #运行“剧本”的命令
/usr/bin/ansible-pull
/usr/bin/ansible-vault   #可把playbook加密存放
/usr/lib/python2.6/site-packages/ansible
...
# vim /etc/ansible/ansible.cfg
...


# some basic default values...

inventory      = /etc/ansible/hosts   #inventory文件路径
#library      = /usr/share/my_modules/
remote_tmp   = $HOME/.ansible/tmp
pattern      = *
forks          = 5   #执行任务时启动的并发线程数
poll_interval= 15
sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
transport      = smart
#remote_port    = 22   #远程被控节点的ssh端口
module_lang    = C
...
...





2、添加被控制主机
    vim /etc/ansible/hosts   
      ntp.iyunv.com   #不属于任何组的主机直接定义在文件中最上端
         #可将一批主机归于一个组
      www1.iyunv.com:2222   #若被控节点的ssh使用了非默认端口,可以在被控节点后标明
      www2.iyunv.com
      www.example.com   #可使用通配
      172.16.100.7
      
      db-.example.com

    ◆默认以root用户身份执行,如果是口令认证,则需要在任务执行时输入密码或在inventory中的被控制主机旁以参数的形式指明,例如:

       192.168.30.20 ansible_ssh_user=fedora ansible_ssh_pass=magedu
       192.168.30.13 ansible_ssh_pass=magedu
   基于口令认证当然比较麻烦,故通常配置ssh基于密钥认证
   inventory中还有一些可配置的参数,具体见官方文档

    ◆主机变量
   可以在inventory中定义主机时为其添加主机变量。例如:
      
       www1.iyunv.com http_port=80 maxRequestsPerChild=808
       www2.iyunv.com http_port=8080 maxRequestsPerChild=909
    ◆组变量
   组变量是指赋予给指定组内所有主机上的变量。例如:
      
       www1.iyunv.com
       www2.iyunv.com
      
       ntp_server=ntp.iyunv.com
       nfs_server=nfs.iyunv.com
    ◆组嵌套
   inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。例如:
      
       httpd1.iyunv.com
       httpd2.iyunv.com
      
       ngx1.iyunv.com
       ngx2.iyunv.com
      
       apache
       nginx
      
       ntp_server=ntp.iyunv.com
    注意:在inventory文件中定义的变量只能在playbook中使用,而ansible命令不支持


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# cd /etc/ansible/
# vim hosts    #inventory文件中有些配置示例,可参考
....
# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com   #可以是主机名
## blue.example.com
## 192.168.100.1    #也可是ip地址
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

##
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www.example.com

# Ex 3: A collection of database servers in the 'dbservers' group

##
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
...
# cp hosts hosts.bac




3、配置ssh基于密钥认证
      ssh-keygen -t rsa
      ssh-copy-id -i .ssh/id_rsa.pub root@192.168.30.20


4、ansible的几个命令:
    ⑴ansible-doc
       ansible-doc -l:列出所有模块
       ansible-doc [-s] MODULE_NAME:查看指定模块的用法
         -s:生成可以复制到playbook中以作修改的摘要;简而言之就是以简要形式显示模块用法

   常用模块:command, user, copy, cron, file, filesystem, group, hostname, ping, yum
            service, shell, script
       command模块并不支持shell变量和管道等,若想使用shell来执行,应调用shell模块
    ⑵ansible:执行任务
       ansible[-f forks][-m module_name] [-a args]
          -m module_name:指定调用的模块
          -a args:指定向模块传递的参数
          -f #:指定并发数         
          -k:默认基于密钥认证,使用该选项指定基于口令认证

       ◆host-pattern:
          all, *   #所有主机
          192.168.30.20, 192.168.30.*
          www.example.com, www.example.com:ftp.test.com
          websrvs   #组中的所有主机
          websrvs:dbsrvs   #两个组中的所有主机
          websrvs:!dbsrvs   #在websrvs不在dbsrvs的主机
          websrvs:&dbsrvs   #同时在websrvs和dbsrvs中的主机


5、ansible常用模块及其使用示例
    ①command:命令模块,默认模块,用于在远程执行命令;command模块并不支持shell变量和管道等,若想使用shell执行复杂命令,应调用shell模块
       例:ansible [-m command] all -a 'date'
    ②cron:周期性任务计划模块
       state:
         present:生成
         absent:移除
       例:ansible websrvs -m cron -a 'name="sync time" minute="*/3" job="/usr/sbin/ntpdate 172.16.100.1 &> /dev/null"'
    ③user:管理用户
       例:ansible websrvs -m user -a 'name=fedora password=加密串'
    ④copy:复制文件
       src=:指定本地源文件路径
       content=:取代src=,表示直接用此处指定的内容生成为目标文件内容
       dest:指定远程目标文件路径
       例:ansible websrvs -m copy -a 'src=/mine/ntp.conf dest=/etc/ntp.conf[ owner=root group=root mode=644 backup=yes]'
          ansible websrvs -m copy -a 'content="hello" dest=/tmp/test.ansible'    ⑤file:设定文件属性       path=:指定文件路径,可换成name或dest       创建符号链接:         src=:指明源文件         path=:指明符号链接文件路径       例:ansible websrvs -m file -a 'src=/tmp/test.ansible path=/tmp/test.link state=link'
          ansible websrvs -m file -a 'owner=fedra group=fedra mode=644 path=/tmp/test.ansible'    ⑥service:控制服务的运行状态       enabled=:是否开机自动启动,取值为true或false       state=:状态,取值有started, stopped, restarted       例:ansible websrvs -m service -a 'name=httpd state=started enabled=true'    ⑦shell:       ansible websrvs -m shell -a 'echo $TERM'    ⑧script:将本地脚本复制到远程主机并运行       例:ansible websrvs -m script '/root/adduser.sh'    ⑨ping:测试指定主机是否能连接    ⑩yum:管理程序包       name=:指明要安装或卸载的程序包,可带上版本号       state:present,latest表示安装;absent表示卸载       例:ansible all -m yum -a 'name=zsh'          ansible all -m yum -a 'name=zsh state=absent'    ⑾setup:收集远程主机的facs       每个被管理节点在接收并运行管理命令之前,会将本主机相关信息,如操作系统版本,ip地址,cpu数量等报告给ansible主机    其它模块:filesystem, group, hostname等
页: [1]
查看完整版本: 轻量级自动化运维工具ansible之一:介绍及初步使用