使用 ansible-bender 工具构建容器镜像

学习如何使用 Ansible 在容器中执行命令。
284 位读者喜欢这篇文章。
3 mistakes to avoid when learning to code in Python

Opensource.com

容器和 Ansible 完美地融合在一起——从管理和编排到配置和构建。在本文中,我们将专注于构建部分。

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

这是一个例子

- name: Serve our file using httpd
  hosts: all
  tasks:
  - name: Install httpd
    package:
      name: httpd
      state: installed
  - name: Copy our file to httpd’s webroot
    copy:
      src: our-file.txt
      dest: /var/www/html/

您可以在本地 Web 服务器或容器中执行此 playbook,它会正常工作——只要您记得首先创建 our-file.txt 文件。

但是缺少了一些东西。您需要启动(并配置)httpd 才能提供您的文件。这是容器构建和基础设施配置之间的区别:构建镜像时,您只需准备内容;运行容器是不同的任务。另一方面,您可以将元数据附加到容器镜像,以告知默认运行的命令。

这就是工具可以发挥作用的地方。 试试 ansible-bender 怎么样?

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

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

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

- name: Serve our file using httpd
  hosts: all
  vars:
    ansible_bender:
      base_image: fedora:30
      target_image:
        name: our-httpd
        cmd: httpd -DFOREGROUND
  tasks:
  - name: Install httpd
    package:
      name: httpd
      state: installed
  - name: Listen on all network interfaces. 
    lineinfile:      
      path: /etc/httpd/conf/httpd.conf  
      regexp: '^Listen ' 
      line: Listen 0.0.0.0:80   
  - name: Copy our file to httpd’s webroot
    copy:
      src: our-file.txt
      dest: /var/www/html

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

# ansible-bender build the-playbook.yaml
PLAY [Serve our file using httpd] ****************************************************
                                                                                                                                                                             
TASK [Gathering Facts] ***************************************************************    
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 [Copy our file 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=0 

Getting image source signatures
Copying blob sha256:4650c04b851c62897e9c02c6041a0e3127f8253fafa3a09642552a8e77c044c8
Copying blob sha256:87b740bba596291af8e9d6d91e30a01d5eba9dd815b55895b8705a2acc3a825e
Copying blob sha256:82c21252bd87532e93e77498e3767ac2617aa9e578e32e4de09e87156b9189a0
Copying config sha256:44c6dc6dda1afe28892400c825de1c987c4641fd44fa5919a44cf0a94f58949f
Writing manifest to image destination
Storing signatures 
44c6dc6dda1afe28892400c825de1c987c4641fd44fa5919a44cf0a94f58949f
Image 'our-httpd' was built successfully \o/

镜像已构建完成,现在是运行容器的时候了

# podman run our-httpd
AH00558: httpd: Could not 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 }}' 7418570ba5a0
10.88.2.106

现在您可以检查了

$ curl http://10.88.2.106/our-file.txt
Ansible is ❤

您的文件内容是什么?

这只是使用 Ansible 构建容器镜像的介绍。如果您想了解更多关于 ansible-bender 可以做什么的信息,请在 GitHub 上查看它。祝您构建愉快!

接下来阅读
标签
human
工程师。黑客。演讲者。修补匠。红帽人。喜欢容器、Linux、开源、Python 3、Rust、zsh、tmux。

9 条评论

感谢分享,我喜欢这张照片

嗨 Tomas,感谢分享。我不知道 Ansible 可以用于构建容器并在其中运行命令。它看起来确实很有前景。继续保持良好的工作。

好文章!感谢分享

这真是一篇有趣的文章!继续分享

这真是一篇有趣的文章!继续分享

大家好,我是 Taylor。我是一名专业的 Web 开发人员和培训师,曾在加拿大的 Microtek Learning Inc. 工作。 我是 Web 开发、技术和编程的爱好者。我也对教育和创业感兴趣。

我只是想让您知道,我非常感谢您的精彩文章!

谢谢

谢谢

© . All rights reserved.