自动化运维工具ansible学习+使用ansible批量推送公钥到远程主机

目录:php

1、ansible简介html

1.一、ansible是什么node

1.二、ansible如何工做python

1.三、ansible优缺点linux

1.四、ansible安装方式git

1.五、ansible文件简单介绍github

1.六、ansible简单使用web

2、ansible-shell简介shell

2.一、ansible-shell是什么编程

2.二、ansible-shell安装方式

2.三、ansible-shell简单使用

3、使用ansible的authorized_key模块批量推送公钥到远程主机

3.一、生产秘钥对

3.二、使用ansible-playbook来生成推送公钥

4、相关站点的推荐

 


 

废话很少说,开始上干货。。。。大笑

1、ansible简介
1.一、ansible是什么

ansible是个什么东西呢?官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具。这个工具的目标有这么几项:让咱们自动化部署APP;自动化管理配置项;自动化的持续交付;自动化的(AWS)云服务管理。

 

 


 

下面这是摘自36氪的一段介绍,很详细:

 

AnsibleWorks成立于2012年,由自动化工具Cobbler及Func的开发者Michael DeHaan建立。其Ansible平台是一个开源的配置及计算机管理平台。可实现多节点的软件部署,执行特定任务并进行配置管理。

Ansible 跟其余IT自动化技术的区别在于其关注点并不是配置管理、应用部署或IT流程工做流,而是提供一个统一的界面来协调全部的IT自动化功能,所以 Ansible的系统更加易用,部署更快。受管理的节点无需安装额外的远程控制软件,由平台经过SSH(Secure SHell)对其进行管理,所以十分方便。其模块支持JSON等标准输出格式,可采用任何编程语言重写。

Ansible可让用户避免编写脚本或代码来管理应用,同时还能搭建工做流实现IT任务的自动化执行。IT自动化能够下降技术门槛及对传统IT的依赖,从而加快项目的交付速度。

目前,Ansible已有30万用户,每个月下载量接近3万次。其客户包括AppDynamics、Evernote 、MapR以及金融服务、电信、医疗保健、媒体业的财富500强公司。

 

免费版:http://www.ansibleworks.com/docs/

 

1.二、ansible如何工做:
Ansible默认是经过SSH通道来管理的,也就是它所说的免客户端方式管理, 它底层是经过 paramiko 来实现的。   
对Ansible底层工做机制感兴趣的能够参考这个:  http://www.the5fire.com/explore-the-ansible.html

 

ansible执行过程大致过程以下图,其中暖色调的表明已经模块化。

wKiom1N1faXDDKHyAAJ2qmuQi9E804.jpg

图片来源于:http://ju.outofmemory.cn/entry/67581

 

1.三、ansible优缺点

ansible有以下优势:       
1.3.一、轻量级,他不须要去客户端安装agent,更新时,只须要在操做机上进行一次更新便可        
1.3.二、批量任务执行能够写成脚本,并且不用分发到远程就能够执行        
1.3.三、使用python编写的,维护更简单        
1.3.四、支持sudo

缺点:       
1.3.五、对于几千台、上万台机器的操做,还不清楚性能、效率状况如何,须要进一步了解。        

 
1.四、ansible安装方式
1.4.一、安装前提

Control Machine Requirements

Currently Ansible can be from any machine with Python 2.6 installed (Windows isn’t supported for the control machine).

 

This includes Red Hat, Debian, CentOS, OS X, any of the BSDs, and so on.

 

Managed Node Requirements

On the managed nodes, you only need Python 2.4 or later, but if you are are running less than Python 2.5 on the remotes, you will also need:

 

python-simplejson

