使用ansible配置docker变量

时间:2018-04-13 09:08:31

标签: docker ansible containers

我在存储库中有一个FTP服务器的docker镜像,这个图像将用于多台机器,我需要根据目标机器部署容器并更改func numberOfSections(in tableView: UITableView) -> Int { return inr.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return reqtime[section] } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // Here you have same no of items in inr, reqtime and status so for each inr item you will only have a single reqtime and single status return 1 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60.0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DepositHistoryTableViewCell let inrval = Double(inr[indexPath.section]) cell.lblINR.text = String(format: "%.2f",inrval) cell.lblStatus.text = status[indexPath.section] return cell } 变量。

这是我的图片(我删除了proftpd安装的行,因为它与此案例无关):

let inr = ["200.0", "1200.0", "500.0", "2000.0", "2000.0", "2000.0", "2000.0", "2000.0", "3000.0", "5000.0", "1000.0", "100000.0", "100000.0"]
let reqtime = ["2018-04-11 13:54:46", "2018-04-09 16:31:42", "2018-04-09 12:26:32", "2018-04-09 12:19:40", "2018-04-09 12:19:35", "2018-04-09 12:19:23", "2018-04-09 12:09:05", "2018-04-09 12:08:25", "2018-04-09 11:38:15", "2018-04-09 11:07:59", "2018-04-09 11:03:58", "2018-04-09 10:54:55", "2018-04-09 10:45:47"]
let status = ["Approve", "Pending", "Pending", "Pending", "Pending", "Approve", "Pending", "Pending", "Pending", "Pending", "Pending", "Pending", "Approve"]


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return inr.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40.0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DepositHistoryTableViewCell

    cell.lblRequestTime.text = reqtime[indexPath.row]
    let inrval = Double(inr[indexPath.row])
    cell.lblINR.text = String(format: "%.2f",inrval!)
    cell.lblStatus.text = status[indexPath.row]

    return cell
}

我的PORT文件(同时删除了与此案例不相关的信息)

FROM alpine:3.5

ARG vcs_ref="Unknown"
ARG build_date="Unknown"
ARG build_number="1"

LABEL org.label-schema.vcs-ref=$vcs_ref \
  org.label-schema.build-date=$build_date \
  org.label-schema.version="alpine-r${build_number}"

ENV PORT=10000
COPY assets/port.conf /usr/local/etc/ports.conf
COPY replace.sh /replace.sh
#It is for a proFTPD Server
CMD ["/replace.sh"]

port.conf脚本是:

# This is a basic ProFTPD configuration file (rename it to
# 'proftpd.conf' for actual use.  It establishes a single server
# and a single anonymous login.  It assumes that you have a user/group
# "nobody" and "ftp" for normal operation and anon.

ServerName          "ProFTPD Default Installation"
ServerType          standalone
DefaultServer           on

# Port 21 is the standard FTP port.
Port                {{PORT}}
.
.
.

...有没有办法避免使用replace.sh并使用ansible作为替换replace.sh容器内#!/bin/bash set -e [ -z "${PORT}" ] && echo >&2 "PORT is not set" && exit 1 sed -i "s#{{PORT}}#$PORT#g" /usr/local/etc/ports.conf /usr/local/sbin/proftpd -n -c /usr/local/etc/proftpd.conf 变量的人?

我对容器的实际ansible脚本是:

PORT

恢复所有我需要的是使用Ansible替换配置变量而不是使用在运行服务之前替换变量的shell脚本,是否可能?

非常感谢

1 个答案:

答案 0 :(得分:0)

您正在使用需要预建图像的docker_container模块。文件port.conf在图像内部烘焙。您需要做的是在此文件中设置静态端口。在容器内,你总是使用 静态端口21并且根据计算机,您使用ansible 此端口映射到不同的端口。

内部port.conf始终使用端口21

# Port 21 is the standard FTP port.
Port                21

ansible任务看起来像:

- name: (ftpd) Run container
  docker_container:
    name: "myimagename"
    image: "myimage"
    state: present
    pull: true
    restart_policy: always
networks:
  - name: "{{ network }}"
ports:
 - "{{myportUsingAnsible}}:21"

现在,当您连接到容器时,您需要使用<hostnamne>:{{myportUsingAnsible}}。这是标准的码头工作方式。图像中的端口是静态的,您可以根据该端口更改端口映射 你有的可用端口。