Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13787

Error connecting: Error while fetching server API version: Ansible

I'm very new at Ansible. I've run following ansible PlayBook and found those errors:

---
- hosts: webservers
  remote_user: linx
  become: yes
  become_method: sudo
  tasks:

    - name: install docker-py
      pip: name=docker-py

    - name: Build Docker image from Dockerfile
      docker_image:
        name: web
        path: docker
        state: build

    - name: Running the container
      docker_container:
        image: web:latest
        path: docker
        state: running

    - name: Check if container is running
      shell: docker ps

Error message:

FAILED! => {"changed": false, "msg": "Error connecting: Error while fetching server API version: ('Connection aborted.', error(2, 'No such file or directory'))"}

And here is my folder structure:

.
├── ansible.cfg
├── docker
│   └── Dockerfile
├── hosts
├── main.retry
├── main.yml

I'm confused that docker folder is already inside my local but don't know why I encountered those error message.

Upvotes: 14

Views: 34114

Answers (2)

Kartal Tabak
Kartal Tabak

Reputation: 894

I have faced the same problem. I am trying to perform a docker login and get the same weird error. In my case, the ansible user does not have the necessary docker credentials. The solution, in that case, is to switch to a user with docker credentials:

- name: docker login
  hosts: my_server
  become: yes
  become_user: docker_user
  tasks:
    - docker_login:
        registry: myregistry.com
        username: myusername
        password: mysecret

Upvotes: 1

Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13787

I've found solution is Docker daemon is not working after Docker was installed by Ansible. It's required to add following command in my play board.

---
- hosts: webservers
  remote_user: ec2-user
  become: yes
  become_method: sudo
  tasks:
    - name: install docker
      yum: name=docker

    **- name: Ensure service is enabled
      command: service docker restart***

    - name: copying file to remote
      copy:
        src: ./docker
        dest: /home/ec2-user/docker
    - name: Build Docker image from Dockerfile
      docker_image:
        name: web
        path: /home/ec2-user/docker
        state: build
    - name: Running the container
      docker_container:
        image: web:latest
        name: web
    - name: Check if container is running
      shell: docker ps

Upvotes: 10

Related Questions