新聞中心
ansible是新出現(xiàn)的自動化運維工具,基于Python開發(fā),集合了眾多運維工具(puppet、chef、func、fabric)的優(yōu)點,實現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運行命令等功能。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),阜新企業(yè)網(wǎng)站建設(shè),阜新品牌網(wǎng)站建設(shè),網(wǎng)站定制,阜新網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,阜新網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
如果你熟悉 Ansible,就會知道你可以編寫一系列任務(wù),ansible-playbook 命令將為你執(zhí)行這些任務(wù)。你知道嗎,如果你編寫 Dockerfile 并運行 podman build,你還可以在容器環(huán)境中執(zhí)行此類命令,并獲得相同的結(jié)果。
這是一個例子:
- 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 服務(wù)器本地或容器中執(zhí)行這個劇本,并且只要你記得先創(chuàng)建 our-file.txt,它就可以工作。
但是這里缺少了一些東西。你需要啟動(并配置)httpd 以便提供文件。這是容器構(gòu)建和基礎(chǔ)架構(gòu)供應(yīng)之間的區(qū)別:構(gòu)建鏡像時,你只需準備內(nèi)容;而運行容器是另一項任務(wù)。另一方面,你可以將元數(shù)據(jù)附加到容器鏡像,它會默認運行命令。
這有個工具可以幫助。試試看 ansible-bender 怎么樣?
$ ansible-bender build the-playbook.yaml fedora:30 our-httpd
該腳本使用 ansible-bender 對 Fedora 30 容器鏡像執(zhí)行該劇本,并將生成的容器鏡像命名為 our-httpd。
但是,當你運行該容器時,它不會啟動 httpd,因為它不知道如何操作。你可以通過向該劇本添加一些元數(shù)據(jù)來解決此問題:
- 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
現(xiàn)在你可以構(gòu)建鏡像(從這里開始,請以 root 用戶身份運行所有命令。目前,Buildah 和 Podman 不會為無 root 容器創(chuàng)建專用網(wǎng)絡(luò)):
# 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/
鏡像構(gòu)建完畢,可以運行容器了:
# 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
你現(xiàn)在可以檢查了:
$ curl http://10.88.2.106/our-file.txt
Ansible is
你文件內(nèi)容是什么?
當前文章:通過ansible-bender構(gòu)建容器鏡像
瀏覽路徑:http://fisionsoft.com.cn/article/cohgsdj.html


咨詢
建站咨詢
