使用 ansible-bender 構建容器鏡像

瞭解如何使用 Ansible 在容器中執行命令。html

容器和 Ansible 能夠很好地融合在一塊兒:從管理和編排到供應和構建。在本文中,咱們將重點介紹構建部分。linux

若是你熟悉 Ansible,就會知道你能夠編寫一系列任務,ansible-playbook 命令將爲你執行這些任務。你知道嗎,若是你編寫 Dockerfile 並運行 podman build,你還能夠在容器環境中執行此類命令,並得到相同​​的結果。git

這是一個例子:github

- 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 服務器本地或容器中執行這個劇本,而且只要你記得先建立 our-file.txt,它就能夠工做。web

可是這裏缺乏了一些東西。你須要啓動(並配置)httpd 以便提供文件。這是容器構建和基礎架構供應之間的區別:構建鏡像時,你只需準備內容;而運行容器是另外一項任務。另外一方面,你能夠將元數據附加到容器鏡像,它會默認運行命令。bash

這有個工具能夠幫助。試試看 ansible-bender 怎麼樣?服務器

$ ansible-bender build the-playbook.yaml fedora:30 our-httpd
複製代碼

該腳本使用 ansible-bender 對 Fedora 30 容器鏡像執行該劇本,並將生成的容器鏡像命名爲 our-httpd網絡

可是,當你運行該容器時,它不會啓動 httpd,由於它不知道如何操做。你能夠經過向該劇本添加一些元數據來解決此問題:架構

- 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 不會爲無 root 容器建立專用網絡):dom

# 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 頁面。構建快樂!


via: opensource.com/article/19/…

做者:Tomas Tomecek 選題:lujun9972 譯者:geekpi 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

相關文章
相關標籤/搜索