openstack-基于packer自动化build镜像

Packer是一个从单一的模板文件来创建多平台一致性镜像的轻量级开源工具,它能够运行在常用的主流操作系统如Windows、Linux和Mac os上,能够高效的并行创建多平台例如AWS、Azure和Alicloud/OpenStack 的镜像,它的目的并不是取代Puppet/Chef等配置管理工具,实际上,当制作镜像的时候,Packer可以使用Chef或者Puppet,ansible 等工具来安装镜像所需要的软件。通过Packer自动化的创建各种平台的镜像是非常容易的。

安装packer

1
2
3
4
#wget https://releases.hashicorp.com/packer/1.7.10/packer_1.7.10_linux_amd64.zip
#unzip packer_1.7.10_linux_amd64.zip
#mv packer /usr/local/bin/
#/usr/local/bin/packer --help

定义packer模板

1
2
#touch openstack.json
#vim openstack.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"builders": [{
"type": "openstack",
"identity_endpoint": "http://10.224.130.52:35357/v3",
"tenant_name": "admin",
"domain_name": "default",
"username": "admin",
"password": "password",
"ssh_username": "root",
"region": "bj",
"image_name": "Ubuntu-image-updating-powered-by-Packer",
"instance_name": "Ubuntu-image-updating-powered-by-Packer",
"source_image": "c751e91f-80f7-43a1-afea-58287a8df695",
"availability_zone": "nova",
"flavor": "ecs.2large",
"use_blockstorage_volume": true,
"networks": ["9df8061e-abb2-4166-9c16-452f389c3e39"]
}],
"provisioners": [
{
"type": "shell",
"scripts": ["os-init.sh"]
}
],
"post-processors": [{
"strip_path": true,
"output": "packer-template-ubuntu-updating-result.log",
"type": "manifest"
}]
}
1
#vim os-init.sh
1
2
3
#!/bin/bash
# os-init.sh
yum -y install gcc

build 镜像

1
packer build openstack.json
感受一下packer的魔力吧~