ansbile简单应用

一、简介

Ansible is a radically simple configuration-management, application deployment, task-execution, and multinode orchestration engine.

Design Principles

Have a dead simple setup process and a minimal learning curve
Be super fast & parallel by default
Require no server or client daemons; use existing SSHd
Use a language that is both machine and human friendly
Focus on security and easy auditability/review/rewriting of content
Manage remote machines instantly, without bootstrapping
Allow module development in any dynamic language, not just Python
Be usable as non-root
Be the easiest IT automation system to use, ever.

 


二、安装

ansible依赖于Python 2.6或更高的版本、paramiko、PyYAML及Jinja2。

2.1 编译安装

解决依赖关系

# 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

2.2 rpm包安装
# yum install ansible

注意:不同版本的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)

实例应用:

ssh-keygen -t rsa -P ''    

ssh-copy-id -i .ssh/id_rsa.pub root@103.242.135.25   

一、定义host

注:一个主机可以定义于多个不同组          解决通信问题:1.直接可以编辑文件加入密码  2.可以基于ssh秘钥认证方式进行

ping模块使用,也可以使用单个主机或者已定义好的组名,这里使用all,代表所有主机

command模块使用,创建目录!可以使用linux系统命令

shell模块使用,命令中执行管道,重定向之类的需使用shell模块

shell模块和command模块的区别

cpoy模块使用,cp本地的1.html到远端主机的/root目录下,必须使用绝对路径

cron模块,批量执行定时任务

state=absent 删除指定name对应的任务计划

fetch拉取模块,将远端的文件拉到本地,和copy模块相反

file模块使用,创建目录,并赋予权限

用file模块,做软连接

使用yum模块,批量安转tree工具包

ansible all -m yum -a "name=tree state=installed"

name:指定安装包名

state:指定方式,安装或卸载

安装: install (`present' or `installed', `latest'),

卸载:remove (`absent' or `removed') a package

ansible all -m yum -a "name=httpd* state=latest"       #安装httpd服务

service模块使用,关闭服务:stopped 开启服务:started

 

posted @ 2017-03-12 10:06  ShinyBoy1  阅读(293)  评论(0编辑  收藏  举报