简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM