首页 > 其他 > 详细

ansible批量管理主机部署

时间:2014-03-22 09:00:43      阅读:604      评论:0      收藏:0      [点我收藏+]


Ansible 提供一种最简单的方式用于发布、管理和编排计算机系统的工具,你可在数分钟内搞定。

Ansible 是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展。

bubuko.com,布布扣




系统环境:Centos 6.4 x86_64


1.设置yum源:

[root@localhost yum.repos.d]# cat epel.repo
[epel]
name=epel
baseurl=http://mirrors.sohu.com/fedora-epel/6/x86_64/
gpgcheck=1
gpgkey=http://mirrors.sohu.com/fedora-epel/RPM-GPG-KEY-EPEL-6


2.安装ansible

yum -y install ansible



3.编辑ansible配置文件


[root@localhost ~]# vi /etc/ansible/ansible.cfg
hostfile       = /etc/ansible/hosts
library        = /usr/share/ansible
remote_tmp     = $HOME/.ansible/tmp
pattern        = *
forks          = 5
poll_interval  = 15
sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
transport      = smart
remote_port    = 22                 远程连接端口
remote_user = luojianlong           远程执行命令的用户
log_path = /var/log/ansible.log     ansible日志文件位置


4.设置ansible主机到被管理主机的免密码登录,这里不做详细介绍。


5.编辑/etc/ansible/hosts ,指定被管理的对象

[root@localhost ~]# vi /etc/ansible/hosts
[webservers]                          组名称
alpha.example.org                     可以使用域名或者IP地址
beta.example.org
192.168.1.100
192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
www[001:006].example.com               可以使用通配符
# Ex 3: A collection of database servers in the ‘dbservers‘ group
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
# Here‘s another example of host ranges, this time there are no
# leading 0s:
db-[99:101]-node.example.com


6.ansible命令的使用


[root@localhost ~]# ansible weidu-nginx-web -s -u luojianlong -a "ls /root"
ansible后面加hosts文件指定的组名字
-s            指定执行sudo命令
-u            设定远程连接的用户
-a            远程执行的命令


 
[root@localhost ~]ansible all -m copy -a "src=/root/hequan/shell/t.sh dest=/tmp/ mode=755 owner=root group=root" -k -u root
[root@localhost ~]ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root" -k -u root
[root@localhost ~]ansible all -i /etc/ansible/serverlist -m shell -a "/tmp/t.sh" -k -u root
[root@localhost ~]ansible all -m user -a "name=foo password=<crypted password here>"
[root@localhost ~]ansible all -m user -a "name=foo state=absent"
-i:指定 inventory 文件,使用当前目录下的 hosts
all:针对 hosts 定义的所有主机执行,这里也可以指定组名或模式
-m:指定所用的模块,我们使用 Ansible 内置的 ping 模块来检查能否正常管理远端机器






使用-s时,连接客户端的用户需要设置/etc/sudoers文件,使用户执行sudo时不需要密码


注意:客户端如果是redhat/Centos 5.7以前的话,需要升级python程序。




本文出自 “龍行天下” 博客,请务必保留此出处http://luojianlong.blog.51cto.com/4412415/1380861

ansible批量管理主机部署,布布扣,bubuko.com

ansible批量管理主机部署

原文:http://luojianlong.blog.51cto.com/4412415/1380861

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!