我正在尝试编写一个ansible playbook来设置一些docker容器,然后在它们上运行一个角色:
- hosts: localhost
gather_facts: no
vars:
- docker_test_hosts:
- container_name: 'test_precise'
image_name: 'ubuntu'
image_tag: '12.04'
- container_name: 'test_trusty'
image_name: 'ubuntu'
image_tag: '14.04'
# "Registry returned more than one result for ubuntu:16.04"
#- container_name: 'xenial'
#- image_name: 'ubuntu'
#- image_tag: '16.04'
tasks:
- pip:
name: docker-py
# >= 1.7.0
# using 1.9.0 due to https://github.com/ansible/ansible/issues/17495
version: 1.9.0
state: present
- docker_container:
name: '{{item.container_name}}'
image: '{{item.image_name}}:{{item.image_tag}}'
pull: yes
with_items: '{{docker_test_hosts}}'
- add_host:
name: '{{item.container_name}}'
hostname: '{{item.container_name}}'
ansible_host: '{{item.container_name}}'
ansible_connection: docker
ansible_user: root
groups: docker
with_items: '{{docker_test_hosts}}'
- hosts: docker
tasks:
- debug:
msg: 'hello'
第二场比赛一直失败:
PLAY [localhost] [...] TASK [add_host]已更改:[localhost] =>(item = {u'image_tag':u'12.04',u'image_name':u'ubuntu',u'container_name': u'test_precise'})改变了:[localhost] =>(item = {u'image_tag':u'14.04',u'image_name':u'ubuntu',u'container_name':u'test_trusty'})播放[ docker] TASK [setup]致命:[test_precise]:无法访问! => {“changed”:false,“msg”:“身份验证或权限失败 . 在某些情况下,您可能已经能够对远程目录进行身份验证并且没有权限 . 请考虑更改ansible.cfg中的远程临时路径一个根目录在\“/ tmp \”的路径 . 失败的命令是:(umask 77 && mkdir -p \“echo $ HOME / .ansible / tmp / ansible / tmp-1474479086.86-239783828445202 \”&& echo ansible-tmp-1474479086.86 -239783828445202 = \“echo $ HOME / .ansible / tmp / ansible-tmp-1474479086.86-239783828445202 \”),退出,结果1“,”无法访问“:true} [...]
有任何想法吗?
docker 1.11.2 ansible 2.1.1.0 python 2.7.12 Linux Mint 18 Sarah
1 回答
首先:你需要保持你的docker容器运行,所以使用
应该导致更改的错误消息:
fatal: [test_trusty]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
这意味着python没有安装在容器内 . 所以在使用容器之前,需要在其中安装python . 您可以通过自定义
Dockerfile
并使用创建的docker镜像而不是默认的ubuntu镜像来完成此操作