1.4.二、源码安装
1.4.三、pip安装
1.4.四、yum安装
1
2
3
4
[root@ansible ~]#
[root@ansible ~]# rpm -qa | grep ansible  # 检查是否安装
ansible-1.5.3-1.el6.noarch
[root@ansible ~]# yum install ansible -y  # 没有安装的话,使用这个安装便可

 

 
1.五、ansible组成介绍
1.5.一、包含文件介绍
1
2
3
4
5
6
[root@ansible ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg  # ansible的配置文件
└── hosts             # ansible的主仓库 用来存储须要管理的远程主机的相关信息
0 directories, 2 files
[root@ansible ~]#
1.5.二、ansible其余组件介绍

ansible-doc                模块文档查看器,颇有用,使用ansible -l 显示全部module列表,使用 ansible-doc  <module_x> 来查看module_x的详细文档,这里不作太多介绍

ansible-playbook    传说中的配置管理工具,若是你用过puppet或者saltstack,这个能够不用介绍了,若是你没用过,也不要紧,花费20分钟看下官方的 Video。 掌握一件事情最好的办法就是使用它,你能够下载官方的example来学习

ansible-galaxy         和三星不要紧,你能够把他理解成ansible的pip,能够从galaxy.ansible.com下载官方收录的playbooks

ansible-pull               支持直接从git下载playbook执行,须要遵循其规定的目录格式,用处不是特别大,能够不关注

ansible-vault            若是你的配置文件中含有敏感信息,你可能并不但愿他能被人看到,vault能够帮你加密/解密这个配置文件,高级用法,请参照http: //blog.ansibleworks.com/2014/02/19/ansible-vault/

1.5.三、ansible的  Inventory 介绍
ansible.cfg文件不用去管,咱们只须要配置hosts文件便可,hosts文件也就是官方说的Inventory,
 
hosts文件配置的几种格式:
单纯的写主机名或ip,所属组为all
1
2
#blue.example.com
#192.168.100.1
 
定义一个组
1
2
3
4
#[webservers]
#alpha.example.org
#beta.example.org
#192.168.1.100
组成员可使用通配符来匹配 www[001:006].example.com
 
若是你没有使用公钥,想要使用密码,你也能够这样写(适用于第一次登录控制)
格式:【主机名】 【主机地址】 【主机密码】  默认是root用户来进行的
1
2
3
[keepalived]
keepalived1  ansible_ssh_host=192.168.146.136 ansible_ssh_pass="test"
keepalived2  ansible_ssh_host=192.168.146.137 ansible_ssh_pass="test"

 

hosts文件设置的参数官方给出的:

1c2a7094-1c62-4a4a-98ef-8a6a8b0cfcb3

 

 

1.六、ansible的简单使用

1.6.一、上传公钥到远程主机

定义好了仓库,那么咱们就须要把控制端的公钥推送到远程主机上 (稍后会有讲到)

方法:一、使用expect+shell方式批量推送

方法:二、本身手动一个一个的推送

1.6.二、牛刀小试看看效果如何

好了,公钥都传到对方主机了,那么咱们来简单的使用来试用下

1
2
3
4
5
6
[root@ansible ~]# ansible keepalived -a "uptime"  # 默认使用command模块
keepalived1 | success | rc=0 >>
 13:07:24 up  7:59,  1 user,  load average: 0.00, 0.00, 0.00
keepalived2 | success | rc=0 >>
 13:07:31 up  7:58,  1 user,  load average: 0.00, 0.00, 0.00
[root@ansible ~]#
 

经常使用模块:

yum、service、shell、raw

yum模块经常使用来安装软件

service模块经常使用来对服务的开关操做

shell模块能够用来执行命令以及脚本

raw和command、shell相似,可是它能够传递管道

 

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
[root@ansible ~]# ansible keepalived -m raw -a "rpm -qa | grep xinetd"  # 使用raw模块查看远程服务器上是否安装了xinetd服务
keepalived1 | FAILED | rc=1 >>
keepalived2 | FAILED | rc=1 >>
[root@ansible ~]# ansible keepalived -m yum -a "name=xinetd state=latest"  # 使用yum模块进行安装yum源里最新版的xinetd软件
keepalived1 | success >> {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * epel: ftp.riken.jp\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package xinetd.x86_64 2:2.3.14-39.el6_4 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch            Version                    Repository     Size\n================================================================================\nInstalling:\n xinetd          x86_64          2:2.3.14-39.el6_4          base          121 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 121 k\nInstalled size: 259 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : 2:xinetd-2.3.14-39.el6_4.x86_64                              1/1 \n\r  Verifying  : 2:xinetd-2.3.14-39.el6_4.x86_64                              1/1 \n\nInstalled:\n  xinetd.x86_64 2:2.3.14-39.el6_4                                               \n\nComplete!\n"
    ]
}
keepalived2 | success >> {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * epel: mirrors.yun-idc.com\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package xinetd.x86_64 2:2.3.14-39.el6_4 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch            Version                    Repository     Size\n================================================================================\nInstalling:\n xinetd          x86_64          2:2.3.14-39.el6_4          base          121 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 121 k\nInstalled size: 259 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : 2:xinetd-2.3.14-39.el6_4.x86_64                              1/1 \n\r  Verifying  : 2:xinetd-2.3.14-39.el6_4.x86_64                              1/1 \n\nInstalled:\n  xinetd.x86_64 2:2.3.14-39.el6_4                                               \n\nComplete!\n"
    ]
}
[root@ansible ~]# ansible keepalived -m raw -a "rpm -qa | grep xinetd"  # 再次使用raw模块查看xinetd是否安装
keepalived1 | success | rc=0 >>
xinetd-2.3.14-39.el6_4.x86_64
keepalived2 | success | rc=0 >>
xinetd-2.3.14-39.el6_4.x86_64
[root@ansible ~]# ansible keepalived -m shell -a "service xinetd restart"   # 使用shell模块启动xinetd服务
keepalived2 | success | rc=0 >>
Stopping xinetd: [FAILED]
Starting xinetd: [  OK  ]
keepalived1 | success | rc=0 >>
Stopping xinetd: [FAILED]
Starting xinetd: [  OK  ]
[root@ansible ~]#
[root@ansible ~]# ansible keepalived -m service -a "name=xinetd state=restarted" # 一样你也可使用service模块进行服务的启动,关闭和重启
keepalived1 | success >> {
    "changed": true,
    "name": "xinetd",
    "state": "started"
}
keepalived2 | success >> {
    "changed": true,
    "name": "xinetd",
    "state": "started"
}
[root@ansible ~]#

 

 

 

