分享

Ansible

 Glory____ 2017-03-01

Ansible


简介

Ansible是一款自动化运维工具。可以通过它来进行部署应用,管理系统,扫平复杂的运维工作。

官方标语

Deploy apps. Manage systems. Crush complexity.Ansible helps you build a strong foundation for DevOps.

Ansible不需要额外代理和安全基础设施,足够简单的去部署,足够简单的去使用,采用yaml格式的配置语言。 Ansible通过SSH连接到目标机器,并推送要在模板机器执行的small programs 去执行。这些small programs在ansible中被称为'Ansible Modules'


Getting started

install

可以通过命令直接按照 比如CentOS or RHEL

sudo yum install ansible

Ubuntu

sudo apt-get install software-properties-common sudo apt-add-repository ppa:ansible/ansible sudo apt-get update sudo apt-get install ansible

也可以通过Pip安装,mac上可以通过这种方式安装

sudo easy_install pip sudo pip install ansible

安装方式很多,不一一介绍,具体可参考 详细安装

Inventory

Inventory 主要是用来管理目标机器列表的,是普通文本格式,一般习惯以.ini为后缀。

host.ini

[webservers] www1.example.com www2.example.com [dbservers] db0.example.com db1.example.com

这些域名也可以直接用ip。

hello world

在进行ansible hello world之前我们需要把目标机器和源机器直接的SSH通道打通。当然也可以直接加-k 执行命令的时候动态输入密码

直接通过ansible命令执行

ansible -i host.ini all -m ping ansible foo.example.com -m yum -a 'name=httpd state=installed' ansible foo.example.com -a '/bin/ls' ansible -i host.ini webservers -m copy -a 'src=/etc/hosts dest=/tmp/hosts'

-m 指代ansible模块,-a 指代模块参数,-u指代用户名,--sudo 指代sudo权限 -k 输入密码 -K 输入sudo权限密码 -i 指定目标机器的Inventory,all指代所有机器,可以用Inventory中设定好的分组名称,用来指定特定分组机器。

-m 如果不指定默认的模块是command

这种直接同ansible命令指定目标机器 模块名称 模块参数快速执行任务 叫作ad-hoc。相比下面介绍的playbook,这种方式可以快速了解ansible基础功能,更快的做些简单任务。


playbooks

Ansible的强大之处在于playbooks。 如果Ansible模块是一个个工具的话,playbook就是通过组装这些工具完成特定任务的设计方案。通过playbook可以完成对远程目标机器的部署,配置管理等操作。

Playbooks 采用yaml语法格式,尽量不成为一个编程语言或者脚本,而是通过配置的形式来实现模块的编排。

Playbooks中的任务按顺序执行,只要满足条件的目标机器都完成任务分发,才进行下一个任务; ( Tasks are executed in order, one at a time, against all machines matched by the host pattern, before moving on to the next task. )

hello world

hello.yml
--- - hosts: webservers vars: http_port: 80 max_clients: 200 remote_user: root tasks: - name: ensure apache is at the latest version yum: name=httpd state=latest - name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd/conf/httpd.conf notify: - restart apache - name: ensure apache is running (and enable it at boot) service: name=httpd state=started enabled=yes handlers: - name: restart apache service: name=httpd state=restarted

注意dest 目标文件的位置,服务指定指定的配置目录不一样,找个把httpd.conf文件/srv 把监听端口替换为 {{ http_port }} ,最大连接数替换为{{max_clients}}

hello.ini
[webservers] 127.0.0.1
启动命令
ansible-playbook hello.yml -u xxx -i hello.ini -kK #kK代表用密码的方式分发

hosts代表目标机器,remote_user指代远程使用的用户,vars定义变量,在该play下文中都可以直接用{{}}引用,下面详细讲解task

tasks

一个playbook往往由多个task组成,依次执行完成特定目的。playbook中的任务按顺序执行,只要满足条件的目标机器都完成任务分发,才进行下一个任务; ( Tasks are executed in order, one at a time, against all machines matched by the host pattern, before moving on to the next task. ) 一个task由name ,模块,加特定的条件,异常处理逻辑组成。注意yml语法的缩进格式,一般如果以tasks左界限为准,下一行需要两个空格,加'-',然后在一个空格加name,其他都直接4个空格。

tasks: - name: xxxx 模块名称: xxxxx when: xxxx ignore_errors: True

ansible包含很多模块比如 copy,shell,command,group,user,template;模块可以通过ansible命令直接用,也可以在tasks中用; 下面举几个task例子,运行这些例子时,可以把上面helloworld中的task去掉,换成下面的task即可。

copy

tasks: - name: copy copy: src=/home/robin/xx.txt dest=/home/robin/xx1.txt ignore_errors: True

kill java process

tasks: - name: kill java process shell: jps | grep -v Jps | awk -F ' ' '{print $1}' | xargs kill -9 ignore_errors: True

Loops and Conditionals

tasks: - command: echo {{ item }} with_items: [ 0, 2, 4, 6, 8, 10 ] when: item > 5

详细的循环参照

任务与任务之间,我们可以把上一个任务的结果注册register起来,供下一个任务调用,做一些条件判断等。

详细的ansible模块目录

roles

一个大的playbooks项目可能包含很多功能,playbooks提供了Roles概念,实现某个具体目的的role由它所需的 tasks ,handler,variables组成,这些角色可能对应操作dbserver的role,操作webserver的role。 Roles概念另外一个好处是可以利用公共的task文件等 例如下边这个任务文件

--- # possibly saved as tasks/foo.yml - name: placeholder foo command: /bin/foo - name: placeholder bar command: /bin/bar

我们可以通过在另外一个文件这样引用 tasks:

- include: tasks/foo.yml

这里举个mongodb机器操作的例子 链接。一个mongo集群通常包括mongod数据节点 ,mongos 路由,mongoc 配置 三个不同服务组成,因此在部署集群时我们会通过指定不同role来运行安装特定服务。详细结构可以看上面贴出的链接。


Tips and Tricks

查看影响的目标机器 ansible-playbook playbook.yml --list-hosts

设置并发度 ansible-playbook playbook.yml -f 10

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多