Ansible docker_container gives "Error getting network id"

admin

Administrator
Staff member
I'm running an ansible playbook from an ansible control located in a docker container. The playbook should create another docker container and attach it to a bridge network. The docker daemon is located on the host machine from which I launched the ansible control container.

My playbook is simple and I can ansible ping the host:

Code:
  - hosts: docker
      become: myuser
      connection: local
      gather_facts: no        
      tasks:   
          - name: Start Node container, connect to network
            docker_container:
              name: node1
              hostname: node1
              docker_host: tcp://172.18.0.1:2375
              image: ubuntu-java
              state: started
              tty: yes
              detach: yes
              restart_policy: on-failure
              networks:
                 - name: my-network

I'm getting this error:

<blockquote>
"msg": "Error getting network id for my-network - ('Connection
aborted.', error(104, 'Connection reset by peer'))"
</blockquote>

UPDATE 1/19/2017:

I can now do this. The provsioning playbook looks something like this:

Code:
- hosts: docker-host
  vars_files:
    - ~/resources/vars/docker-inst-setup.yml

  tasks: 
    - name: debug check
      debug:
        msg: "container is {{ item.cont_name }}"
      with_items: "{{ inst_types }}"
    - name: Start container, connect to network
      become: true
      become_user: "{{ ansible_user_id }}"
      docker_container:
        name: "{{ item.cont_name }}" 
        hostname: "{{ item.cont_name }}"
        image: gf-node:latest
        state: started
        tty: yes
        detach: yes
        restart_policy: on-failure
        network_mode: bridge
        volumes: /home/me/.ssh
        volumes_from: ansible-master 
      register: ansible_docker_container
      with_items: "{{ inst_types }}"

Notes:

<ul>
<li>First I had to set up .ssh between ansible-master and the docker containers I want to provision, which I do in the dockerfiles. </li>
<li>docker-host is defined in inventory file</li>
<li>register: ansible_docker_container just returns some facts from docker_container. It can be printed later in a debug task, but its not very helpful.</li>
</ul>

docker-inst-setup.yml looks something like:

Code:
inst_types:
  - {cont_name: "gf-das", cont_count: 1}
  - {cont_name: "gf-build-node", cont_count: 1}

The process is complicated but I wrote it all up in <a href="https://princesscornflakesit.wordpr...ners-from-a-docker-container-running-ansible/" rel="nofollow noreferrer">this blog article</a>. I hope this helps.