您的位置:首页 > 运维架构

十大自动化运维工具 ansible 推荐

2014-04-19 11:27 686 查看

一、简介

Ansible是一种集IT系统的配置管理、应用部署、流程设置的开源软件工具,与其他管理软件工具有多方面的差异。目标是提供面对广泛的自动化挑战如何获得大型生产力的优势。当Ansible提供更强大的生产力逐步替代其他许多核心性能的自动化解决方案,它也在寻求解决其他还没解决的IT挑战,这些包括如何时复杂多层级工作流程清晰化、清楚统一的OS配置、在单一框架下应用软件的部署。Ansible追寻保持IT工作流程描述的可理解性和能够快速部署。这意味着需要容易构建、容易理解 – 例如新用户能够快速融入新的IT项目。长期存在自动化的内容即使是项目结束数个月之后也能够容易理解。Ansible追寻使事情强大满足专家使用,同时也能满足不同水平级别用户需求,保证较快把IT项目推向市场,在IT配置需要变更时候能够易于较快、较少错误。Ansible是基于最小性质、一致性、安全性、高可靠性设计的,同时对于管理员、开发者、IT经理的学习曲线都非常低。




 模块 
 Ansible模块资源分发到远程节点使其执行特定任务或匹配一个特定的状态。Ansible遵循“batteries included“哲学,所以你有很多的各种各样的任务模块核心分布。这意味着模块是最新的,你不需要寻找一个实现,将在你的平台上工作。你可能认为模块库的工具箱里充满了有用的系统管理工具,和剧本作为建筑的指示使用这些工具的东西。



 剧本  
剧本是Ansible的配置、部署和编排语言。他们可以描述一个你想要你的远程系统执行政策,或一组步骤一般过程。如果Ansible模块的工具在你的工作室,剧本是你的设计方案。在基本层面上,剧本可用于管理配置和部署远程机器。在更高级的水平,他们可以序列多层糊涂事涉及滚动更新,和行动可以委托给其他主机,与监控服务器和负载平衡器交互。这里有大量的信息,没有必要学习一切。你可以开始小,接更多的功能随着时间的推移你需要他们。剧本被设计成人类可读的和正在开发的一个基本的文本语言。有多种方法来组织它们包括剧本和文件,我们将提供一些建议,使Ansible

二、安装

下载安装:http://releases.ansible.com/ansible/
解决依赖关系

# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
# tar xf ansible-1.5.4.tar.gz
# cd ansible-1.5.4
# python setup.py build
# python setup.py install
# mkdir /etc/ansible
# cp -r examples/* /etc/ansible


三、简单应用

ansible通过ssh实现配置管理、应用部署、任务执行等功能,因此,需要事先配置ansible端能基于密钥认证的方式联系各被管理节点。

ansible <host-pattern> [-f forks] [-m module_name] [-a args]
-m module:默认为command
ansible-doc: Show Ansible module documentation
-l, --list            List available modules
-s, --snippet         Show playbook snippet for specified module(s)


四、YAML

4.1 YAML介绍

YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy dt Net与Oren Ben-Kiki也是这语言的共同设计者。

YAML Ain't Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。其特性:

YAML的可读性好
YAML和脚本语言的交互性好
YAML使用实现语言的数据类型
YAML有一个一致的信息模型
YAML易于实现
YAML可以基于流来处理
YAML表达能力强,扩展性好

更多的内容及规范参见http://www.yaml.org。

4.2 YAML语法

YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。下面是一个示例。

name: John Smith
age: 41
gender: Male
spouse:
name: Jane Smith
age: 37
gender: Female
children:
-   name: Jimmy Smith
age: 17
gender: Male
-   name: Jenny Smith
age 13
gender: Female
YAML文件扩展名通常为.yaml,如example.yaml。

五、ansible playbooks

playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏。下面是一个简单示例。


- hosts: webnodes
vars:
http_port: 80
max_clients: 256
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted

5.1 playbook基础组件

5.1.1 Hosts和Users
playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。如上面示例中的


-hosts: webnodes
remote_user: root
不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。

- hosts: webnodes
remote_user: mageedu
tasks:
- name: test connection
ping:
remote_user: mageedu
sudo: yes

5.1.2 任务列表和action

play的主体部分是task list。task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。

task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。

每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出。

定义task的可以使用“action: module options”或“module: options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也中使用在行首使用几个空白字符进行换行。

tasks:
- name: make sure apache is running
service: name=httpd state=running
在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:


tasks:
- name: disable selinux
command: /sbin/setenforce 0
如果命令或脚本的退出码不为零,可以使用如下方式替代:


tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
或者使用ignore_errors来忽略错误信息:


tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True

5.1.3 handlers
用于当关注的资源发生变化时采取一定的操作。
“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。


- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handler是task列表,这些task与前述的task并没有本质上的不同。

handlers:
- name: restart memcached
service:  name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
案例:

heartbeat.yaml
- hosts: hbhosts
remote_user: root
tasks:
- name: ensure heartbeat latest version
yum: name=heartbeat state=present
- name: authkeys configure file
copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys
- name: authkeys mode 600
file: path=/etc/ha.d/authkeys mode=600
notify:
- restart heartbeat
- name: ha.cf configure file
copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf
notify:
- restart heartbeat
handlers:
- name: restart heartbeat
service: name=heartbeat state=restarted


应用实例:
Ansible可以同时管理您基础架构中多个系统,他是通过选择Ansible的资产文件中的系统列表的部分实现的,缺省的资产文件是/etc/Ansible/hosts/etc/Ansible/hosts是一个ini格式的文件,如下:
# cd /etc/ansible/
[root@dc ansible]# ls
ansible.cfg  hosts
cat hosts # 定义集群主机
#db-[99:101]-node.example.com
[webservers]
node1.firefox.com
node2.firefox.com
[dbservers]
node3.firefox.com
这些由一些命名的群组组成,您也可以不用群组,但这样使用很有好处。如果您的主机是运行在非标准的SSH端口,您可以把端口号与主机名称通过冒号隔开。不会去读取SSH配置文件中的端口列表,所以如果没有运行在缺省端口,您这是这些端口是非常重要的:
# (default is sftp)
#scp_if_ssh = True
[accelerate]
accelerate_port = 5099

基本命令应用; 查看ansible 能够支持那些命令 ansible-doc -l 查看某个命令的详细参数以及用法帮助信息: ansible-doc -s :指定命令比如 :
# ansible-doc  -s yum
- name: Manages packages with the `yum' package manager
action: yum
state=                 # Whether to install (`present', `latest'), or remove (`absent') a package.
disablerepo=           # `repoid' of repositories to disable for the install/update operation These repos will not persist beyond the transaction Multiple repos separated with a ','
name=                  # Package name, or package specifier with version, like `name-1.0'. When using state=latest, this can be '*' which means run: yum -y update. You can also pass a
enablerepo=            # Repoid of repositories to enable for the install/update operation. These repos will not persist beyond the transaction multiple repos separated with a ','
list=                  # Various (non-idempotent) commands for usage with `/usr/bin/ansible' and `not' playbooks. See examples.
disable_gpg_check=     # Whether to disable the GPG checking of signatures of packages being installed. Has an effect only if state is `present' or `latest'.
conf_file=             # The remote yum configuration file to use for the transaction.
# ansible all -m ping
# ansible all -a 'date'
# ansible all -m command -a 'date'
# 查看web服务的运行状态
# ansible webservers -m command -a 'service httpd status'
# ansible  all -m copy  -a "src=/root/named dest=/root/"
# ansible all -a "ls /tmp"
# ansible-doc  -s copy  # ansible-doc -s 指定命令;能够查看命令的详细用法
# 定义时间同步:每3分钟同步一次,以局域网内的时间服务器同步一次时间;
# ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.0.1"'
# ansible all -a "crontab -l" # 查看周期性计划任务
# ansible all -m group -a "gid=360 system=yes name=mysql"  # 在所有节点上添加mysql组 system=yes: 表示此组是系统组
# # ansible all -m command -a "tail -1 /etc/group"  # 查看组是否添加成功;
# ansible all -a 'yum -y install corosync' 在所有节点上安装corosync
因为corosync安装完成之后会生成配置文件
# ansible all  -m copy -a "src=/etc/corosync/corosync.conf.example  dest=/etc/corosynccorosync.conf"
# ansible all -m copy  -a "src=/root/pssh-2.3.1-2.el6.x86_64.rpm  dest=/root/"
# ansible all -m copy  -a "src=/root/crmsh-1.2.6-4.el6.x86_64.rpm   dest=/root/"
# 复制需要安装的rpm包
# ansible all -a "yum -y install crmsh-1.2.6-4.el6.x86_64.rpm  pssh-2.3.1-2.el6.x86_64.rpm"
# 安装
# ansible all -a "service httpd stop"  # 将当前服务关闭掉
# ansible all -a "chkconfig --list httpd" # 查看web服务的运行级别
# ansible all -m service -a "state=started name=httpd enabled=yes"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息