设为首页 收藏本站
查看: 2595|回复: 0

[经验分享] 轻量级自动化运维工具ansible之一:介绍及初步使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-5-3 09:33:16 | 显示全部楼层 |阅读模式
一、常见的自动化运维工具:
     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介绍
wKiom1ckdkuymoGJAAKh7B3vN2Q035.png

   ansible是一款基于python开发,揉合了众多自动化运维工具功能的轻量级自动化运维工具,目前实现了除系统安装以外的批量系统配置、批量任务执行及批量程序部署等功能。
  1、ansibler的架构
wKioL1ckfeKg2CzWAAdahMlM--E648.png
    ⑴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
[iyunv@node1 ~]# 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!

[iyunv@node1 ~]# 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
...
[iyunv@node1 ~]# vim /etc/ansible/ansible.cfg
...
[defaults]

# 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   #不属于任何组的主机直接定义在文件中最上端
      [websrvs]   #可将一批主机归于一个组
      www1.iyunv.com:2222   #若被控节点的ssh使用了非默认端口,可以在被控节点后标明
      www2.iyunv.com
      www[01:50].example.com   #可使用通配
      172.16.100.7
      [dbsrvs]
      db-[a:f].example.com

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

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

    ◆主机变量
     可以在inventory中定义主机时为其添加主机变量。例如:
       [websrvs]
       www1.iyunv.com http_port=80 maxRequestsPerChild=808
       www2.iyunv.com http_port=8080 maxRequestsPerChild=909
    ◆组变量
     组变量是指赋予给指定组内所有主机上的变量。例如:
       [websrvs]
       www1.iyunv.com
       www2.iyunv.com
       [websrvs:vars]
       ntp_server=ntp.iyunv.com
       nfs_server=nfs.iyunv.com
    ◆组嵌套
     inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。例如:
       [apache]
       httpd1.iyunv.com
       httpd2.iyunv.com
       [nginx]
       ngx1.iyunv.com
       ngx2.iyunv.com
       [websrvs:children]
       apache
       nginx
       [websrvs:vars]
       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
[iyunv@node1 ~]# cd /etc/ansible/
[iyunv@node1 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

## [webservers]
## 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[001:006].example.com

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

## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
...
[iyunv@node1 ansible]# 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] [options]
          -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等

wKioL1ckfeKg2CzWAAdahMlM--E648.jpg
wKiom1ckdkuymoGJAAKh7B3vN2Q035.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-211987-1-1.html 上篇帖子: ansible之file模块 下篇帖子: 运维自动化工具ansible学习笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表