NOTE:

   通俗点说,ansible执行管理的时候分两部分:

一、Ad-Hoc(咱们刚才1.6所演示的就是属于Ad-Hoc)

 

What’s an ad-hoc command?

An ad-hoc command is something that you might type in to do something really quick, but don’t want to save for later.

详情请查看:http://www.ansible.cn/docs/intro_adhoc.html

二、Playbooks

 

Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.

 

详情请查看:http://www.ansible.cn/docs/#playbooks

 


2、ansible-shell简介
2.一、ansible-shell是什么
ansible-shell是用python写出来的一个脚本,配合ansible使用的一个利器,可让你像在shell下同样使用命令。
 

项目地址:https://github.com/dominis/ansible-shell

 

2.二、ansible-shell安装方式

 

1
2
[root@ansible ~]#  git clone https://github.com/dominis/ansible-shell.git
[root@ansible ~]#  pip install -e ./ansible-shell

 

2.三、ansible-shell简单使用

 

2

 

ansible-shell内置的命令主要有四个:

cd :      切换到指定的组/表达式筛选的机器集合上

list:    显示目前的机器集合,list groups 能够列出全部的组(对咱们可能没啥用)

serial:  运行时的并发度,默认是2

help:    顾名思义,他能生成简单的模块帮助信息,方便即时查询

 

wKiom1N1f_vClWaQAAS97Kryses820.jpg

 

固然,ansible-shell的功能不止这些,这里只是作下简单的演示而已,咱们后期会继续更新相关内容

若是你python不错的话,能够在ansible-shell的基础上进行修改,更新,确保更加适合你的环境。

 


