3
votes

I am trying to write an ansible playbook that sets up some docker containers and then runs a role on them:

- 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'

The second play keeps failing:

PLAY [localhost]

[...]

TASK [add_host] changed: [localhost] => (item={u'image_tag': u'12.04', u'image_name': u'ubuntu', u'container_name': u'test_precise'}) changed: [localhost] => (item={u'image_tag': u'14.04', u'image_name': u'ubuntu', u'container_name': u'test_trusty'})

PLAY [docker]

TASK [setup] fatal: [test_precise]: UNREACHABLE! => {"changed": false, "msg": "Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the remote directory. Consider changing the remote temp path in ansible.cfg to a path rooted in \"/tmp\". Failed command was: ( 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\" ), exited with result 1", "unreachable": true}

[...]

Any ideas?

docker 1.11.2 ansible 2.1.1.0 python 2.7.12 Linux Mint 18 Sarah

1
Are you setting a custom TMP or TMPDIR environment variable anywhere?thrig
Currently I do not.canochordo

1 Answers

4
votes

First of all: You need to keeps your docker containers running, so using

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

Should lead to a changed error message: 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}

This means that python is not installed inside the containers. So before using the containers you need to install python inside them. You could do this via a custom Dockerfile and using the created docker image instead of the default ubuntu one