ansible批量管理主机部署

简介:

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

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

ansible architecture diagram




系统环境: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,如需转载请自行联系原作者





相关文章
|
6月前
|
运维 Kubernetes 网络安全
Ansible自动化运维工具之主机管理与自定义配置文件(2)
Ansible自动化运维工具之主机管理与自定义配置文件(2)
|
8月前
|
运维 负载均衡 关系型数据库
【运维知识进阶篇】用Ansible Roles重构LNMP架构(Linux+Nginx+Mariadb+PHP),实现4个项目一键部署
【运维知识进阶篇】用Ansible Roles重构LNMP架构(Linux+Nginx+Mariadb+PHP),实现4个项目一键部署
112 0
|
9月前
管理工具ansible的部署
管理工具ansible的部署
65 0
管理工具ansible的部署
|
5月前
|
Linux 网络安全 调度
使用docker部署awx-1.7.1.0(ansible图形化界面)
使用docker部署awx-1.7.1.0(ansible图形化界面)
288 0
|
5月前
|
弹性计算 关系型数据库 数据库
通过计算巢轻松部署 Ansible Semaphore
AnsibleSemaphore是一个现代化的Ansible用户界面,通过计算巢可轻松部署。
152 1
通过计算巢轻松部署 Ansible Semaphore
|
8月前
|
运维 监控
【运维知识进阶篇】Zabbix5.0稳定版详解10(Zabbix自动注册+Ansible自动部署,实现一条命令监控任意主机)
【运维知识进阶篇】Zabbix5.0稳定版详解10(Zabbix自动注册+Ansible自动部署,实现一条命令监控任意主机)
112 0
|
8月前
|
运维 Ubuntu 应用服务中间件
【运维知识进阶篇】Ansible自动化运维-Ansible安装与主机列表
【运维知识进阶篇】Ansible自动化运维-Ansible安装与主机列表
117 0
|
8月前
|
应用服务中间件 nginx
Ansible模块——软件包管理模块
Ansible模块——软件包管理模块
|
9月前
|
存储 JSON 数据安全/隐私保护
ansible定义变量和管理事实
ansible定义变量和管理事实
124 0
|
11月前
|
Unix 应用服务中间件 Linux
ansible在web集群中批量部署nfs
ansible在web集群中批量部署nfs
154 0