Somethingwhatever
Somethingwhatever

Reputation: 1348

Convert Docker-compose to ansible playbook?

I have a docker-compose file and i am trying to move it to an ansible playbook. But there are certain parts in the docker-compose file that i am not sure how to handle. I am relatively new to ansible so please excuse any naive mistakes.

Specifically i don't know how to specify the server ,scheduler, scheduled_worker and adhoc_worker part of the docker-compose. Also how do i deal with links under nginx

Here is my Docker-Compose file:-

version: "2"
x-redash-service: &redash-service
  image: redash/redash:8.0.0.b32245
  depends_on:
    - postgres
    - redis
  env_file: /opt/redash/env
  restart: always
services:
  server:
    <<: *redash-service
    command: server
    ports:
      - "5000:5000"
    environment:
      REDASH_WEB_WORKERS: 4
  scheduler:
    <<: *redash-service
    command: scheduler
    environment:
      QUEUES: "celery"
      WORKERS_COUNT: 1
  scheduled_worker:
    <<: *redash-service
    command: worker
    environment:
      QUEUES: "scheduled_queries,schemas"
      WORKERS_COUNT: 1
  adhoc_worker:
    <<: *redash-service
    command: worker
    environment:
      QUEUES: "queries"
      WORKERS_COUNT: 2
  redis:
    image: redis:5.0-alpine
    restart: always
  postgres:
    image: postgres:9.6-alpine
    env_file: /opt/redash/env
    volumes:
      - /opt/redash/postgres-data:/var/lib/postgresql/data
    restart: always
  nginx:
    image: redash/nginx:latest
    ports:
      - "80:80"
    depends_on:
      - server
    links:
      - server:redash
    restart: always

This is my ansible playbook so far:-

    ---

- hosts: redash-server
  tasks:
  - name: install redash container
    docker_container:
    name: redash
    image: redash/redash:8.0.0.b32245
    env:
      env_file: /home/env
    command: server
    restart_policy: always

  - name: rnitiate redash server
    shell: server
    ports:
      - "5000:5000"
    env:
      REDASH_WEB_WORKERS: 4

  - name: initiate redash scheduler service
    shell: scheduler
    env:
      QUEUES: "celery"
      WORKERS_COUNT: 1

  - name: initiate redash scheduled worker
    shell: worker
    env:
      QUEUES: "scheduled_queries,schemas"
      WORKERS_COUNT: 1

  - name: initiate redash adhoc_worker
    shell: worker
    env:
      QUEUES: "queries"
      WORKERS_COUNT: 2

  - name: install redis server
    docker_container:
    name: redis
    image: redis:5.0-alpine
    restart_policy: always

  - name: install postgres container
    docker_container:
    name: postgres
    image: postgres:9.6-alpine
    env:
      env_file: /home/env
      restart_policy: always


            - name: install nginx container
    docker_container:
    name: nginx
    image: nginx:latest
    ports:
      - "80:80"
    when: server
    links:
      - server:redash
    restart_policy: always

I am running into this issue:-

[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: While constructing a mapping from /home/redash-version-8.yml, line 5, column 5, found a duplicate dict key (name). Using last defined value only.
[WARNING]: While constructing a mapping from /home/redash-version-8.yml, line 39, column 5, found a duplicate dict key (name). Using last defined value only.
[WARNING]: While constructing a mapping from /home/redash-version-8.yml, line 45, column 5, found a duplicate dict key (name). Using last defined value only.
[WARNING]: While constructing a mapping from /home/redash-version-8.yml, line 53, column 5, found a duplicate dict key (name). Using last defined value only.
ERROR! conflicting action statements: docker_container, image

The error appears to be in '/home/redash-version-8.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - name: install redash container
    ^ here

Any help will be appreciated. Thank you.

Upvotes: 1

Views: 4495

Answers (1)

Aaron
Aaron

Reputation: 1306

First, this part of the Ansible documentation to get a better idea of how you want to structure your Ansible project. Not sure how familiar you are with Ansible so this may be unnecessary info for you.

Ansible documentation

You can rename "common" to whatever you want. In the "files" directory, put your docker file in there calling it docker-compose.yml so that Ansible can find it.

In the tasks/main.yml file:

---
- name: Write docker-compose file
  copy:
    src: docker-compose.yml
    dest: your-destination

- name: Run docker service
  docker_compose:
    project_src: your-destination

In the playbook that you provided (which is in the directory above the "roles" directory):

- name: configure redash-server
  hosts: redash-server
  roles:
    - role: common (or whatever you decided to rename common to)

At the end (using this example) your folder structure should look like:

/home/ubuntu/playbooks/redash-version-8.yml
/home/ubuntu/playbooks/roles/common/files/docker-compose.yml
/home/ubuntu/playbooks/roles/common/tasks/main.yml

Let me know if that works for you.

If you want to better see what all this fancy syntax is trying to do, change the format of that compose file to look like the following. It takes up more space, but without all the fancy yaml features.

version: "2"
services:
  server:
    image: redash/redash:8.0.0.b32245
    command: server
    ports:
      - "5000:5000"
    environment:
      REDASH_WEB_WORKERS: 4
    depends_on:
      - postgres
      - redis
    env_file: /opt/redash/env
    restart: always
  scheduler:
    image: redash/redash:8.0.0.b32245
    command: scheduler
    environment:
      QUEUES: "celery"
      WORKERS_COUNT: 1
    depends_on:
      - postgres
      - redis
    env_file: /opt/redash/env
    restart: always
  scheduled_worker:
    image: redash/redash:8.0.0.b32245
    command: worker
    environment:
      QUEUES: "scheduled_queries,schemas"
      WORKERS_COUNT: 1
    depends_on:
      - postgres
      - redis
    env_file: /opt/redash/env
    restart: always
  adhoc_worker:
    image: redash/redash:8.0.0.b32245
    command: worker
    environment:
      QUEUES: "queries"
      WORKERS_COUNT: 2
    depends_on:
      - postgres
      - redis
    env_file: /opt/redash/env
    restart: always    
  redis:
    image: redis:5.0-alpine
    restart: always
  postgres:
    image: postgres:9.6-alpine
    env_file: /opt/redash/env
    volumes:
      - /opt/redash/postgres-data:/var/lib/postgresql/data
    restart: always
  nginx:
    image: redash/nginx:latest
    ports:
      - "80:80"
    depends_on:
      - server
    links:
      - server:redash
    restart: always

Upvotes: 3

Related Questions