Ansible 提供一种最简单的方式用于发布、管理和编排计算机系统的工具,你可在数分钟内搞定。
Ansible 是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展。
系统环境:Centos 6.4 x86_64
1.设置yum源:
1
2
3
4
5
6
|
[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
1
|
yum -y
install
ansible
|
3.编辑ansible配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[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 ,指定被管理的对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[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命令的使用
1
2
3
4
5
|
[root@localhost ~]
# ansible weidu-nginx-web -s -u luojianlong -a "ls /root"
#ansible后面加hosts文件指定的组名字
#-s 指定执行sudo命令
-
#u 设定远程连接的用户
#-a 远程执行的命令
|
1
2
3
4
5
6
7
8
|
[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程序。
本文转自ljl_19880709 51CTO博客,原文链接:http://blog.51cto.com/luojianlong/1380861,如需转载请自行联系原作者