ansible入门

前言

    最近看了一下ansible,挺火的一个配置管理工具,对比老大哥puppet,使用起来要简单一些,并且可以批量执行命令,对比同是python语言编写的saltstack,不需要安装客户端(基于paramiko),也更灵活一些,尤其我们现有的登录方式,通过跳板机加密钥,改造成ansible成本很低,准备学习一下以后在生产上使用。

    ansible已被红帽收购,目前出到2.0了,和红帽的朋友聊了一下,这个配合现在大火的openstack(红帽是其代码最大贡献者之一),看来以后会成为自动化运维的一个趋势。

    官网地址http://docs.ansible.com/

安装

最简单的epel之后,yum install ansible

也可以用pip的方式

pip install ansible即可

如果像我们一样服务器不能连接公网,需要手动下载一下python模块ecdsa, pycrypto, paramiko, MarkupSafe, jinja2, PyYAML, ansible

目前安装的版本是1.9.4

ansible --version
ansible 1.9.4

入门使用

首先把ssh密钥搞好,这里就不说了,保证master和minion之间不通过密码

定义主机和组:

vim /etc/ansible/hosts  #默认的文件位置,也可执行ansible时手动指定hosts文件,通过-i参数

[test]
10.199.2.45
10.199.2.46

[web]
10.199.2.[42:47]  #=10.199.2.424344454647
test.case.com  #主机名定义

使用第一个模块:

ansible test -m ping -u sre -s #-m接模块名,因为我们使用的是sre账户登录后通过sudo的方式,-u接用户,-s表示sudo方式执行
ansible test:!10.199.2.46 -m ping -u sre -s #:!排除某个主机
ansible 10.199.2.46 -m ping -u sre -s #当然也可以直接接ip而不使用组进行

ansible自带了很多模块

可以通过ansible-doc -l 查看总共有哪些模块,ansible-doc ping 显示某个模块的用法,ansible-doc -s ping 显示某个模块在playbooks中的代码片段

远程执行命令模块:

远程执行命令可能是ansible最常用也是最方便的一个功能,这里举几个例子

1.执行minion操作系统命令

ansible test -a 'w' -u sre -s #默认模块command,实现执行远程命令,-a接模块参数

2.minion上执行master上脚本

master先创建脚本1.sh

#!/bin/bash

ls /tmp/
ansible test -m script -a '1.sh' -u sre -s  #script模块相当于scp+shell,将本地脚本在远端minion进行执行

3.minion执行minion上脚本,2.45远端存在minion.sh,2.46不存在

ansible test -m shell -a '/tmp/minion.sh' -u sre -s 

shell和command模块很类似,看帮助信息了解到command和shell功能基本一致,但shell可以使用环境变量、管道等,功能更强大

copy模块 

ansible test -m copy -a 'src=1.sh dest=/tmp/ owner=root group=root mode=0755' -u sre -s
#将本地1.sh文件传到远端,如第二次执行此命令,如无更新,则远端无更新,有更新,则远端更新

yum模块

ansible test -m yum -a "name=nc state=latest" -u sre -s

cron模块

ansible test -m cron -a "name='test' job='ls /tmp' minute=*/2 hour=3,4,5" -u sre -s
#对端服务器crontab -l
#Ansible: test
*/2 3,4,5 * * * ls /tmp

ansible test -m cron -a "name='test' state=absent" -u sre -s #删除该条cron

service模块

ansible test -m service -a "name=ntpd state=started" -u sre -s     
ansible test -m service -a "name=ntpd state=stopped" -u sre -s     

user模块

ansible test -m user -a "name=test123" -u sre -s  #创建用户
ansible test -m user -a "name=test123 state=absent remove=yes" -u sre -s  #删除用户并删除家目录

lineinfile模块

#用于文件内的内容处理
ansible ctx-lf -m lineinfile -a "dest=/tmp/sudoers line='appuser  ALL=(ALL)       NOPASSWD:ALL' insertafter=^sre" -u sre -s -i host  #在sudo文件中sre开头之后加入line=的内容,insertafter可以写正则或EOF(结尾),同理还有insertbefore也可以写正则或BOF(开头)
ansible ctx-lf -m lineinfile -a "dest=/tmp/sudoers state=absent regexp=^appuser" -u sre -s -i hosts  #去掉正则匹配的所有行
ansible ctx-lf -m lineinfile -a "dest=/tmp/sudoers regexp=^sre line='#sre  ALL=(ALL)       NOPASSWD:ALL'" -u sre -s -i hosts  #将sre开头的最后匹配的一行前边加上#
#也可以用()形式做替代变更,类似sed
ansible ctx-lf -m lineinfile -a "dest=/tmp/sudoers regexp=^(sre.*)$ line='#1' backrefs=yes" -u sre -s -i hosts   #1表示第一个()里的内容,注意这种用法需要backrefs为yes,开启扩展正则匹配
ansible ctx-lf -m lineinfile -a "dest=/tmp/sudoers regexp=^(sre.*)$ line='123123' validate='visudo -cf %s'" -u sre -s -i hosts   #加入validate的验证,比如sudo文件如果改错了,可能影响整个系统的管理,加入验证之后,如果修改的sudo文件格式错误,将不会保存

 进阶一下,请看下一篇ansible playbook的基本介绍 http://www.cnblogs.com/caseast/p/5181910.html

原文地址:https://www.cnblogs.com/caseast/p/5180205.html