加入收藏 | 设为首页 | 会员中心 | 我要投稿 济南站长网 (https://www.0531zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

使用ansible-bender构建容器镜像

发布时间:2019-10-31 16:37:24 所属栏目:Windows 来源:Tomas Tomecek
导读:副标题#e# 了解如何使用 Ansible 在容器中执行命令。 容器和 Ansible 可以很好地融合在一起:从管理和编排到供应和构建。在本文中,我们将重点介绍构建部分。 如果你熟悉 Ansible,就会知道你可以编写一系列任务, ansible-playbook 命令将为你执行这些任务
副标题[/!--empirenews.page--]

使用ansible-bender构建容器镜像

了解如何使用 Ansible 在容器中执行命令。

容器和 Ansible 可以很好地融合在一起:从管理和编排到供应和构建。在本文中,我们将重点介绍构建部分。

如果你熟悉 Ansible,就会知道你可以编写一系列任务,ansible-playbook 命令将为你执行这些任务。你知道吗,如果你编写 Dockerfile 并运行 podman build,你还可以在容器环境中执行此类命令,并获得相同​​的结果。

这是一个例子:

  1. - name: Serve our file using httpd
  2. hosts: all
  3. tasks:
  4. - name: Install httpd
  5. package:
  6. name: httpd
  7. state: installed
  8. - name: Copy our file to httpd’s webroot
  9. copy:
  10. src: our-file.txt
  11. dest: /var/www/html/

你可以在 Web 服务器本地或容器中执行这个剧本,并且只要你记得先创建 our-file.txt,它就可以工作。

但是这里缺少了一些东西。你需要启动(并配置)httpd 以便提供文件。这是容器构建和基础架构供应之间的区别:构建镜像时,你只需准备内容;而运行容器是另一项任务。另一方面,你可以将元数据附加到容器镜像,它会默认运行命令。

这有个工具可以帮助。试试看 ansible-bender 怎么样?

  1. $ ansible-bender build the-playbook.yaml fedora:30 our-httpd

该脚本使用 ansible-bender 对 Fedora 30 容器镜像执行该剧本,并将生成的容器镜像命名为 our-httpd

但是,当你运行该容器时,它不会启动 httpd,因为它不知道如何操作。你可以通过向该剧本添加一些元数据来解决此问题:

  1. - name: Serve our file using httpd
  2. hosts: all
  3. vars:
  4. ansible_bender:
  5. base_image: fedora:30
  6. target_image:
  7. name: our-httpd
  8. cmd: httpd -DFOREGROUND
  9. tasks:
  10. - name: Install httpd
  11. package:
  12. name: httpd
  13. state: installed
  14. - name: Listen on all network interfaces.
  15. lineinfile:
  16. path: /etc/httpd/conf/httpd.conf
  17. regexp: '^Listen '
  18. line: Listen 0.0.0.0:80
  19. - name: Copy our file to httpd’s webroot
  20. copy:
  21. src: our-file.txt
  22. dest: /var/www/html

现在你可以构建镜像(从这里开始,请以 root 用户身份运行所有命令。目前,Buildah 和 Podman 不会为无 root 容器创建专用网络):

  1. # ansible-bender build the-playbook.yaml
  2. PLAY [Serve our file using httpd] ****************************************************
  3. TASK [Gathering Facts] ***************************************************************
  4. ok: [our-httpd-20191004-131941266141-cont]
  5.  
  6. TASK [Install httpd] *****************************************************************
  7. loaded from cache: 'f053578ed2d47581307e9ba3f64f4b4da945579a082c6f99bd797635e62befd0'
  8. skipping: [our-httpd-20191004-131941266141-cont]
  9.  
  10. TASK [Listen on all network interfaces.] *********************************************
  11. changed: [our-httpd-20191004-131941266141-cont]
  12.  
  13. TASK [Copy our file to httpd’s webroot] **********************************************
  14. changed: [our-httpd-20191004-131941266141-cont]
  15.  
  16. PLAY RECAP ***************************************************************************
  17. our-httpd-20191004-131941266141-cont : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
  18.  
  19. Getting image source signatures
  20. Copying blob sha256:4650c04b851c62897e9c02c6041a0e3127f8253fafa3a09642552a8e77c044c8
  21. Copying blob sha256:87b740bba596291af8e9d6d91e30a01d5eba9dd815b55895b8705a2acc3a825e
  22. Copying blob sha256:82c21252bd87532e93e77498e3767ac2617aa9e578e32e4de09e87156b9189a0
  23. Copying config sha256:44c6dc6dda1afe28892400c825de1c987c4641fd44fa5919a44cf0a94f58949f
  24. Writing manifest to image destination
  25. Storing signatures
  26. 44c6dc6dda1afe28892400c825de1c987c4641fd44fa5919a44cf0a94f58949f
  27. Image 'our-httpd' was built successfully o/

(编辑:济南站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!