一、安装
rpm包安装: EPEL源
yum install ansible
编译安装:
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
Git方式:
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
pip安装: pip是安装Python包的管理器,类似yum
yum install python-pip python-devel
yum install gcc glibc-devel zibl-devel rpm-bulid openssl-devel
pip install --upgrade pip
pip install ansible --upgrade
确认安装:
ansible --version
相关文件
配置文件
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性(一般无需修改)
/etc/ansible/hosts 主机清单(将被管理的主机放到此文件)
/etc/ansible/roles/ 存放角色的目录
程序
/usr/bin/ansible 主程序,临时命令执行工具
/usr/bin/ansible-doc 查看配置文档,模块功能查看工具
/usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台
/usr/bin/ansible-playbook 定制自动化任务,编排剧本工具
/usr/bin/ansible-pull 远程执行命令的工具
/usr/bin/ansible-vault 文件加密工具
/usr/bin/ansible-console 基于Console界面与用户交互的执行工具
主机清单inventory
Inventory 主机清单
1> ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名
2> 默认的inventory file为/etc/ansible/hosts
3> inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成
/etc/ansible/hosts文件格式
inventory文件遵循INI文件风格,中括号中的字符为组名。
可以将同一个主机同时归并到多个不同的组中;
此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明
ntp.magedu.com 不分组,直接加
[webservers] webservers组
www1.magedu.com:2222 可以指定端口
www2.magedu.com
[dbservers]
db1.magedu.com
db2.magedu.com
db3.magedu.com
如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机
示例:
[websrvs]
www[1:100].example.com ip: 1-100
[dbsrvs]
db-[a:f].example.com dba-dbff
二、实战
主控机 : 192.168.188.135
受控机 : 192.168.188.131、192.168.188.134
定义了两个不同的组,dbservers和webservers
1️⃣自定义Hello.yml
#hello world yml file
- hosts: websrvs
remote_user: root
tasks:
- name: hello world
command: /usr/bin/wall hello world
运行命令 ansible-playbook Hello.yml,对主机组webservers下的主机发送hello world
运行成功:
两台受控机均响应成功,打印hello world
2️⃣自定义stop.yml
#stop yml file
- hosts: 192.168.188.131
remote_user: root
tasks:
- name: stop clickhouse-server
shell: "ps -ef|grep clickhouse-server|grep -v grep|awk '{print $2}'|xargs -r kill"
register: startup_out
- name: print stopclickhouse-server
debug: var=startup_out
运行命令 ansible-playbook stop.yml,关闭主机192.168.188.131下的clickhouse-server
首先看下clickhouse服务的状态:
运行中
运行命令 ansible-playbook stop.yml
再看下clickhouse服务的状态:
已经被中止
3️⃣java请求ansible Api
这里还是运行命令 ansible-playbook Hello.yml
public class DemoApplication {
public static void main(String[] args) {
//运行命令生成对比信息
List<String> generateComparativeInformationCommands = new ArrayList<>();
generateComparativeInformationCommands.add("ansible-playbook Hello.yml");
executeNewFlow(generateComparativeInformationCommands);
System.out.println("finish");
}
public static List<String> executeNewFlow(List<String> commands) {
List<String> rspList = new ArrayList<String>();
Runtime run = Runtime.getRuntime();
try {
Process proc = run.exec("/bin/bash", null, null);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
for (String line : commands) {
out.println(line);
}
// 这个命令必须执行,否则in流不结束。
out.println("exit");
String rspLine = "";
while ((rspLine = in.readLine()) != null) {
System.out.println(rspLine);
rspList.add(rspLine);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return rspList;
}
}
将代码打包扔在主控机上
运行jar包:
发送成功再看看其他两台受控机:
均响应成功