将 SSH 作为参数传递给 ansible-playbook
pass SSH as argument into ansible-playbook
我有 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
P.S。我知道将 ssh 密钥传递到图像中不是最佳选择,但作为临时解决方案是可以接受的。
你不能在ansible的这个级别使用command
。
但是要获取本地文件的内容,请查看 lookup
[...]
buildargs:
ssh_pub_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
ssh_prv_key: "{{ lookup('file', '~/.ssh/id_rsa') }}"
我有 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
P.S。我知道将 ssh 密钥传递到图像中不是最佳选择,但作为临时解决方案是可以接受的。
你不能在ansible的这个级别使用command
。
但是要获取本地文件的内容,请查看 lookup
[...]
buildargs:
ssh_pub_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
ssh_prv_key: "{{ lookup('file', '~/.ssh/id_rsa') }}"