ansible playbook:设置并连接到docker(身份验证或权限失败)

时间:2016-09-21 17:37:22

标签: docker ansible

我正在尝试编写一个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'

第二场比赛一直失败:

  

播放[localhost]

     

[...]

     

任务[add_host]   已更改:[localhost] => (item = {u' image_tag&#39 ;: u' 12.04',u' image_name':u' ubuntu',u' container_name': U' test_precise'})   已更改:[localhost] => (item = {u' image_tag&#39 ;: u' 14.04',u' image_name':u' ubuntu',u' container_name': U' test_trusty'})

     

播放[docker]

     

任务[设置]   致命:[test_precise]:无法访问! => {"已更改":false," msg":"身份验证或权限失败。在某些情况下,您可能已经能够对远程目录进行身份验证并且没有权限。考虑将ansible.cfg中的远程临时路径更改为以" / tmp \"为根的路径。失败的命令是:(umask 77&& mkdir -p \" echo $HOME/.ansible/tmp/ansible/tmp-1474479086.86-239783828445202 \"&& echo ansible-tmp-1474479086.86-239783828445202 = \" {{1退出结果1","无法访问":true}

     

[...]

有什么想法吗?

docker 1.11.2 ansible 2.1.1.0 python 2.7.12 Linux Mint 18 Sarah

1 个答案:

答案 0 :(得分:3)

首先:你需要让docker容器保持运行,所以使用

- docker_container:
  name: '{{item.container_name}}'
  image: '{{item.image_name}}:{{item.image_tag}}'
  command: tail -f /dev/null
  pull: yes

应该导致更改的错误消息: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并使用创建的泊坞窗图像而不是默认的ubuntu图像来执行此操作