博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ansible
阅读量:7226 次
发布时间:2019-06-29

本文共 4119 字,大约阅读时间需要 13 分钟。

hot3.png

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-commonsudo apt-add-repository ppa:ansible/ansiblesudo apt-get updatesudo apt-get install ansible

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

sudo easy_install pip sudo pip install ansible

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

Inventory

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

host.ini

[webservers]www1.example.comwww2.example.com[dbservers]db0.example.comdb1.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起来,供下一个任务调用,做一些条件判断等。

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

转载于:https://my.oschina.net/robinyao/blog/686646

你可能感兴趣的文章
Mysql 批量写入数据,对于这类性能问题,你是如何优化的
查看>>
MySQL无法启动几种常见问题小结
查看>>
阿里CTO:阿里所有技术和产品输出都将必须通过阿里云进行
查看>>
更好用的集群限流功能,Sentinel 发布 v1.4.2
查看>>
Python(生成执行文件)
查看>>
redis安装配置 - ttlsa教程系列之redis
查看>>
Linux --DHCP服务器配置;DHCP服务器中继
查看>>
IE版本多的可爱_已迁移
查看>>
eclipse查看jar包中class的中文注释乱码问题的解决
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
mariadb安装
查看>>
vue+vuex+axios+echarts画一个动态更新的中国地图
查看>>
5.8 volumetric post-processing--game programming gems5 笔记
查看>>
8086的地址空间
查看>>
Android开发动画效果被遮掉的解决方法
查看>>
Apache2.2.17源码编译安装以及配置虚拟主机
查看>>
2017年开发语言排名
查看>>
读二进制表的显示 Binary Watch
查看>>
我的友情链接
查看>>