当前位置:网站首页>使用 ansible-bender 构建容器镜像
使用 ansible-bender 构建容器镜像
2022-08-08 15:57:00 【星哥玩云】
了解如何使用 Ansible 在容器中执行命令。
容器和 Ansible 可以很好地融合在一起:从管理和编排到供应和构建。在本文中,我们将重点介绍构建部分。
如果你熟悉 Ansible,就会知道你可以编写一系列任务,ansible-playbook 命令将为你执行这些任务。你知道吗,如果你编写 Dockerfile 并运行 podman build,你还可以在容器环境中执行此类命令,并获得相同的结果。
这是一个例子:
- name:Serveourfileusing httpdhosts: alltasks:- name:Install httpdpackage:name: httpdstate: installed- name:Copyourfile to httpd’s webrootcopy:src:our-file.txtdest:/var/www/html/
你可以在 Web 服务器本地或容器中执行这个剧本,并且只要你记得先创建 our-file.txt,它就可以工作。
但是这里缺少了一些东西。你需要启动(并配置)httpd 以便提供文件。这是容器构建和基础架构供应之间的区别:构建镜像时,你只需准备内容;而运行容器是另一项任务。另一方面,你可以将元数据附加到容器镜像,它会默认运行命令。
这有个工具可以帮助。试试看 ansible-bender 怎么样?
$ ansible-bender build the-playbook.yaml Fedora:30our-httpd
该脚本使用 ansible-bender 对 Fedora 30 容器镜像执行该剧本,并将生成的容器镜像命名为 our-httpd。
但是,当你运行该容器时,它不会启动 httpd,因为它不知道如何操作。你可以通过向该剧本添加一些元数据来解决此问题:
- name:Serveourfileusing httpdhosts: allvars:ansible_bender:base_image: fedora:30target_image:name:our-httpdcmd: httpd -DFOREGROUNDtasks:- name:Install httpdpackage:name: httpdstate: installed- name:Listen on all network interfaces.lineinfile:path:/etc/httpd/conf/httpd.confregexp:'^Listen 'line:Listen0.0.0.0:80- name:Copyourfile to httpd’s webrootcopy:src:our-file.txtdest:/var/www/html
现在你可以构建镜像(从这里开始,请以 root 用户身份运行所有命令。目前,Buildah 和 Podman 不会为无 root 容器创建专用网络):
# ansible-bender build the-playbook.yamlPLAY [Serveourfileusing httpd]****************************************************TASK [GatheringFacts]***************************************************************ok:[our-httpd-20191004-131941266141-cont]TASK [Install httpd]*****************************************************************loaded from cache:'f053578ed2d47581307e9ba3f64f4b4da945579a082c6f99bd797635e62befd0'skipping:[our-httpd-20191004-131941266141-cont]TASK [Listen on all network interfaces.]*********************************************changed:[our-httpd-20191004-131941266141-cont]TASK [Copyourfile to httpd’s webroot]**********************************************changed:[our-httpd-20191004-131941266141-cont]PLAY RECAP ***************************************************************************our-httpd-20191004-131941266141-cont : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0Getting image source signaturesCopying blob sha256:4650c04b851c62897e9c02c6041a0e3127f8253fafa3a09642552a8e77c044c8Copying blob sha256:87b740bba596291af8e9d6d91e30a01d5eba9dd815b55895b8705a2acc3a825eCopying blob sha256:82c21252bd87532e93e77498e3767ac2617aa9e578e32e4de09e87156b9189a0Copying config sha256:44c6dc6dda1afe28892400c825de1c987c4641fd44fa5919a44cf0a94f58949fWriting manifest to image destinationStoring signatures44c6dc6dda1afe28892400c825de1c987c4641fd44fa5919a44cf0a94f58949fImage'our-httpd' was built successfully \o/
镜像构建完毕,可以运行容器了:
# podman run our-httpdAH00558: httpd:Couldnot reliably determine the server's fully qualified domain name, using 10.88.2.106. Set the 'ServerName' directive globally to suppress this message
是否提供文件了?首先,找出你容器的 IP:
# podman inspect -f '{{ .NetworkSettings.IPAddress }}'7418570ba5a010.88.2.106
你现在可以检查了:
$ curl http://10.88.2.106/our-file.txtAnsibleis
你文件内容是什么?
这只是使用 Ansible 构建容器镜像的介绍。如果你想了解有关 ansible-bender 可以做什么的更多信息,请查看它的 GitHub 页面。构建快乐!
via: https://opensource.com/article/19/10/building-container-images-ansible
作者:Tomas Tomecek
边栏推荐
- 消除游戏中宝石下落的原理和实现
- 【Unity入门计划】制作RubyAdventure02-处理瓦片地图&碰撞
- 【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
- bzoj3262 Flowers bloom on Mo
- 成员变量和局部变量的区别?
- 跟我一起来学弹性云服务器ECS【华为云至简致远】
- 基于LEAP模型的能源环境发展、碳排放建模预测及不确定性分析
- Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
- Guanghong Technology: The company provides manufacturing services for Xiaomi, Samsung, OPPO, Nokia and other products in India
- 如何制作网页
猜你喜欢

Notes on the development of kindergarten enrollment registration system based on WeChat applet

C#/VB.NET convert PDF to PDF/X-1a:2001

成员变量和局部变量的区别?

groovy基础学习

本机Redis Desktop Manager连不上vmware的redis

解决Redis、MySQL缓存双写不一致问题
![[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision](/img/e9/2fd665da5ef8ce6f350a338ab1e81e.png)
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision

All volunteers V853 chip Tina RTSP environment set up

光弘科技:公司在印度为小米、三星、OPPO、诺基亚提供智能手机等产品的制造服务

【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
随机推荐
返回分页查询分类并统计多对多关系表中各分类下的应用数量
【Unity入门计划】用双血条方法控制伤害区域减血速度
Streamsets Data Collector 3.12
Tungsten Fabric SDN — OpenStack 与 Kubernetes 异构集群统一 SDN 方案
hdu2475 Box
【愚公系列】华为云云数据库MySQL的体验流程|【华为云至简致远】
EMQ畅谈IoT数据基础软件开源版图,引领本土开源走向全球
文档管理系统:攻克这3个痛点,解决80%企业文档管理难题
Introduction to Recurrent Neural Network (RNN)
firewall高级配置
手机注册股票开户的流程?网上开户安全?
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
如何选择ui设计机构
干货:从零设计高并发架构
promise学习笔记
掌握财富密码,运维需要了解这些技术
使用FastApi服务解决程序反复调试导致速度过慢的问题(以tsfresh为例)
web automation headless mode
MySQL清除表数据 id重置
CS231n:6 训练神经网络(二)