【问题标题】:pass SSH as argument into ansible-playbook将 SSH 作为参数传递给 ansible-playbook
【发布时间】:2017-12-25 22:22:39
【问题描述】:

我有 Dockerfile:

FROM ubuntu:16.04

ARG ssh_prv_key
ARG ssh_pub_key

RUN apt-get update && \
    apt-get install ... USUAL INSTALL STUFF ...

WORKDIR /app/

CMD git clone MY_REPO

我像这样构建图像:

$ docker build -t example --build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)" --build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa.pub)" .

当我从这个图像创建容器时,它会自动提取 git-repo 并使用 -v 参数将其保存在主机上,并使用 --rm 自行销毁。

当我尝试使用 ansible docker_image 模块做同样的事情时,问题就开始了。

剧本看起来像这样:

---
- hosts: localhost
  environment:
    PYTHONPATH: /usr/local/lib/python2.7/site-packages/

  tasks:
  - name: create image from Dockerfile
    docker_image:
      path: /home/demaunt/Jun/dock_click
      dockerfile: biba.dockerfile
      name: myimage
      buildargs: 
        ssh_pub_key: '{{ pub }}'
        ssh_prv_key: '{{ pvt }}'

我是这样开始的:

ansible-playbook bibansible.yml --extra-vars "pub=$(cat ~/.ssh/id_rsa.pub) pvt=$(cat ~/.ssh/id_rsa)"

图像已成功构建,但在运行容器时出现权限错误(公钥)。 我还尝试将参数传递到 .yml 文件中,如下所示:

---
- hosts: localhost
  environment:
    PYTHONPATH: /usr/local/lib/python2.7/site-packages/

  tasks:
  - name: create image from Dockerfile
    docker_image:
      path: /home/demaunt/Jun/dock_click
      dockerfile: biba.dockerfile
      name: myimage
      buildargs: 
        ssh_pub_key:
          command: cat ~/.ssh/id_rsa.pub
        ssh_prv_key: 
          command: cat ~/.ssh/id_rsa

附:我知道将 ssh 密钥传递到图像中并不是最好的选择,但它可以作为临时解决方案。

【问题讨论】:

    标签: docker ssh ansible docker-image


    【解决方案1】:

    你不能在 ansible 的这个级别使用command
    但是要获取本地文件的内容,请看lookup

    [...]
    buildargs: 
        ssh_pub_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
        ssh_prv_key: "{{ lookup('file', '~/.ssh/id_rsa') }}"
    

    【讨论】:

      猜你喜欢
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2013-01-27
      相关资源
      最近更新 更多