好 了,ansible和ansible-shell大体讲解了一下,“知道的越多,肩上的责任越重“,随之而来是你如何在一个很大的环境中来使用 ansible,由于ansible是依托于ssh的,难到你要手动的去ssh-copy-id到几十台,几百台甚至上千台机器上么,估计等你公钥都传完 了,早已通过了项目上线的时间,在这里咱们使用ansible来进行公钥的批量推送,如下几个是我我的思路,若是你有更好的方式请告知

一、shell 脚本 + expect

二、scp 脚本 + expect + 修改authorized_keys文件权限

三、ansible直接推送脚本

我的感受:

第一个坑:1和2两条的主要问题是,第一次ssh到远程主机上的时候,远程主机会对你进行公钥验证,因此这是一个坑,

第二个坑:针对scp上传以后,文件不对又是另一个坑

第三个坑:若是只是简单的使用expect的话,remote host主机密码要统一,不统一的话又是一个大坑

第四个坑:使用ansible推送公钥的话,第一次操做须要把远程主机的信息,帐户,密码都写到host文件中去,这是一项大的工程

废话很少说,咱们继续上干货

 

3、使用ansible的authorized_keys模块+yaml实现批量上传公钥

 

3.一、生成秘钥对
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''  # 生产密钥对

这个命令会产生一个公钥(~/.ssh/id_rsa.pub)和密钥(~/.ssh/id_rsa),

 -t dsa:表示使用密钥的加密类型,能够为'rsa'和'dsa'

 -P '':表示不须要密码登陆

 -f ~/.ssh/id_dsa:表示密钥存放的路径为${USER}/.ssh/id_dsa

ssh-copy-id -i ~/.ssh/id_rsa.pub username@[ip,hostname]  #若是你是单台机器的话,可使用这种方式把公钥文件传递到对方主机

 

3
PS:
   不少时候,一些童鞋使用scp来把公钥文件传递到远程主机上,而后再给cat到远程主机的 authorized_keys文件中,可是这种状况是属于你手动建立了authorized_keys文件,这个时候这个文件的权限跟你系统定义的umask是相关联的,因此也就致使了你已经上传公钥到远程主机可是依旧不能经过公钥来登陆远程主机。这是 authorized_keys文件权限问题致使的。

无图无真相,来看看咱们已经作好的一台ansible机器上来罗列远程主机上的authorized_keys的权限,若是你的机器不行,那就改成600便可

 

4

 

3.二、使用ansible-playbook来生成推送ymal文件

这里使用到了authoried_keys模块
估计背墙了,因此。。。。。

authoried_keys模块参数介绍

 

5 

 

修改ansible的hosts(Inventory)文件,以下图所示,格式:

[主机名] [ssh链接帐户] [ssh主机地址] [ssh链接帐户对应的密码]

 

6

 

下面演示咱们使用ansible-playbook来推送咱们写好的ymal文件

 

78

 

再次查看hosts文件内容

 

9

 

到了这里你能够对着大片的服务器说,管理大家so easy大笑,今后你不用在为不会except而发愁,解放你的双手,让你更从容,更快速便捷的来管理你的服务器群组!若是你会python,那就是如虎添翼了!

 

4、相关站点的推荐

国内一个团队把1.3的官方文档所有移过来了,今后你不再用担忧阅读官方文档被墙了,感谢他们。。。

对应官方的1.3版本的手册:http://www.ansible.cn/docs/

想要了解yaml的同窗能够移步到这里:

YAML的官方网站:http://www.yaml.org

 

后续主要针对ansible-playbook对线上环境的使用博客,敬请关注

参考文章:  
http://www.kankanews.com/ICkengine/archives/136988.shtml
http://www.shencan.net/index.php/2014/04/10/ansible-好***-ansible-shell/
https://github.com/dominis/ansible-shell
https://linuxtoy.org/archives/hands-on-with-ansible.html
https://github.com/ansible/ansible
http://www.the5fire.com/explore-the-ansible.html
http://ju.outofmemory.cn/entry/67581

分享到:  0
相关文章
相关标签/搜索