一、ansible简介 1、ansible是什么? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。 2、组织结构 ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架,如下图所示:
主要包括: 1)ansible core 2)连接插件connection plugins:负责和被监控端实现通信; 3)host inventory:管理操作的主机,是一个配置文件里面定义监控的主机; 4)各种模块核心模块、command模块、自定义模块; 5)playbook(yaml文件格式,jinjia2):剧本执行多个任务时,非必需可以让节点一次性运行多个任务。 6)借助于插件完成记录日志邮件等功能; 3、特性
基于python语言实现,由paramiko,PyYAML和Jinjia2三个关键模块 (1)、no agents:不需要在被管控主机上安装任何客户端; (2)、no server:无服务器端,使用时直接运行命令即可; (3)、modules in any languages:基于模块工作,可使用任意语言开发模块; (4)、yaml,not code:使用yaml语言定制剧本playbook; (5)、ssh by default:基于SSH工作; 1) 基于密钥认证 2)在inventory文件中指定帐号和密码 (6)、strong multi-tier solution:可实现多级指挥。 4、优点
(1)、轻量级,部署简单,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。 二、ansible安装 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [iyunv@localhost ~]# yum list|grep ansible
ansible.noarch 1.9.4-1.el6 epel
ansible-inventory-grapher.noarch 1.0.1-2.el6 epel
ansible-lint.noarch 2.0.1-1.el6 epel
[iyunv@localhost ~]# yum list|grep ansible
ansible.noarch 1.9.4-1.el6 epel
ansible-inventory-grapher.noarch 1.0.1-2.el6 epel
ansible-lint.noarch 2.0.1-1.el6 epel
[iyunv@localhost ~]# yum install ansible -y
[iyunv@localhost ~]# rpm -ql ansible|less
/etc/ansible
/etc/ansible/ansible.cfg #主配置文件
/etc/ansible/hosts #管理的主机,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
/usr/lib/python2.6/site-packages/ansible
|
三、ansible配置 1、/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
47
48
49
| [iyunv@localhost ~]# cd /etc/ansible/
[iyunv@localhost ansible]# ls
ansible.cfg hosts roles
[iyunv@localhost ansible]# cat hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
green.example.com
blue.example.com
192.168.100.1
192.168.100.10 #可以使用主机名和ip地址单独列在这里
# 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 #某种风格主机名的集合(从www001.example.com -->www006.example.com 6个主机)
# 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
# Here's another example of host ranges, this time there are no
# leading 0s:
db-[99:101]-node.example.com
[iyunv@localhost ansible]#
|
1
2
3
4
5
6
7
8
9
10
11
| [iyunv@localhost ansible]# cat hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
10.0.250.203
|
四、ansible命令使用用 1、查看模块帮助 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
| [iyunv@localhost ansible]# ansible-doc -h
Usage: ansible-doc [options] [module...]
Show Ansible module documentation
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-M MODULE_PATH, --module-path=MODULE_PATH
Ansible modules/ directory
-l, --list List available modules
-s, --snippet Show playbook snippet for specified module(s)
-v Show version number and exit
[iyunv@localhost ansible]# ansible-doc -s yum
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: M a n a g e s p a c k a g e s w i t h t h e I ( y u m ) p a c k a g
action: yum
conf_file # The remote yum configuration file to use for the transa
disable_gpg_check # Whether to disable the GPG checking of signatures of pa
disablerepo # `Repoid' of repositories to disable for the install/upd
enablerepo # `Repoid' of repositories to enable for the install/upda
list # Various (non-idempotent) commands for usage with `/usr/
name= # Package name, or package specifier with version, like `
state # Whether to install (`present', `latest'), or remove (`a
update_cache # Force updating the cache. Has an effect only if state i
|
2、使用语法 ansible <host-pattern> [-f forks] [-m module_name] [-a args] host-patten:对哪些主机生效
-f forks:启动的并发线程数 -m module_name:要使用的模块 -a args:模块特有的参数 3、ansible常用模块
1)command 命令模块 默认模块,用于在远程执行命令 1
2
3
4
5
6
7
8
9
10
11
| [iyunv@localhost ansible]# ansible 10.0.250.203 -m command -a "date"
10.0.250.203 | success | rc=0 >>
Mon Jan 18 15:33:22 CST 2016
[iyunv@localhost ~]# ansible all -m command -a "hostname -I"
10.0.250.203 | success | rc=0 >>
10.0.250.203 10.0.17.203
[iyunv@localhost ~]# ansible all -a "hostname -I"
10.0.250.203 | success | rc=0 >>
10.0.250.203 10.0.17.203
|
2)cron 计划任务 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| [iyunv@localhost ~]# ansible-doc -s cron
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: M a n a g e c r o n . d a n d c r o n t a b e n t r i e s .
action: cron
backup # If set, create a backup of the crontab before it is mod
cron_file # If specified, uses this file in cron.d instead of an in
day # Day of the month the job should run ( 1-31, *, */2, etc
hour # Hour when the job should run ( 0-23, *, */2, etc )
job # The command to execute. Required if state=present.
minute # Minute when the job should run ( 0-59, *, */2, etc )
month # Month of the year the job should run ( 1-12, *, */2, et
name= # Description of a crontab entry.
reboot # If the job should be run at reboot. This option is depr
special_time # Special time specification nickname.
state # Whether to ensure the job is present or absent. #安装或移除,默认安装咯
user # The specific user whose crontab should be modified.
weekday # Day of the week that the job should run ( 0-6 for Sunda
(END)
|
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
| [iyunv@localhost ~]# ansible 10.0.250.203 -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job of xj"'
10.0.250.203 | success >> {
"changed": true,
"jobs": [
"test cron job of xj"
]
}
[iyunv@localhost ~]# ansible 10.0.250.203 -a crontab -l
Usage: ansible <host-pattern> [options]
ansible: error: -l option requires an argument
[iyunv@localhost ~]# ansible 10.0.250.203 -a 'crontab -l'
10.0.250.203 | success | rc=0 >>
#Ansible: test cron job of xj
*/10 * * * * /bin/echo hello
[iyunv@localhost ~]# ansible 10.0.250.203 -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job of xj" stat=absent' #移除这个计划任务
10.0.250.203 | FAILED >> {
"failed": true,
"msg": "unsupported parameter for module: stat"
}
[iyunv@localhost ~]# ansible 10.0.250.203 -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job of xj" state=absent'
10.0.250.203 | success >> {
"changed": true,
"jobs": []
}
[iyunv@localhost ~]# ansible 10.0.250.203 -a 'crontab -l'
10.0.250.203 | success | rc=0 >>
|
3)user,group 用户,组管理 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [iyunv@localhost ~]# ansible 10.0.250.203 -m user -a 'name=test_user'
10.0.250.203 | success >> {
"changed": true,
"comment": "",
"createhome": true,
"group": 502,
"home": "/home/test_user",
"name": "test_user",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 502
}
[iyunv@localhost ~]# ansible 10.0.250.203 -m group -a 'name=mysql gid=306 system=yes'
10.0.250.203 | success >> {
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": true
}
|
4)copy:复制或新建文件
src=: 定义本地源文件路径可以是相对路径和绝对路径
dest=:定义远程目标文件路径 只能是绝对路径 content=: 取代 (新建文件内容)
5、任务执行流程
说明: (1)、以上内容大多是基于他人分享的基础上总结而来,学习借鉴之用; (2)、本次安装基于 CentOS 6.4 系统环境。
|